Class: Hivemind::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/hivemind/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, **values) ⇒ Environment

Returns a new instance of Environment.



8
9
10
# File 'lib/hivemind/environment.rb', line 8

def initialize(parent, **values)
  @parent, @values, @top = parent, values, (parent.nil? ? values : parent.top)
end

Instance Attribute Details

#current_selfObject

Returns the value of attribute current_self.



6
7
8
# File 'lib/hivemind/environment.rb', line 6

def current_self
  @current_self
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/hivemind/environment.rb', line 5

def parent
  @parent
end

#topObject (readonly)

Returns the value of attribute top.



5
6
7
# File 'lib/hivemind/environment.rb', line 5

def top
  @top
end

#valuesObject (readonly)

Returns the value of attribute values.



5
6
7
# File 'lib/hivemind/environment.rb', line 5

def values
  @values
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
15
16
# File 'lib/hivemind/environment.rb', line 12

def [](key)
  value = fetch key
  return value if value
  raise HivemindMissingNameError.new("#{key} is missing")
end

#[]=(key, value) ⇒ Object



27
28
29
# File 'lib/hivemind/environment.rb', line 27

def []=(key, value)
  @values[key] = value
end

#fetch(key) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/hivemind/environment.rb', line 18

def fetch(key)
  current = self
  until current.nil? || current.values.key?(key)
    current = current.parent
  end
  return current.values[key] unless current.nil?
  nil
end