Module: Toller::Filters::Mutators::Integer

Defined in:
lib/toller/filters/mutators/integer.rb

Overview

Integer filter mutator

Class Method Summary collapse

Class Method Details

.call(value) ⇒ String, Range

Coerces a raw filter param into an integer value or range.

Parameters:

  • value (String)

    the raw filter param value

Returns:

  • (String, Range)

    the original value, or a Range when the value contains range syntax (.. or ...)



18
19
20
21
22
# File 'lib/toller/filters/mutators/integer.rb', line 18

def call(value)
  return value unless range?(value)

  range(value)
end

.inclusive_or_exclusive_range(value) ⇒ String?

Detects whether value contains inclusive or exclusive range syntax.

Parameters:

  • value (String)

    the raw filter param value

Returns:

  • (String, nil)

    "..." for an exclusive range, ".." for an inclusive range, or nil if value is not a range



50
51
52
53
54
55
# File 'lib/toller/filters/mutators/integer.rb', line 50

def inclusive_or_exclusive_range(value)
  return "..." if value.include?("...")
  return ".." if value.include?("..")

  nil
end

.range(value) ⇒ Range

Builds a Range by splitting value on its range dots.

Parameters:

  • value (String)

    the raw range string, e.g. "1..10"

Returns:

  • (Range)

    the resulting range



40
41
42
# File 'lib/toller/filters/mutators/integer.rb', line 40

def range(value)
  Range.new(*value.split(inclusive_or_exclusive_range(value)))
end

.range?(value) ⇒ Boolean

Checks whether value contains range syntax.

Parameters:

  • value (String)

    the raw filter param value

Returns:

  • (Boolean)

    true if value contains .. or ...



29
30
31
32
33
# File 'lib/toller/filters/mutators/integer.rb', line 29

def range?(value)
  range_dots = inclusive_or_exclusive_range(value)

  range_dots.present?
end