Class: Microkit::Group

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, values) ⇒ Group

Returns a new instance of Group.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/microkit/group.rb', line 6

def initialize(type, values)
     if !['config', 'features'].include? type then 
        raise Error('Group type is not valid')
     end
     @change = Publisher.new
     @group = {}
     @type = type
     @updated = false
     @initialized = false
     self.update(values)
end

Instance Attribute Details

#changeObject (readonly)

Returns the value of attribute change.



5
6
7
# File 'lib/microkit/group.rb', line 5

def change
  @change
end

Instance Method Details

#update(values) ⇒ 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
# File 'lib/microkit/group.rb', line 18

def update (values)
  prev_value = self.value
  values.each do |key, item|
      if @group.key?(key) then
          @group[key].update(item)
      else
          if @initialized then
              @updated = true
          end
          if item.key?("value") and item["value"].class != Hash then
            if @type == "config" then
              @group[key] = ConfigItem.new(item, key)
            elsif @type == "features" then
              @group[key] = FeatureItem.new(item, key)
            end
            published = Proc.new do |current_val, prev_val|
              @updated = true
            end
            
            @group[key].change.subscribe(published)
          else
              @group[key] =  Group.new(@type, item)
              published = Proc.new do |current_val, prev_val|
                @updated = true
              end
              @group[key].change.subscribe(published)
          end
      end
      var_name = "@#{key}"  # the '@' is required
      self.instance_variable_set(var_name, @group[key])
      self.class.module_eval { attr_accessor key}
      
  end
  if !@initialized then
      @initialized = true 
  end
  if @updated then
      @change.publish(self.value ,prev_value)
      @updated = false 
  end  
end

#valueObject



59
60
61
# File 'lib/microkit/group.rb', line 59

def value
  @group.reduce({}) {|a, (key, item)| a[key] = item.value; return a}
end