Class: Cult::Definition
- Inherits:
-
Object
- Object
- Cult::Definition
- Defined in:
- lib/cult/definition.rb
Instance Attribute Summary collapse
-
#bag ⇒ Object
(also: #to_h)
readonly
Returns the value of attribute bag.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #definition_parameters ⇒ Object
- #definition_parents ⇒ Object
- #definition_path ⇒ Object
- #direct(k) ⇒ Object
-
#initialize(object) ⇒ Definition
constructor
A new instance of Definition.
- #inspect ⇒ Object (also: #to_s)
Constructor Details
#initialize(object) ⇒ Definition
Returns a new instance of Definition.
8 9 10 |
# File 'lib/cult/definition.rb', line 8 def initialize(object) @object = object end |
Instance Attribute Details
#bag ⇒ Object (readonly) Also known as: to_h
Returns the value of attribute bag.
6 7 8 |
# File 'lib/cult/definition.rb', line 6 def bag @bag end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
5 6 7 |
# File 'lib/cult/definition.rb', line 5 def object @object end |
Instance Method Details
#[](k) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cult/definition.rb', line 51 def [](k) fail ArgumentError unless k.is_a?(String) if bag.key?(k) bag[k] else parent_responses = definition_parents.map do |p| [p, p.definition[k]] end.reject do |k, v| v.nil? end consensus = parent_responses.group_by(&:last) if consensus.empty? return nil elsif consensus.size != 1 msg = "#{object.inspect}: I didn't have key '#{k}', and " + "my parents had conflicting answers: " + "[answer, parents]: #{consensus}" fail KeyError, msg end consensus.keys[0] end end |
#[]=(k, v) ⇒ Object
75 76 77 78 |
# File 'lib/cult/definition.rb', line 75 def []=(k, v) fail "Use string keys" unless k.is_a?(String) bag[k] = v end |
#definition_parameters ⇒ Object
12 13 14 |
# File 'lib/cult/definition.rb', line 12 def definition_parameters object.definition_parameters end |
#definition_parents ⇒ Object
20 21 22 |
# File 'lib/cult/definition.rb', line 20 def definition_parents object.definition_parents end |
#definition_path ⇒ Object
16 17 18 |
# File 'lib/cult/definition.rb', line 16 def definition_path object.definition_path end |
#direct(k) ⇒ Object
45 46 47 48 |
# File 'lib/cult/definition.rb', line 45 def direct(k) fail ArgumentError unless k.is_a?(String) bag[k] end |
#inspect ⇒ Object Also known as: to_s
24 25 26 27 28 29 30 |
# File 'lib/cult/definition.rb', line 24 def inspect "\#<#{self.class.name} " + "object: #{object.inspect}, " + "params: #{definition_parameters}, " + "parents: #{definition_parents}, " + "bag: #{bag}>" end |