Class: Campa::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/campa/context.rb

Direct Known Subclasses

Lisp::Core

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fallbackObject

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

#bindingsObject



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

Returns:

  • (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