Class: Code::Object::Range

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

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_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
# File 'lib/code/object/range.rb', line 8

def initialize(*args, **_kargs, &)
  @left = args.first.presence || Integer.new(0)
  @right = args.second.presence || Integer.new(0)
  @options = Dictionary.new(args.third.presence || {})
  @exclude_end = Boolean.new(options.code_get(String.new(:exclude_end)))
  @raw = ::Range.new(left, right, exclude_end?)
end

Instance Attribute Details

#exclude_endObject (readonly)

Returns the value of attribute exclude_end.



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

def exclude_end
  @exclude_end
end

#leftObject (readonly)

Returns the value of attribute left.



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

def left
  @left
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#rightObject (readonly)

Returns the value of attribute right.



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

def right
  @right
end

Instance Method Details

#call(**args) ⇒ Object



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
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/code/object/range.rb', line 16

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 "all?"
    sig(args) { Function }
    code_all?(value, **globals)
  when "any?"
    sig(args) { Function }
    code_any?(value, **globals)
  when "each"
    sig(args) { Function }
    code_each(value, **globals)
  when "first"
    sig(args)
    code_first
  when "last"
    sig(args)
    code_last
  when "map"
    sig(args) { Function }
    code_map(value, **globals)
  when "select"
    sig(args) { Function }
    code_select(value, **globals)
  when "step"
    sig(args) { Integer | Decimal }
    code_step(value)
  when "to_list"
    sig(args)
    code_to_list
  else
    super
  end
end

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

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/code/object/range.rb', line 55

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

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

Returns:



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

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

#code_each(argument, **globals) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/code/object/range.rb', line 85

def code_each(argument, **globals)
  raw.each.with_index do |element, index|
    argument.call(
      arguments: List.new([element, Integer.new(index), self]),
      **globals
    )
  end
  self
end

#code_firstObject



95
96
97
# File 'lib/code/object/range.rb', line 95

def code_first
  raw.first || Nothing.new
end

#code_lastObject



99
100
101
# File 'lib/code/object/range.rb', line 99

def code_last
  raw.last || Nothing.new
end

#code_map(argument, **globals) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/code/object/range.rb', line 103

def code_map(argument, **globals)
  List.new(
    raw.map.with_index do |element, index|
      argument.call(
        arguments: List.new([element, Integer.new(index), self]),
        **globals
      )
    end
  )
end

#code_select(argument, **globals) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/code/object/range.rb', line 114

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

#code_step(argument) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/code/object/range.rb', line 129

def code_step(argument)
  list = List.new
  element = left
  list.code_append(element)

  element = element.code_plus(argument)
  if exclude_end?
    while element.code_inferior(right).truthy?
      list.code_append(element)
      element = element.code_plus(argument)
    end
  else
    while element.code_inferior_or_equal(right).truthy?
      list.code_append(element)
      element = element.code_plus(argument)
    end
  end

  list
end

#code_to_listObject



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

def code_to_list
  List.new(raw.to_a)
end

#exclude_end?Boolean

Returns:



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

def exclude_end?
  exclude_end.truthy?
end