Class: Stretchy::Types::Range

Inherits:
Base
  • Object
show all
Defined in:
lib/stretchy/types/range.rb

Constant Summary

Constants included from Utils::Contract

Utils::Contract::ASSERTIONS, Utils::Contract::DECAY_FUNCTIONS, Utils::Contract::DISTANCE_FORMAT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Contract

included, #require_one, #validate!

Constructor Details

#initialize(opts_or_range = {}, options = {}) ⇒ Range

Returns a new instance of Range.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stretchy/types/range.rb', line 12

def initialize(opts_or_range = {}, options = {})

  case opts_or_range
  when ::Range
    @min = opts_or_range.min
    @max = opts_or_range.max
    @exclusive_min = !!(options[:exclusive_min] || options[:exclusive])
    @exclusive_max = !!(options[:exclusive_max] || options[:exclusive])
  when ::Hash
    opts = options.merge(opts_or_range)
    @min = opts[:min]
    @max = opts[:max]
    @exclusive_min = !!(opts[:exclusive_min] || opts[:exclusive])
    @exclusive_max = !!(opts[:exclusive_max] || opts[:exclusive])
  else
    raise Stretchy::Errors::ContractError.new("Ranges must be a range or a hash - found #{options.class.name}")
  end

  require_one min: @min, max: @max
  validate!
end

Instance Attribute Details

#exclusive_maxObject (readonly)

Returns the value of attribute exclusive_max.



5
6
7
# File 'lib/stretchy/types/range.rb', line 5

def exclusive_max
  @exclusive_max
end

#exclusive_minObject (readonly)

Returns the value of attribute exclusive_min.



5
6
7
# File 'lib/stretchy/types/range.rb', line 5

def exclusive_min
  @exclusive_min
end

#maxObject (readonly)

Returns the value of attribute max.



5
6
7
# File 'lib/stretchy/types/range.rb', line 5

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



5
6
7
# File 'lib/stretchy/types/range.rb', line 5

def min
  @min
end

Instance Method Details

#to_searchObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stretchy/types/range.rb', line 34

def to_search
  json = {}
  if @exclusive_min && @min
    json[:gt] = @min
  elsif @min
    json[:gte] = @min
  end

  if @exclusive_max && @max
    json[:lt] = @max
  elsif @max
    json[:lte] = @max
  end
  json
end