Class: Luoma::RangeDrop

Inherits:
Drop
  • Object
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.

Parameters:

  • start (Integer)
  • stop (Integer)


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

#startInteger (readonly)

Returns the value of attribute start.

Returns:

  • (Integer)


5
6
7
# File 'lib/luoma/drops/range.rb', line 5

def start
  @start
end

#stopInteger (readonly)

Returns the value of attribute stop.

Returns:

  • (Integer)


5
6
7
# File 'lib/luoma/drops/range.rb', line 5

def stop
  @stop
end

Instance Method Details

#eachObject



37
38
39
# File 'lib/luoma/drops/range.rb', line 37

def each(&)
  @range.each
end

#eq?(obj, context) ⇒ Boolean

Signature:

  • (untyped, RenderContext) -> bool

Returns:

  • (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

Signature:

  • (String, RenderContext) -> untyped



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

Signature:

  • (untyped, RenderContext) -> bool

Returns:

  • (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

Signature:

  • (RenderContext) -> Integer



47
48
49
# File 'lib/luoma/drops/range.rb', line 47

def length(context) # steep:ignore
  @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)] # steep:ignore
end

#to_aObject



33
34
35
# File 'lib/luoma/drops/range.rb', line 33

def to_a
  @range.to_a
end

#to_primitive(hint, context) ⇒ Object

Signature:

  • (:data | :numeric | :string | :boolean, RenderContext) -> untyped



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