module JsonApiable::FilterMatchers

Public Class Methods

matches?(allowed, given) click to toggle source
# 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

Public Instance Methods

any_non_blank_matcher() click to toggle source
# 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() click to toggle source

returns true for boolean values, false for any other

# 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() click to toggle source

returns true if the value is a valid date or datetime

# 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) { || ... } click to toggle source
# File lib/json_apiable/filter_matchers.rb, line 61
def handle_error(value)
  yield
rescue ArgumentError => e
  raise ArgumentError, "#{value}: #{e.message}"
end
ids_matcher(model) click to toggle source

returns true if the value is a an array of existing ids of the given model

# 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() click to toggle source

returns true for true values, false for any other

# 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

Private Instance Methods

matches?(allowed, given) click to toggle source
# 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