Class: Code::Object::Context

Inherits:
Dictionary show all
Defined in:
lib/code/object/context.rb

Instance Attribute Summary collapse

Attributes inherited from Dictionary

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dictionary

#call, #code_any?, #code_clear, #code_compact, #code_compact!, #code_compare, #code_delete, #code_delete_if, #code_delete_unless, #code_dig, #code_each, #code_eight?, #code_empty?, #code_except, #code_fetch, #code_fetch_values, #code_five?, #code_flatten, #code_four?, #code_get, #code_has_key?, #code_has_value?, #code_inferior, #code_inferior_or_equal, #code_invert, #code_keep_if, #code_keep_unless, #code_key, #code_keys, #code_merge, #code_nine?, #code_one?, #code_select, #code_select!, #code_set, #code_seven?, #code_six?, #code_size, #code_superior, #code_superior_or_equal, #code_ten?, #code_three?, #code_to_context, #code_to_list, #code_transform_values, #code_two?, #code_values, #code_zero?, #inspect, #to_s

Methods inherited from Code::Object

#<=>, #==, call, #call, #code_and_operator, code_and_operator, #code_different, code_different, code_equal_equal, #code_equal_equal, #code_equal_equal_equal, code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_or_operator, #code_or_operator, code_self, #code_self, code_to_string, #code_to_string, #falsy?, falsy?, hash, #hash, maybe, multi_fetch, #multi_fetch, repeat, #sig, sig, to_s, #to_s, truthy?, #truthy?, |

Constructor Details

#initialize(raw = {}, parent: nil) ⇒ Context

Returns a new instance of Context.



8
9
10
11
# File 'lib/code/object/context.rb', line 8

def initialize(raw = {}, parent: nil)
  @raw = raw
  @parent = parent
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/code/object/context.rb', line 6

def parent
  @parent
end

Class Method Details

.nameObject



13
14
15
# File 'lib/code/object/context.rb', line 13

def self.name
  "Context"
end

Instance Method Details

#lookup!(identifier) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/code/object/context.rb', line 17

def lookup!(identifier)
  if code_has_key?(identifier).truthy?
    self
  elsif parent?
    parent.lookup!(identifier)
  else
    raise Code::Error::Undefined, "#{identifier} is not defined"
  end
end

#merge(other) ⇒ Object



27
28
29
# File 'lib/code/object/context.rb', line 27

def merge(other)
  Context.new(raw.merge(other.raw), parent: parent || other.parent)
end

#parent?Boolean

Returns:



31
32
33
# File 'lib/code/object/context.rb', line 31

def parent?
  !!parent
end