State.account!   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
1
# frozen_string_literal: true
2
3
require_relative '../mixin/entity'
4
5
require_relative 'account'
6
require_relative 'group'
7
8
module AMA
9
  module Chef
10
    module User
11
      module Model
12
        # Represents complete node state
13
        class State
14
          include Mixin::Entity
15
16
          # !@attribute groups
17
          #   @return [Hash{Symbol, Group}]
18
          attribute :groups, [Hash, K: Symbol, V: Group], default: {}
19
          # !@attribute accounts
20
          #   @return [Hash{Symbol, Account}]
21
          attribute :accounts, [Hash, K: Symbol, V: Account], default: {}
22
          # !@attribute version
23
          #   @return [Integer]
24
          attribute :version, Integer, default: 1
25
26
          def initialize
27
            @groups = {}
28
            @accounts = {}
29
            @version = 1
30
          end
31
32
          # @return [AMA::Chef::User::Model::Account]
33
          def account!(id)
34
            id = id.to_sym
35
            unless accounts[id]
36
              accounts[id] = Account.new
37
              accounts[id].id = id
38
              accounts[id].policy = Policy::NONE
39
            end
40
            account(id)
41
          end
42
43
          # @return [AMA::Chef::User::Model::Account]
44
          def account(id)
45
            accounts[id.to_sym]
46
          end
47
48
          # @return [AMA::Chef::User::Model::Group]
49
          def group!(id)
50
            id = id.to_sym
51
            unless groups[id]
52
              groups[id] = Group.new
53
              groups[id].id = id
54
            end
55
            group(id)
56
          end
57
58
          # @return [AMA::Chef::User::Model::Group]
59
          def group(id)
60
            groups[id.to_sym]
61
          end
62
        end
63
      end
64
    end
65
  end
66
end
67