Class: Code::Object::Context

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary collapse

Attributes inherited from Code::Object

#raw

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_empty?, #code_except, #code_fetch, #code_fetch_values, #code_flatten, #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_merge!, #code_select, #code_select!, #code_set, #code_size, #code_superior, #code_superior_or_equal, #code_to_context, #code_to_list, #code_transform_values, #code_values

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, #call, call, code_and_operator, #code_and_operator, code_as_json, #code_as_json, 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_new, #code_or_operator, code_or_operator, #code_self, code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, inspect, maybe, #multi_fetch, multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **_kargs) ⇒ Context

Returns a new instance of Context.



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

def initialize(*args, **_kargs, &)
  super(args.first)
  @parent = args.second if args.second.is_a?(Dictionary)
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

Instance Method Details

#code_lookup!(identifier) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/code/object/context.rb', line 13

def code_lookup!(identifier)
  code_identifier = identifier.to_code

  if code_has_key?(code_identifier).truthy?
    self
  elsif parent?
    parent.code_lookup!(code_identifier)
  else
    raise Error, "#{code_identifier} is not defined"
  end
end

#merge(other) ⇒ Object



25
26
27
# File 'lib/code/object/context.rb', line 25

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

#parent?Boolean

Returns:



29
30
31
# File 'lib/code/object/context.rb', line 29

def parent?
  !!parent
end