Class: Code::Object::String

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, call, #code_and_operator, code_and_operator, #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_or_operator, #code_or_operator, code_self, #code_self, code_to_string, #code_to_string, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, repeat, sig, #sig, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(string) ⇒ String

Returns a new instance of String.



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

def initialize(string)
  string = string.raw if string.is_a?(String)
  @raw = string.to_s
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Class Method Details

.nameObject



13
14
15
# File 'lib/code/object/string.rb', line 13

def self.name
  "String"
end

Instance Method Details

#as_jsonObject



105
106
107
# File 'lib/code/object/string.rb', line 105

def as_json(...)
  raw.as_json(...)
end

#call(**args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/code/object/string.rb', line 17

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *GLOBALS)
  value = arguments.first&.value

  case operator.to_s
  when "&", "to_function"
    sig(args)
    code_to_function(**globals)
  when "*"
    sig(args) { Number }
    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



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

def code_downcase
  String.new(raw.downcase)
end

#code_include?(value) ⇒ Boolean

Returns:



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

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

#code_multiplication(other) ⇒ Object



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

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

#code_plus(other) ⇒ Object



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

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

#code_reverseObject



63
64
65
# File 'lib/code/object/string.rb', line 63

def code_reverse
  String.new(raw.reverse)
end

#code_to_function(**globals) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/code/object/string.rb', line 67

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

#inspectObject



93
94
95
# File 'lib/code/object/string.rb', line 93

def inspect
  raw.inspect
end

#succObject



97
98
99
# File 'lib/code/object/string.rb', line 97

def succ
  String.new(raw.succ)
end

#to_sObject



101
102
103
# File 'lib/code/object/string.rb', line 101

def to_s
  raw
end