module Quiver::Action::FilterValue

Constants

EQUALITIES
INCLUSIONS
INEQUALITIES
PRESENCE

Attributes

errors[RW]
filter[RW]

Public Class Methods

extra_supported_comparisons() click to toggle source
# File lib/quiver/action/filter_value.rb, line 19
def self.extra_supported_comparisons
  @extra_supported_comparisons
end
klasses() click to toggle source
# File lib/quiver/action/filter_value.rb, line 69
def self.klasses
  @klasses ||= {}
end
new(filter) click to toggle source
Calls superclass method
# File lib/quiver/action/filter_value.rb, line 75
def initialize(filter)
  self.errors = Quiver::ErrorCollection.new

  filter = filter.to_h if filter.is_a?(Lotus::Utils::Hash)
  self.filter = filter

  if filter.is_a?(Hash)
    keys = filter.keys
    keys.each do |key|
      filter[key.sub('~', 'not_')] = filter.delete(key)
    end

    keys.each do |key|
      if INCLUSIONS.include?(key) && !filter[key].is_a?(Array)
        errors << FilterError.new("'#{key}' must map to an Array")
        filter[key] = []
      end
    end

    (filter.keys - supported_comparisons).each do |key|
      errors << FilterError.new("'#{key}' is not supported")
      filter.delete(key)
    end

    filter
  else
    filter = {}
    errors << FilterError.new('filters must be a Hash')
  end

  super

  validate
end
supported_comparisons() click to toggle source
# File lib/quiver/action/filter_value.rb, line 15
def self.supported_comparisons
  @supported_comparisons
end
with(type, *args, extras:[]) click to toggle source
# File lib/quiver/action/filter_value.rb, line 9
def self.with(type, *args, extras:[])
  set = Set.new(args + extras)
  klasses[[set, type]] ||= Class.new do
    include Extant::Attributes
    include FilterValue

    def self.supported_comparisons
      @supported_comparisons
    end

    def self.extra_supported_comparisons
      @extra_supported_comparisons
    end
  end.tap do |klass|
    klass.instance_variable_set(
      '@supported_comparisons',
      set
    )

    klass.instance_variable_set(
      '@extra_supported_comparisons',
      extras
    )

    extras.each do |extra|
      klass.send(:attribute, extra, type)
    end

    if set.include?(:presence)
      klass.send(:attribute, :nil, String)
      klass.send(:attribute, :not_nil, String)
    end

    if set.include?(:equalities)
      klass.send(:attribute, :eq, type)
      klass.send(:attribute, :not_eq, type)
    end

    if set.include?(:inclusions)
      klass.send(:attribute, :in, Array[type])
      klass.send(:attribute, :not_in, Array[type])
    end

    if set.include?(:inequalities)
      klass.send(:attribute, :gt, type)
      klass.send(:attribute, :not_gt, type)
      klass.send(:attribute, :gte, type)
      klass.send(:attribute, :not_gte, type)
      klass.send(:attribute, :lt, type)
      klass.send(:attribute, :not_lt, type)
      klass.send(:attribute, :lte, type)
      klass.send(:attribute, :not_lte, type)
    end
  end
end
with_all(type, extras:[]) click to toggle source
# File lib/quiver/action/filter_value.rb, line 65
def self.with_all(type, extras:[])
  with(type, :presence, :equalities, :inequalities, :inclusions)
end

Public Instance Methods

filter_attributes() click to toggle source
# File lib/quiver/action/filter_value.rb, line 110
def filter_attributes
  attributes.slice(*filter.keys.map(&:to_sym))
end
valid?() click to toggle source
# File lib/quiver/action/filter_value.rb, line 114
def valid?
  !errors.any?
end

Private Instance Methods

supported_comparisons() click to toggle source
# File lib/quiver/action/filter_value.rb, line 136
def supported_comparisons
  unless @supported_comparisons
    @supported_comparisons = []
    @supported_comparisons += PRESENCE if self.class.supported_comparisons.include?(:presence)
    @supported_comparisons += EQUALITIES if self.class.supported_comparisons.include?(:equalities)
    @supported_comparisons += INEQUALITIES if self.class.supported_comparisons.include?(:inequalities)
    @supported_comparisons += INCLUSIONS if self.class.supported_comparisons.include?(:inclusions)
    @supported_comparisons += self.class.extra_supported_comparisons.map(&:to_s)
  end

  @supported_comparisons
end
validate() click to toggle source
# File lib/quiver/action/filter_value.rb, line 123
def validate
  extant_attributes.each do |key, attr_object|
    if attr_object.set? && !attr_object.coerced?
      case
      when EQUALITIES.include?(attr_object.name.to_s) || INEQUALITIES.include?(attr_object.name.to_s) || PRESENCE.include?(attr_object.name.to_s)
        errors << FilterError.new("'#{attr_object.name}' must not map to Hashes or Arrays")
      when INCLUSIONS.include?(attr_object.name.to_s)
        errors << FilterError.new("'#{attr_object.name}' must map to an Array")
      end
    end
  end
end