Class: Campa::Context
- Inherits:
-
Object
- Object
- Campa::Context
- Defined in:
- lib/campa/context.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fallback ⇒ Object
Returns the value of attribute fallback.
Instance Method Summary collapse
- #[](symbol) ⇒ Object
- #[]=(symbol, value) ⇒ Object
- #bindings ⇒ Object
- #include?(symbol) ⇒ Boolean
-
#initialize(env = {}) ⇒ Context
constructor
A new instance of Context.
- #push(new_env = {}) ⇒ Object
Constructor Details
#initialize(env = {}) ⇒ Context
Returns a new instance of Context.
5 6 7 |
# File 'lib/campa/context.rb', line 5 def initialize(env = {}) @env = env end |
Instance Attribute Details
#fallback ⇒ Object
Returns the value of attribute fallback.
3 4 5 |
# File 'lib/campa/context.rb', line 3 def fallback @fallback end |
Instance Method Details
#[](symbol) ⇒ Object
13 14 15 16 17 |
# File 'lib/campa/context.rb', line 13 def [](symbol) return env[symbol] if env.include?(symbol) fallback[symbol] if !fallback.nil? end |
#[]=(symbol, value) ⇒ Object
9 10 11 |
# File 'lib/campa/context.rb', line 9 def []=(symbol, value) env[symbol] = value end |
#bindings ⇒ Object
34 35 36 |
# File 'lib/campa/context.rb', line 34 def bindings @bindings ||= env.is_a?(Context) ? env.bindings : env.to_a end |
#include?(symbol) ⇒ Boolean
19 20 21 22 |
# File 'lib/campa/context.rb', line 19 def include?(symbol) env.include?(symbol) || (!fallback.nil? && fallback.include?(symbol)) end |
#push(new_env = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/campa/context.rb', line 24 def push(new_env = {}) # Context is explicit here # (instead of self.class.new) # because we can inherit a context, # like the Lisp::Core does # and then we want a normal context when pushing to it # (and not a Lisp::Core). Context.new(new_env).tap { |c| c.fallback = self } end |