Class: Code::Object::Range

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary collapse

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_code, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **kargs) ⇒ Range

Returns a new instance of Range.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/code/object/range.rb', line 8

def initialize(*args, **kargs, &)
  if args.first.is_a?(Range)
    @code_left = args.first.code_left
    @code_right = args.first.code_right
    @code_options = args.first.code_options
    @code_exclude_end = args.first.code_exclude_end
  else
    @code_left =
      (args.first.to_code.nothing? ? Integer.new(0) : args.first.to_code)

    @code_right =
      if args.second.to_code.nothing?
        Integer.new(0)
      else
        args.second.to_code
      end

    @code_options = Dictionary.new(args.third.presence || kargs)
    @code_exclude_end = Boolean.new(code_options.code_get(:exclude_end))
  end

  @raw = ::Range.new(code_left, code_right, exclude_end?)
end

Instance Attribute Details

#code_exclude_endObject (readonly)

Returns the value of attribute code_exclude_end.



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

def code_exclude_end
  @code_exclude_end
end

#code_leftObject (readonly)

Returns the value of attribute code_left.



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

def code_left
  @code_left
end

#code_optionsObject (readonly)

Returns the value of attribute code_options.



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

def code_options
  @code_options
end

#code_rightObject (readonly)

Returns the value of attribute code_right.



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

def code_right
  @code_right
end

Instance Method Details

#call(**args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/code/object/range.rb', line 32

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  globals = multi_fetch(args, *GLOBALS)
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "all?"
    sig(args) { Function }
    code_all?(code_value, **globals)
  when "any?"
    sig(args) { Function }
    code_any?(code_value, **globals)
  when "each"
    sig(args) { Function }
    code_each(code_value, **globals)
  when "first"
    sig(args)
    code_first
  when "last"
    sig(args)
    code_last
  when "map"
    sig(args) { Function }
    code_map(code_value, **globals)
  when "select"
    sig(args) { Function }
    code_select(code_value, **globals)
  when "step"
    sig(args) { Integer | Decimal }
    code_step(code_value)
  when "to_list"
    sig(args)
    code_to_list
  else
    super
  end
end

#code_all?(argument, **globals) ⇒ Boolean

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/code/object/range.rb', line 71

def code_all?(argument, **globals)
  code_argument = argument.to_code

  index = 0

  Boolean.new(
    raw.all? do |code_element|
      code_argument
        .call(
          arguments: List.new([code_element, Integer.new(index), self]),
          **globals
        )
        .truthy?
        .tap { index += 1 }
    end
  )
end

#code_any?(argument, **globals) ⇒ Boolean

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/code/object/range.rb', line 89

def code_any?(argument, **globals)
  code_argument = argument.to_code

  index = 0

  Boolean.new(
    raw.any? do |code_element|
      code_argument
        .call(
          arguments: List.new([code_element, Integer.new(index), self]),
          **globals
        )
        .truthy?
        .tap { index += 1 }
    end
  )
end

#code_each(argument, **globals) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/code/object/range.rb', line 107

def code_each(argument, **globals)
  code_argument = argument.to_code

  raw.each.with_index do |code_element, index|
    code_argument.call(
      arguments: List.new([code_element, Integer.new(index), self]),
      **globals
    )
  end

  self
end

#code_firstObject



120
121
122
# File 'lib/code/object/range.rb', line 120

def code_first
  raw.first || Nothing.new
end

#code_lastObject



124
125
126
# File 'lib/code/object/range.rb', line 124

def code_last
  raw.last || Nothing.new
end

#code_map(argument, **globals) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/code/object/range.rb', line 128

def code_map(argument, **globals)
  code_argument = argument.to_code

  List.new(
    raw.map.with_index do |code_element, index|
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      )
    end
  )
end

#code_select(argument, **globals) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/code/object/range.rb', line 141

def code_select(argument, **globals)
  code_argument = argument.to_code

  List.new(
    raw.select.with_index do |code_element, index|
      code_argument.call(
        arguments: List.new([code_element, Integer.new(index), self]),
        **globals
      ).truthy?
    end
  )
end

#code_step(argument) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/code/object/range.rb', line 158

def code_step(argument)
  code_argument = argument.to_code

  code_list = List.new
  code_element = code_left
  code_list.code_append(code_element)

  code_element = code_element.code_plus(code_argument)

  if exclude_end?
    while code_element.code_inferior(code_right).truthy?
      code_list.code_append(code_element)
      code_element = code_element.code_plus(code_argument)
    end
  else
    while code_element.code_inferior_or_equal(code_right).truthy?
      code_list.code_append(code_element)
      code_element = code_element.code_plus(code_argument)
    end
  end

  code_list
end

#code_to_listObject



182
183
184
# File 'lib/code/object/range.rb', line 182

def code_to_list
  List.new(raw.to_a)
end

#exclude_end?Boolean

Returns:



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

def exclude_end?
  code_exclude_end.truthy?
end