Module: Quiver::Action::FilterValue

Defined in:
lib/quiver/action/filter_value.rb

Constant Summary collapse

PRESENCE =
%w|nil not_nil|.freeze
EQUALITIES =
%w|eq not_eq|.freeze
INCLUSIONS =
%w|in not_in|.freeze
INEQUALITIES =
%w|gt lt gte lte not_gt not_lt not_gte not_lte|.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



73
74
75
# File 'lib/quiver/action/filter_value.rb', line 73

def errors
  @errors
end

Class Method Details

.klassesObject



69
70
71
# File 'lib/quiver/action/filter_value.rb', line 69

def self.klasses
  @klasses ||= {}
end

.with(type, *args, extras: []) ⇒ Object



9
10
11
12
13
14
15
16
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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: []) ⇒ Object



65
66
67
# File 'lib/quiver/action/filter_value.rb', line 65

def self.with_all(type, extras:[])
  with(type, :presence, :equalities, :inequalities, :inclusions)
end

Instance Method Details

#filter_attributesObject



110
111
112
# File 'lib/quiver/action/filter_value.rb', line 110

def filter_attributes
  attributes.slice(*filter.keys.map(&:to_sym))
end

#initialize(filter) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#valid?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/quiver/action/filter_value.rb', line 114

def valid?
  !errors.any?
end