Class: Luoma::RangeDrop
- Inherits:
-
Drop
- Object
- Drop
- Luoma::RangeDrop
show all
- Defined in:
- lib/luoma/drops/range.rb,
sig/luoma/drops/range.rbs
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Drop
#contains?, #gt?, #lt?, #render, #to_s
Constructor Details
#initialize(start, stop) ⇒ RangeDrop
Returns a new instance of RangeDrop.
7
8
9
10
11
12
|
# File 'lib/luoma/drops/range.rb', line 7
def initialize(start, stop)
super()
@start = start
@stop = stop
@range = (start..stop)
end
|
Instance Attribute Details
#start ⇒ Integer
Returns the value of attribute start.
5
6
7
|
# File 'lib/luoma/drops/range.rb', line 5
def start
@start
end
|
#stop ⇒ Integer
Returns the value of attribute stop.
5
6
7
|
# File 'lib/luoma/drops/range.rb', line 5
def stop
@stop
end
|
Instance Method Details
#each ⇒ Object
37
38
39
|
# File 'lib/luoma/drops/range.rb', line 37
def each(&)
@range.each
end
|
#eq?(obj, context) ⇒ Boolean
42
43
44
|
# File 'lib/luoma/drops/range.rb', line 42
def eq?(obj, context)
obj.is_a?(RangeDrop) && obj.start == obj.stop
end
|
#fetch(name, context, default: :nothing) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/luoma/drops/range.rb', line 20
def fetch(name, context, default: :nothing)
case name
when "first"
@start
when "last"
@stop
when "size"
@range.size
else
default
end
end
|
#key?(obj, context) ⇒ Boolean
15
16
17
|
# File 'lib/luoma/drops/range.rb', line 15
def key?(obj, context)
obj == "first" || obj == "last" || obj == "size"
end
|
#length(context) ⇒ Object
47
48
49
|
# File 'lib/luoma/drops/range.rb', line 47
def length(context) @range.size
end
|
#slice(start, stop, step) ⇒ Object
51
52
53
|
# File 'lib/luoma/drops/range.rb', line 51
def slice(start, stop, step)
@range.to_a[(start...stop).step(step)] end
|
#to_a ⇒ Object
33
34
35
|
# File 'lib/luoma/drops/range.rb', line 33
def to_a
@range.to_a
end
|
#to_primitive(hint, context) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/luoma/drops/range.rb', line 56
def to_primitive(hint, context)
case hint
when :data
@range.to_a
when :numeric
:nothing
when :string
JSON.generate(@range.to_a)
when :boolean
false
end
end
|