Module: JsonApiable::FilterMatchers
- Included in:
- BaseFilter
- Defined in:
- lib/json_apiable/filter_matchers.rb
Class Method Summary collapse
Instance Method Summary collapse
- #any_non_blank_matcher ⇒ Object
-
#boolean_matcher ⇒ Object
returns true for boolean values, false for any other.
-
#datetime_matcher ⇒ Object
returns true if the value is a valid date or datetime.
- #handle_error(value) ⇒ Object
-
#ids_matcher(model) ⇒ Object
returns true if the value is a an array of existing ids of the given model.
-
#true_matcher ⇒ Object
returns true for true values, false for any other.
Class Method Details
.matches?(allowed, given) ⇒ Boolean
5 6 7 8 9 |
# File 'lib/json_apiable/filter_matchers.rb', line 5 def matches?(allowed, given) given.all? do |value| allowed.is_a?(Proc) ? allowed.call(value) : allowed.include?(value) end end |
Instance Method Details
#any_non_blank_matcher ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/json_apiable/filter_matchers.rb', line 13 def any_non_blank_matcher proc do |value| handle_error(value) do value.present? end end end |
#boolean_matcher ⇒ Object
returns true for boolean values, false for any other
22 23 24 25 26 27 28 |
# File 'lib/json_apiable/filter_matchers.rb', line 22 def boolean_matcher proc do |value| handle_error(value) do true_matcher.call(value) || (value == false || value =~ /^(false|f|0)$/i) end end end |
#datetime_matcher ⇒ Object
returns true if the value is a valid date or datetime
40 41 42 43 44 45 46 47 |
# File 'lib/json_apiable/filter_matchers.rb', line 40 def datetime_matcher proc do |value| handle_error(value) do datetime = value.in_time_zone(Time.zone) datetime.present? end end end |
#handle_error(value) ⇒ Object
61 62 63 64 65 |
# File 'lib/json_apiable/filter_matchers.rb', line 61 def handle_error(value) yield rescue ArgumentError => e raise ArgumentError, "#{value}: #{e.}" end |
#ids_matcher(model) ⇒ Object
returns true if the value is a an array of existing ids of the given model
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/json_apiable/filter_matchers.rb', line 50 def ids_matcher(model) proc do |value| handle_error(value) do given_ids = value.split(',') found_records = model.where(id: given_ids) given_ids.count == found_records.count end end end |
#true_matcher ⇒ Object
returns true for true values, false for any other
31 32 33 34 35 36 37 |
# File 'lib/json_apiable/filter_matchers.rb', line 31 def true_matcher proc do |value| handle_error(value) do value == true || value =~ /^(true|t|1)$/i end end end |