File: organization.rb

package info (click to toggle)
ruby-vagrant-cloud 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 408 kB
  • sloc: ruby: 4,343; makefile: 7
file content (62 lines) | stat: -rw-r--r-- 1,408 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module VagrantCloud
  class Organization < Data::Mutable
    attr_reader :account

    attr_required :username
    attr_optional :boxes, :avatar_url, :profile_html, :profile_markdown

    attr_mutable :boxes

    def initialize(account:, **opts)
      @account = account
      opts[:boxes] ||= []
      super(**opts)
      bxs = boxes.map do |b|
        if !b.is_a?(Box)
          b = Box.load(organization: self, **b)
        end
        b
      end
      clean(data: {boxes: bxs})
    end

    # Add a new box to the organization
    #
    # @param [String] name Name of the box
    # @return [Box]
    def add_box(name)
      if boxes.any? { |b| b.name == name }
        raise Error::BoxError::BoxExistsError,
          "Box with name #{name} already exists"
      end
      b = Box.new(organization: self, name: name)
      clean(data: {boxes: boxes + [b]})
      b
    end

    # Check if this instance is dirty
    #
    # @param [Boolean] deep Check nested instances
    # @return [Boolean] instance is dirty
    def dirty?(key=nil, deep: false)
      if key
        super(key)
      else
        d = super()
        if deep && !d
          d = boxes.any? { |b| b.dirty?(deep: true) }
        end
        d
      end
    end

    # Save the organization
    #
    # @return [self]
    # @note This only saves boxes within organization
    def save
      boxes.map(&:save)
      self
    end
  end
end