Class: Code::Object::Range

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #code_and_operator, #code_different, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_inclusive_range, #code_or_operator, #code_self, #code_to_string, #falsy?, #hash, maybe, #multi_fetch, repeat, #sig, #truthy?, |

Constructor Details

#initialize(left, right, exclude_end: false) ⇒ Range

Returns a new instance of Range.



8
9
10
11
12
13
# File 'lib/code/object/range.rb', line 8

def initialize(left, right, exclude_end: false)
  @left = left
  @right = right
  @exclude_end = 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

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

#rightObject (readonly)

Returns the value of attribute right.



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

def right
  @right
end

Class Method Details

.nameObject



15
16
17
# File 'lib/code/object/range.rb', line 15

def self.name
  "Range"
end

Instance Method Details

#call(**args) ⇒ Object



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
54
55
56
# File 'lib/code/object/range.rb', line 19

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 "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) { Number }
    code_step(value)
  when "to_list"
    sig(args)
    code_to_list
  else
    super
  end
end

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

Returns:



58
59
60
61
62
63
64
# File 'lib/code/object/range.rb', line 58

def code_all?(argument, **globals)
  Boolean.new(
    raw.all? do |element|
      argument.call(arguments: [Argument.new(element)], **globals).truthy?
    end
  )
end

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

Returns:



66
67
68
69
70
71
72
# File 'lib/code/object/range.rb', line 66

def code_any?(argument, **globals)
  Boolean.new(
    raw.any? do |element|
      argument.call(arguments: [Argument.new(element)], **globals).truthy?
    end
  )
end

#code_each(argument, **globals) ⇒ Object



74
75
76
77
78
79
# File 'lib/code/object/range.rb', line 74

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

#code_firstObject



81
82
83
# File 'lib/code/object/range.rb', line 81

def code_first
  raw.first || Nothing.new
end

#code_lastObject



85
86
87
# File 'lib/code/object/range.rb', line 85

def code_last
  raw.last || Nothing.new
end

#code_map(argument, **globals) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/code/object/range.rb', line 89

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

#code_select(argument, **globals) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/code/object/range.rb', line 97

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

#code_step(argument) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/code/object/range.rb', line 105

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



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

def code_to_list
  List.new(raw.to_a)
end

#exclude_end?Boolean

Returns:



130
131
132
# File 'lib/code/object/range.rb', line 130

def exclude_end?
  !!exclude_end
end

#inspectObject



134
135
136
# File 'lib/code/object/range.rb', line 134

def inspect
  to_s
end

#to_sObject



138
139
140
# File 'lib/code/object/range.rb', line 138

def to_s
  raw.to_s
end