Class: Stretchy::Types::Range
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(opts_or_range = {}, options = {}) ⇒ Range
constructor
A new instance of Range.
- #to_search ⇒ Object
Methods included from Utils::Validation
#errors, included, #require_one!, #require_only_one!, #valid?, #validate!, #validator
Constructor Details
#initialize(opts_or_range = {}, options = {}) ⇒ Range
Returns a new instance of Range.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/stretchy/types/range.rb', line 17 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]) @inverse = !![:inverse] @should = !![:should] 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]) @inverse = !!opts[:inverse] @should = !!opts[:should] when Range @min = opts_or_range.min @max = opts_or_range.max @exclusive_min = opts_or_range.exclusive_min @exclusive_max = opts_or_range.exclusive_max else raise Stretchy::Errors::ContractError.new("Ranges must be a range or a hash - found #{.class.name}") end require_one! :min, :max validate! end |
Instance Method Details
#empty? ⇒ Boolean
48 49 50 |
# File 'lib/stretchy/types/range.rb', line 48 def empty? !(@min || @max) end |
#to_search ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/stretchy/types/range.rb', line 52 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 |