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 =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Filter.



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

def initialize(jar, options = {})
  @jar = jar
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/filters/bmc/filter.rb', line 29

def method_missing(method, *args)
  if method.to_s.end_with?("=")
    key    = method.to_s[0..-2]
    value  = args.first
    action = :write
  else
    key    = method.to_s
    action = :read
  end

  if strategies.key?(key) && action == :read
    get(key)
  elsif strategies.key?(key) && action == :write
    set(key, value)
  else
    super
  end
end

Instance Attribute Details

#jarObject (readonly)

Returns the value of attribute jar.



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

def jar
  @jar
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#actives_countObject



66
67
68
# File 'app/filters/bmc/filter.rb', line 66

def actives_count
  read.count { |k, v| strategies.key?(k.to_s) && v.present? }
end

#any?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/filters/bmc/filter.rb', line 70

def any?
  actives_count.positive?
end

#apply(query) ⇒ Object



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

def apply(query)
  strategies.each do |key, strategy|
    value = get(key)

    next if value.blank?

    query = strategy.apply(query, value)
  end

  return query
end

#empty?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/filters/bmc/filter.rb', line 74

def empty?
  !any?
end

#merge(new_filters) ⇒ Object



62
63
64
# File 'app/filters/bmc/filter.rb', line 62

def merge(new_filters)
  write read.merge(new_filters)
end

#readObject



52
53
54
55
56
# File 'app/filters/bmc/filter.rb', line 52

def read
  JSON.parse jar["filters"].to_s
rescue JSON::ParserError
  {}
end

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

Returns:

  • (Boolean)


48
49
50
# File 'app/filters/bmc/filter.rb', line 48

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

#strategiesObject



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

def strategies
  self.class::STRATEGIES.with_indifferent_access
end

#write(filters) ⇒ Object



58
59
60
# File 'app/filters/bmc/filter.rb', line 58

def write(filters)
  jar["filters"] = filters.to_json
end