Class: Groupy::Group

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

Direct Known Subclasses

OuterGroup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Group

Returns a new instance of Group.



26
27
28
29
30
31
# File 'lib/groupy.rb', line 26

def initialize(name, &block)
  @name = name
  @sub_groups = []

  instance_eval(&block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/groupy.rb', line 32

def name
  @name
end

#sub_groupsObject (readonly)

Returns the value of attribute sub_groups.



32
33
34
# File 'lib/groupy.rb', line 32

def sub_groups
  @sub_groups
end

Instance Method Details

#subgroupsObject



48
49
50
51
52
53
54
55
56
# File 'lib/groupy.rb', line 48

def subgroups
  self.sub_groups.inject({}) do |hash, g|
    if g.respond_to?(:subgroups)
      hash.merge!(g.subgroups)
    end
    hash[g.name] = g.values
    hash
  end
end

#value_groupsObject



38
39
40
41
42
43
44
45
46
# File 'lib/groupy.rb', line 38

def value_groups
  self.sub_groups.inject([]) do |array, sg|
    if sg.is_a?(Value)
      array << sg
    else
      array += sg.value_groups
    end
  end
end

#valuesObject



34
35
36
# File 'lib/groupy.rb', line 34

def values
  value_groups.map{|g| g.value}
end