Class: Code::Object::String

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

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, 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_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

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

Returns a new instance of String.



6
7
8
9
10
# File 'lib/code/object/string.rb', line 6

def initialize(*args, **_kargs, &)
  raw = args.first || Nothing.new
  raw = raw.raw if raw.is_a?(Object)
  @raw = raw.to_s
end

Instance Method Details

#call(**args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/code/object/string.rb', line 12

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, List.new)
  globals = multi_fetch(args, *GLOBALS)
  value = arguments.code_first

  case operator.to_s
  when "&", "to_function"
    sig(args)
    code_to_function(**globals)
  when "*"
    sig(args) { Integer | Decimal }
    code_multiplication(value)
  when "+"
    sig(args) { Object }
    code_plus(value)
  when "downcase"
    sig(args)
    code_downcase
  when "include?"
    sig(args) { String }
    code_include?(value)
  when "reverse"
    sig(args)
    code_reverse
  else
    super
  end
end

#code_downcaseObject



42
43
44
# File 'lib/code/object/string.rb', line 42

def code_downcase
  String.new(raw.downcase)
end

#code_include?(value) ⇒ Boolean

Returns:



46
47
48
# File 'lib/code/object/string.rb', line 46

def code_include?(value)
  Boolean.new(raw.include?(value.raw))
end

#code_multiplication(other) ⇒ Object



50
51
52
# File 'lib/code/object/string.rb', line 50

def code_multiplication(other)
  String.new(raw * other.raw)
end

#code_plus(other) ⇒ Object



54
55
56
# File 'lib/code/object/string.rb', line 54

def code_plus(other)
  String.new(raw + other.to_s)
end

#code_reverseObject



58
59
60
# File 'lib/code/object/string.rb', line 58

def code_reverse
  String.new(raw.reverse)
end

#code_to_function(**globals) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/code/object/string.rb', line 62

def code_to_function(**globals)
  Node::Code.new(
    [
      {
        function: {
          parameters: [{ name: "_" }],
          body: [
            {
              left_operation: {
                first: {
                  call: {
                    name: "_"
                  }
                },
                others: [
                  { operator: ".", statement: { call: { name: raw } } }
                ]
              }
            }
          ]
        }
      }
    ]
  ).evaluate(**globals)
end