Class: Code::Object::Class
Constant Summary collapse
- CLASS_DOCUMENTATION =
{ name: "Class", description: "wraps a value constructor and documents its class and instance functions.", examples: %w[Class Class.new(String) Class.documentation.name] }.freeze
- INSTANCE_FUNCTIONS =
{ "documentation" => { name: "documentation", description: "returns documentation for this class.", examples: %w[ Class.documentation.description List.documentation.name Dictionary.documentation.name ] }, "functions" => { name: "functions", description: "returns documented class functions available on this class.", examples: %w[ Class.functions.keys.include?(:new) List.functions.keys.include?(:new) Dictionary.functions.keys.include?(:from_entries) ] }, "instance_functions" => { name: "instance_functions", description: "returns documented functions available on values built by this class.", examples: %w[ Class.instance_functions.keys.include?(:documentation) List.instance_functions.keys.include?(:map) String.instance_functions.keys.include?(:upcase) ] }, "class_functions" => { name: "class_functions", description: "returns documented class functions available on this class.", examples: %w[ Class.class_functions.keys.include?(:new) List.class_functions.keys.include?(:new) Dictionary.class_functions.keys.include?(:from_entries) ] }, "call" => { name: "call", description: "returns a new value by calling this class constructor.", examples: [ "Class.call(String)", "List.call([1, 2])", "String.call(:hello)" ] }, "extend" => { name: "extend", description: "returns a function that builds a value from this class before running the body.", examples: [ "Widget = Dictionary.extend(() => { self.name = :widget self }) Widget().fetch(:name)", "Person = Dictionary.extend((name) => { self.name = name self }) Person(:Ada).fetch(:name)", "Counter = Dictionary.extend(() => { self.count = 0 self }) Counter().fetch(:count)" ] } }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_call(*arguments, **_globals) ⇒ Object
- #code_class_functions ⇒ Object
- #code_documentation ⇒ Object
- #code_extend(function) ⇒ Object
- #code_functions ⇒ Object
- #code_instance_functions ⇒ Object
- #code_to_string ⇒ Object
-
#initialize(*args, **_kargs, &_block) ⇒ Class
constructor
A new instance of Class.
Constructor Details
#initialize(*args, **_kargs, &_block) ⇒ Class
Returns a new instance of Class.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/code/object/class.rb', line 79 def initialize(*args, **_kargs, &_block) self.raw = if args.first.is_a?(Class) args.first.raw elsif args.first.is_an?(Object) args.first.class elsif args.first && args.first.ancestors.include?(Object) args.first else Nothing end end |
Class Method Details
.function_documentation(scope) ⇒ Object
73 74 75 76 77 |
# File 'lib/code/object/class.rb', line 73 def self.function_documentation(scope) return INSTANCE_FUNCTIONS if scope == :instance {} end |
Instance Method Details
#call(**args) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/code/object/class.rb', line 92 def call(**args) code_operator = args.fetch(:operator, nil).to_code dynamic_result = code_dynamic_call(code_operator, **args) return dynamic_result if dynamic_result case code_operator.to_s when "documentation" sig(args) code_documentation when "functions" sig(args) code_functions when "instance_functions" sig(args) code_instance_functions when "class_functions" sig(args) code_class_functions when "extend" sig(args) { Function } code_extend(args.fetch(:arguments).code_first) when "call" sig(args) { Object.repeat } code_call(*args.fetch(:arguments, []).to_code.raw) when /=$/ if raw_class_functions.code_has_key?(code_operator).truthy? raw.call(**args) else super end else raw.call(**args) end end |
#code_call(*arguments, **_globals) ⇒ Object
127 128 129 |
# File 'lib/code/object/class.rb', line 127 def code_call(*arguments, **_globals) raw.code_new(*arguments) end |
#code_class_functions ⇒ Object
151 152 153 |
# File 'lib/code/object/class.rb', line 151 def code_class_functions raw_class_functions end |
#code_documentation ⇒ Object
155 156 157 |
# File 'lib/code/object/class.rb', line 155 def code_documentation Object.documentation_for(raw) end |
#code_extend(function) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/code/object/class.rb', line 131 def code_extend(function) code_function = function.to_code Function.new( code_function.code_parameters, code_function.code_body.raw, code_function.definition_context, code_function.definition_object, parent: self ) end |
#code_functions ⇒ Object
143 144 145 |
# File 'lib/code/object/class.rb', line 143 def code_functions code_class_functions end |
#code_instance_functions ⇒ Object
147 148 149 |
# File 'lib/code/object/class.rb', line 147 def code_instance_functions Object.documented_functions_for(raw, :instance) end |