Class: Contextuality::Context

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

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



3
4
5
# File 'lib/contextuality/context.rb', line 3

def initialize
  @scopes = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



38
39
40
# File 'lib/contextuality/context.rb', line 38

def method_missing method, *args, &block
  self[method]
end

Instance Method Details

#[](name) ⇒ Object



23
24
25
26
27
# File 'lib/contextuality/context.rb', line 23

def [] name
  name = name.to_s
  scope = @scopes.detect { |scope| scope.key? name }
  scope ? scope[name] : Contextuality.defaults[name.to_sym]
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/contextuality/context.rb', line 34

def empty?
  !@scopes.any? { |scope| !scope.empty? }
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/contextuality/context.rb', line 29

def key? name
  name = name.to_s
  @scopes.any? { |scope| scope.key? name }
end

#popObject



18
19
20
21
# File 'lib/contextuality/context.rb', line 18

def pop
  Contextuality.log "#{'=' * @scopes.size} Exiting scope"
  @scopes.shift
end

#push(variables) ⇒ Object



13
14
15
16
# File 'lib/contextuality/context.rb', line 13

def push variables
  @scopes.unshift Hash[variables.map { |(name, variable)| [name.to_s, variable] }]
  Contextuality.log "#{'=' * @scopes.size} Entering scope: #{scope_inspect @scopes.first}"
end

#scope_inspect(scope) ⇒ Object



7
8
9
10
11
# File 'lib/contextuality/context.rb', line 7

def scope_inspect scope
  scope.map do |name, value|
    "#{name}: #{value.inspect}"
  end.to_a.join(', ')
end