Class: Stretchy::Types::Range
Constant Summary
Constants included from Utils::Contract
Utils::Contract::ASSERTIONS, Utils::Contract::DECAY_FUNCTIONS, Utils::Contract::DISTANCE_FORMAT
Instance Attribute Summary collapse
-
#exclusive_max ⇒ Object
readonly
Returns the value of attribute exclusive_max.
-
#exclusive_min ⇒ Object
readonly
Returns the value of attribute exclusive_min.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Instance Method Summary collapse
-
#initialize(opts_or_range = {}, options = {}) ⇒ Range
constructor
A new instance of Range.
- #to_search ⇒ Object
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 = {}, = {}) case opts_or_range when ::Range @min = opts_or_range.min @max = opts_or_range.max @exclusive_min = !!([:exclusive_min] || [:exclusive]) @exclusive_max = !!([:exclusive_max] || [:exclusive]) when ::Hash opts = .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 #{.class.name}") end require_one min: @min, max: @max validate! end |
Instance Attribute Details
#exclusive_max ⇒ Object (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_min ⇒ Object (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 |
#max ⇒ Object (readonly)
Returns the value of attribute max.
5 6 7 |
# File 'lib/stretchy/types/range.rb', line 5 def max @max end |
#min ⇒ Object (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_search ⇒ Object
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 |