Class: Cult::Definition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bagObject (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

#objectObject (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_parametersObject



12
13
14
# File 'lib/cult/definition.rb', line 12

def definition_parameters
  object.definition_parameters
end

#definition_parentsObject



20
21
22
# File 'lib/cult/definition.rb', line 20

def definition_parents
  object.definition_parents
end

#definition_pathObject



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

#inspectObject 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