Class: Keyrack::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/keyrack/group.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) ⇒ Group

Returns a new instance of Group.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/keyrack/group.rb', line 3

def initialize(arg = nil)
  @attributes = {}

  case arg
  when String
    @attributes['name'] = arg
    @attributes['sites'] = []
    @attributes['groups'] = {}
  when Hash
    load(arg)
  when nil
    @uninitialized = true
  end

  @after_event = []
end

Instance Method Details

#add_group(group) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/keyrack/group.rb', line 138

def add_group(group)
  raise "add_group is not allowed until Group is initialized" if @uninitialized && !@loading
  add_group_without_callbacks(group)

  event = Event.new(self, 'add')
  event.collection_name = 'groups'
  event.object = group
  trigger(event)
end

#add_site(site) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/keyrack/group.rb', line 110

def add_site(site)
  raise "add_site is not allowed until Group is initialized" if @uninitialized && !@loading
  add_site_without_callbacks(site)

  event = Event.new(self, 'add')
  event.collection_name = 'sites'
  event.object = site
  trigger(event)
end

#after_event(&block) ⇒ Object



169
170
171
# File 'lib/keyrack/group.rb', line 169

def after_event(&block)
  @after_event << block
end

#change_attribute(name, value) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/keyrack/group.rb', line 84

def change_attribute(name, value)
  event = Event.new(self, 'change')
  event.attribute_name = name
  event.previous_value = @attributes[name]
  event.new_value = value

  @attributes[name] = value
  trigger(event)
end

#group(group_name) ⇒ Object



161
162
163
# File 'lib/keyrack/group.rb', line 161

def group(group_name)
  groups[group_name]
end

#group_namesObject



165
166
167
# File 'lib/keyrack/group.rb', line 165

def group_names
  groups.keys
end

#groupsObject



106
107
108
# File 'lib/keyrack/group.rb', line 106

def groups
  @attributes['groups']
end

#load(hash) ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/keyrack/group.rb', line 20

def load(hash)
  @loading = true

  if !hash.has_key?('name')
    raise ArgumentError, "hash is missing the 'name' key"
  end
  if !hash['name'].is_a?(String)
    raise ArgumentError, "name is not a String"
  end
  @attributes['name'] = hash['name']

  if !hash.has_key?('sites')
    raise ArgumentError, "hash is missing the 'sites' key"
  end
  if !hash['sites'].is_a?(Array)
    raise ArgumentError, "sites is not an Array"
  end

  if !hash.has_key?('groups')
    raise ArgumentError, "hash is missing the 'groups' key"
  end
  if !hash['groups'].is_a?(Hash)
    raise ArgumentError, "groups is not a Hash"
  end

  @attributes['sites'] = []
  hash['sites'].each_with_index do |site_hash, site_index|
    if !site_hash.is_a?(Hash)
      raise ArgumentError, "site #{site_index} is not a Hash"
    end

    begin
      site = Site.new(site_hash)
      add_site_without_callbacks(site)
    rescue SiteError => e
      raise ArgumentError, "site #{site_index} is not valid: #{e.message}"
    end
  end

  @attributes['groups'] = {}
  hash['groups'].each_pair do |group_name, group_hash|
    if !group_name.is_a?(String)
      raise ArgumentError, "group key is not a String"
    end
    if !group_hash.is_a?(Hash)
      raise ArgumentError, "group value for #{group_name.inspect} is not a Hash"
    end

    begin
      group = Group.new(group_hash)
      add_group_without_callbacks(group)
    rescue ArgumentError => e
      raise ArgumentError, "group #{group_name.inspect} is not valid: #{e.message}"
    end

    if group.name != group_name
      raise ArgumentError, "group name mismatch: #{group_name.inspect} != #{group.name.inspect}"
    end
  end

  @loading = false
  @uninitialized = false
end

#nameObject



94
95
96
# File 'lib/keyrack/group.rb', line 94

def name
  @attributes['name']
end

#name=(name) ⇒ Object



98
99
100
# File 'lib/keyrack/group.rb', line 98

def name=(name)
  change_attribute('name', name)
end

#remove_group(group_name) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/keyrack/group.rb', line 148

def remove_group(group_name)
  raise "remove_group is not allowed until Group is initialized" if @uninitialized && !@loading
  if !groups.has_key?(group_name)
    raise GroupError, "group doesn't exist"
  end
  group = groups.delete(group_name)

  event = Event.new(self, 'remove')
  event.collection_name = 'groups'
  event.object = group
  trigger(event)
end

#remove_site(site) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/keyrack/group.rb', line 124

def remove_site(site)
  raise "remove_site is not allowed until Group is initialized" if @uninitialized && !@loading
  index = sites.index(site)
  if index.nil?
    raise GroupError, "site doesn't exist"
  end
  site = sites.delete_at(index)

  event = Event.new(self, 'remove')
  event.collection_name = 'sites'
  event.object = site
  trigger(event)
end

#site(index) ⇒ Object



120
121
122
# File 'lib/keyrack/group.rb', line 120

def site(index)
  sites[index]
end

#sitesObject



102
103
104
# File 'lib/keyrack/group.rb', line 102

def sites
  @attributes['sites']
end

#to_hObject



173
174
175
176
177
178
179
180
181
# File 'lib/keyrack/group.rb', line 173

def to_h
  hash = @attributes.dup
  hash['sites'] = hash['sites'].collect(&:to_h)
  hash['groups'] = hash['groups'].inject({}) do |hash2, (key, value)|
    hash2[key] = value.to_h
    hash2
  end
  hash
end