Class: Code::Object::Code
- Inherits:
-
Code::Object
- Object
- Code::Object
- Code::Object::Code
- Defined in:
- lib/code/object/code.rb
Defined Under Namespace
Classes: ScopedObject
Constant Summary collapse
- CLASS_DOCUMENTATION =
{ name: "Code", description: "stores parsed source code for later evaluation.", examples: [ "Code", "Code.evaluate(\"1 + 2\")", "Code.new(\"1 + 2\").evaluate" ] }.freeze
- CLASS_FUNCTIONS =
{ "evaluate" => { name: "evaluate", description: "evaluates source code and returns the result.", examples: [ "Code.evaluate(\"1\")", "Code.evaluate(\"1 + 2\")", "Code.evaluate(\"[1, 2].size\")" ] } }.freeze
- INSTANCE_FUNCTIONS =
{ "evaluate" => { name: "evaluate", description: "evaluates stored source code and returns the result.", examples: [ "Code(\"1\").evaluate", "Code(\"1 + 2\").evaluate", "Code(\"[1, 2].size\").evaluate" ] } }.freeze
Constants inherited from Code::Object
Constants included from Concerns::Shared
Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS
Instance Attribute Summary collapse
-
#trusted ⇒ Object
readonly
Returns the value of attribute trusted.
Attributes included from Concerns::Shared
Class Method Summary collapse
- .call(**args) ⇒ Object
- .code_evaluate(*args, **globals) ⇒ Object
- .function_documentation(scope) ⇒ Object
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_deep_duplicate(_seen = {}) ⇒ Object
- #code_evaluate(trusted_evaluation: false, **globals) ⇒ Object
-
#initialize(*args, **_kargs, &_block) ⇒ Code
constructor
A new instance of Code.
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) ⇒ Code
Returns a new instance of Code.
68 69 70 71 72 |
# File 'lib/code/object/code.rb', line 68 def initialize(*args, **_kargs, &_block) @trusted = args.first.is_a?(Node::Code) self.raw = (trusted ? args.first : Node::Code.new(::Code.parse(args.first.to_s))) end |
Instance Attribute Details
#trusted ⇒ Object (readonly)
Returns the value of attribute trusted.
66 67 68 |
# File 'lib/code/object/code.rb', line 66 def trusted @trusted end |
Class Method Details
.call(**args) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/code/object/code.rb', line 80 def self.call(**args) code_operator = args.fetch(:operator, nil).to_code globals = multi_fetch(args, *GLOBALS) case code_operator.to_s when "evaluate" sig(args) { Object } code_evaluate(*args.fetch(:arguments, []).to_code.raw, **globals) else super end end |
.code_evaluate(*args, **globals) ⇒ Object
74 75 76 77 78 |
# File 'lib/code/object/code.rb', line 74 def self.code_evaluate(*args, **globals) code_args = args.to_code new(*code_args.raw).code_evaluate(**globals) end |
.function_documentation(scope) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/code/object/code.rb', line 38 def self.function_documentation(scope) return INSTANCE_FUNCTIONS if scope == :instance return CLASS_FUNCTIONS if scope == :class {} end |
Instance Method Details
#call(**args) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/code/object/code.rb', line 93 def call(**args) code_operator = args.fetch(:operator, nil).to_code globals = multi_fetch(args, *GLOBALS) case code_operator.to_s when "evaluate" sig(args) code_evaluate(**globals) else super end end |
#code_deep_duplicate(_seen = {}) ⇒ Object
112 113 114 |
# File 'lib/code/object/code.rb', line 112 def code_deep_duplicate(_seen = {}) self.class.new(raw) end |
#code_evaluate(trusted_evaluation: false, **globals) ⇒ Object
106 107 108 109 110 |
# File 'lib/code/object/code.rb', line 106 def code_evaluate(trusted_evaluation: false, **globals) raw.evaluate( **evaluation_globals(globals, trusted_evaluation: trusted_evaluation) ) end |