Class: Keyrack::Group

Inherits:
Hash
  • 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
# File 'lib/keyrack/group.rb', line 3

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

  @after_event = []
end

Instance Method Details

#add_group(group) ⇒ Object



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

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



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

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



167
168
169
# File 'lib/keyrack/group.rb', line 167

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

#change_attribute(name, value) ⇒ Object



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

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

  self[name] = value
  trigger(event)
end

#encode_with(coder) ⇒ Object



171
172
173
# File 'lib/keyrack/group.rb', line 171

def encode_with(coder)
  coder.represent_map(nil, self)
end

#group(group_name) ⇒ Object



159
160
161
# File 'lib/keyrack/group.rb', line 159

def group(group_name)
  groups[group_name]
end

#group_namesObject



163
164
165
# File 'lib/keyrack/group.rb', line 163

def group_names
  groups.keys
end

#groupsObject



104
105
106
# File 'lib/keyrack/group.rb', line 104

def groups
  self['groups']
end

#load(hash) ⇒ Object



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

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
  self['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

  self['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

  self['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



92
93
94
# File 'lib/keyrack/group.rb', line 92

def name
  self['name']
end

#name=(name) ⇒ Object



96
97
98
# File 'lib/keyrack/group.rb', line 96

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

#remove_group(group_name) ⇒ Object



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

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



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

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



118
119
120
# File 'lib/keyrack/group.rb', line 118

def site(index)
  sites[index]
end

#sitesObject



100
101
102
# File 'lib/keyrack/group.rb', line 100

def sites
  self['sites']
end