Class: BMC::Filter

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/filters/bmc/filter.rb

Defined Under Namespace

Classes: ByDate, ByDateBegin, ByDateEnd, ByDateOrDatetimePeriod, ByDatePeriod, ByDatetimePeriod, ByKeyValue, ByKeyValues

Constant Summary collapse

STRATEGIES =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, options = {}) ⇒ Filter

Returns a new instance of Filter.



8
9
10
11
# File 'app/filters/bmc/filter.rb', line 8

def initialize(values, options = {})
  @values = values.with_indifferent_access
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



29
30
31
32
33
34
35
# File 'app/filters/bmc/filter.rb', line 29

def method_missing(method, ...)
  if strategies.key?(method)
    values[method]
  else
    super
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'app/filters/bmc/filter.rb', line 6

def options
  @options
end

#valuesObject (readonly)

Returns the value of attribute values.



6
7
8
# File 'app/filters/bmc/filter.rb', line 6

def values
  @values
end

Instance Method Details

#actives_countObject



41
42
43
44
45
46
47
48
49
# File 'app/filters/bmc/filter.rb', line 41

def actives_count
  values.count do |k, v|
    next false unless strategies.key?(k)
    next false if v.blank?
    next false if v.respond_to?(:compact_blank) && v.compact_blank.none?

    true
  end
end

#any?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/filters/bmc/filter.rb', line 51

def any?
  actives_count.positive?
end

#call(query) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'app/filters/bmc/filter.rb', line 17

def call(query)
  strategies.each do |key, strategy|
    value = values[key]

    next if value.blank?

    query = strategy.call(query, value)
  end

  query
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/filters/bmc/filter.rb', line 55

def empty?
  !any?
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/filters/bmc/filter.rb', line 37

def respond_to_missing?(method, include_private = false)
  strategies.key?(method) || super
end

#strategiesObject



13
14
15
# File 'app/filters/bmc/filter.rb', line 13

def strategies
  self.class::STRATEGIES.with_indifferent_access
end