Class: Groupy::Group
- Inherits:
-
Object
- Object
- Groupy::Group
- Defined in:
- lib/groupy.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sub_groups ⇒ Object
readonly
Returns the value of attribute sub_groups.
Instance Method Summary collapse
-
#initialize(name, &block) ⇒ Group
constructor
A new instance of Group.
- #subgroups ⇒ Object
- #value_groups ⇒ Object
- #values ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
33 34 35 |
# File 'lib/groupy.rb', line 33 def name @name end |
#sub_groups ⇒ Object (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
#subgroups ⇒ Object
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_groups ⇒ Object
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 |
#values ⇒ Object
35 36 37 |
# File 'lib/groupy.rb', line 35 def values value_groups.map{|g| g.value} end |