Class: Code::Object::Context
- Inherits:
-
Dictionary
- Object
- Code::Object
- Dictionary
- Code::Object::Context
- Defined in:
- lib/code/object/context.rb
Constant Summary collapse
- CLASS_DOCUMENTATION =
{ name: "Context", description: "stores scoped identifier values used while evaluating code.", examples: [ "context", "Context.new(a: 1)", "Context.new({ a: 1 }, Context.new(b: 2)).lookup!(:b)" ] }.freeze
- INSTANCE_FUNCTIONS =
{ "lookup!" => { name: "lookup!", description: "returns the context that defines an identifier or raises when it is missing.", examples: [ "Context.new(a: 1).lookup!(:a)", "Context.new({ a: 1 }, Context.new(b: 2)).lookup!(:b)", "Context.new(a: 1).lookup!(:missing) rescue nothing" ] } }.freeze
Constants inherited from Dictionary
Constants inherited from Code::Object
CLASS_FUNCTIONS, NUMBER_CLASSES
Constants included from Concerns::Shared
Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Attributes included from Concerns::Shared
Class Method Summary collapse
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_deep_duplicate(seen = {}) ⇒ Object
- #code_lookup!(identifier) ⇒ Object
-
#initialize(*args, **_kargs, &_block) ⇒ Context
constructor
A new instance of Context.
- #merge(other) ⇒ Object
- #parent? ⇒ Boolean
Methods inherited from Dictionary
#<=>, call, #code_any?, code_assign, #code_associate, #code_clear, #code_compact, #code_compact!, #code_delete, #code_delete_if, #code_delete_unless, #code_dig, #code_each, #code_each_key, #code_each_pair, #code_each_value, #code_empty?, code_entries, #code_except, #code_fetch, #code_fetch_values, #code_flatten, code_from_entries, #code_get, #code_has_key?, code_has_own?, #code_has_own?, #code_has_value?, #code_invert, #code_keep_if, #code_keep_unless, #code_key, #code_keys, #code_map, #code_merge, #code_merge!, #code_reject, #code_reject!, #code_replace, #code_right_associate, #code_select, #code_select!, #code_set, #code_shift, #code_size, #code_slice, #code_store, #code_to_context, #code_to_dictionary, #code_to_list, #code_to_query, #code_transform_keys, #code_transform_keys!, #code_transform_values, #code_transform_values!, #code_update, #code_values, #code_values_at, #present?
Methods inherited from Code::Object
class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, instance_functions, maybe, #name, repeat, sorted_dictionary, |
Methods included from Concerns::Shared
#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?
Constructor Details
#initialize(*args, **_kargs, &_block) ⇒ Context
Returns a new instance of Context.
37 38 39 40 |
# File 'lib/code/object/context.rb', line 37 def initialize(*args, **_kargs, &_block) super(args.first) @parent = args.second if args.second.is_a?(Dictionary) end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
35 36 37 |
# File 'lib/code/object/context.rb', line 35 def parent @parent end |
Class Method Details
.function_documentation(scope) ⇒ Object
29 30 31 32 33 |
# File 'lib/code/object/context.rb', line 29 def self.function_documentation(scope) return INSTANCE_FUNCTIONS if scope == :instance {} end |
Instance Method Details
#call(**args) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/code/object/context.rb', line 42 def call(**args) code_operator = args.fetch(:operator, nil).to_code case code_operator.to_s when "lookup!" sig(args) { Object } code_lookup!(args.fetch(:arguments, []).to_code.code_first) else super end end |
#code_deep_duplicate(seen = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/code/object/context.rb', line 70 def code_deep_duplicate(seen = {}) seen.compare_by_identity unless seen.compare_by_identity? return seen[self] if seen.key?(self) duplicate = Context.new seen[self] = duplicate raw.each do |key, value| duplicate.code_set( key.code_deep_duplicate(seen), value.code_deep_duplicate(seen) ) end duplicate.parent = parent&.code_deep_duplicate(seen) duplicate end |
#code_lookup!(identifier) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/code/object/context.rb', line 54 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 |