Class: Hivemind::Environment
- Inherits:
-
Object
- Object
- Hivemind::Environment
- Defined in:
- lib/hivemind/environment.rb
Instance Attribute Summary collapse
-
#current_self ⇒ Object
Returns the value of attribute current_self.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#top ⇒ Object
readonly
Returns the value of attribute top.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(parent, **values) ⇒ Environment
constructor
A new instance of Environment.
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_self ⇒ Object
Returns the value of attribute current_self.
6 7 8 |
# File 'lib/hivemind/environment.rb', line 6 def current_self @current_self end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/hivemind/environment.rb', line 5 def parent @parent end |
#top ⇒ Object (readonly)
Returns the value of attribute top.
5 6 7 |
# File 'lib/hivemind/environment.rb', line 5 def top @top end |
#values ⇒ Object (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 |