Class: Code::Object::Class

Inherits:
Code::Object show all
Defined in:
lib/code/object/class.rb

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

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

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Instance Method Summary collapse

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_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #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_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) ⇒ 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_functionsObject



150
151
152
# File 'lib/code/object/class.rb', line 150

def code_class_functions
  raw_class_functions
end

#code_documentationObject



154
155
156
# File 'lib/code/object/class.rb', line 154

def code_documentation
  Object.documentation_for(raw)
end

#code_extend(function) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# 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,
    parent: self
  )
end

#code_functionsObject



142
143
144
# File 'lib/code/object/class.rb', line 142

def code_functions
  code_class_functions
end

#code_instance_functionsObject



146
147
148
# File 'lib/code/object/class.rb', line 146

def code_instance_functions
  Object.documented_functions_for(raw, :instance)
end

#code_to_stringObject



158
159
160
# File 'lib/code/object/class.rb', line 158

def code_to_string
  String.new(raw.name.to_s.split("::")[2..].join("::"))
end