Class: Cubicle::Data::Level
- Inherits:
-
OrderedHashWithIndifferentAccess
- Object
- BSON::OrderedHash
- OrderedHashWithIndifferentAccess
- Cubicle::Data::Level
- Defined in:
- lib/cubicle/data/level.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dimension ⇒ Object
readonly
Returns the value of attribute dimension.
-
#missing_member_default ⇒ Object
Returns the value of attribute missing_member_default.
-
#parent_level ⇒ Object
readonly
Returns the value of attribute parent_level.
Instance Method Summary collapse
- #[]=(key, val) ⇒ Object
- #flatten(member_name = nil, opts = {}, &block) ⇒ Object
- #hierarchy ⇒ Object
-
#initialize(dimension, parent_level = nil) ⇒ Level
constructor
A new instance of Level.
- #leaf_level? ⇒ Boolean
- #name ⇒ Object
Methods inherited from OrderedHashWithIndifferentAccess
#[], #include?, #method_missing
Constructor Details
#initialize(dimension, parent_level = nil) ⇒ Level
Returns a new instance of Level.
5 6 7 8 9 |
# File 'lib/cubicle/data/level.rb', line 5 def initialize(dimension,parent_level=nil) @dimension = dimension @parent_level = parent_level super() {|hash,key|hash[key]=[]}#Always have an array freshly baked when strangers call end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class OrderedHashWithIndifferentAccess
Instance Attribute Details
#dimension ⇒ Object (readonly)
Returns the value of attribute dimension.
11 12 13 |
# File 'lib/cubicle/data/level.rb', line 11 def dimension @dimension end |
#missing_member_default ⇒ Object
Returns the value of attribute missing_member_default.
12 13 14 |
# File 'lib/cubicle/data/level.rb', line 12 def missing_member_default @missing_member_default end |
#parent_level ⇒ Object (readonly)
Returns the value of attribute parent_level.
11 12 13 |
# File 'lib/cubicle/data/level.rb', line 11 def parent_level @parent_level end |
Instance Method Details
#[]=(key, val) ⇒ Object
43 44 45 46 |
# File 'lib/cubicle/data/level.rb', line 43 def []=(key,val) prepare_level_member(val,key,self) super(key.to_s,val) end |
#flatten(member_name = nil, opts = {}, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cubicle/data/level.rb', line 21 def flatten(member_name = nil, opts={}, &block) default_val = opts[:default] || @missing_member_default || 0 self.values.inject([]) do |output, data| value = data.measure_values value.missing_member_default = default_val if value.respond_to?(:missing_member_default) if block_given? flat_val = block.arity == 1 ? (yield value) : (value.instance_eval(&block)) end flat_val ||= value[member_name] if member_name && value.include?(member_name) flat_val ||= default_val output << flat_val end end |
#hierarchy ⇒ Object
48 49 50 |
# File 'lib/cubicle/data/level.rb', line 48 def hierarchy parent_level || self end |
#leaf_level? ⇒ Boolean
38 39 40 41 |
# File 'lib/cubicle/data/level.rb', line 38 def leaf_level? return self.length < 1 || !self[self.keys[0]].is_a?(Cubicle::Data::Level) end |
#name ⇒ Object
17 18 19 |
# File 'lib/cubicle/data/level.rb', line 17 def name @dimension.name end |