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.



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

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

  instance_eval(&block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#sub_groupsObject (readonly)

Returns the value of attribute sub_groups.



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

def sub_groups
  @sub_groups
end

Instance Method Details

#subgroupsObject



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

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



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

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



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

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