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) ⇒ Filter

Returns a new instance of Filter.



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

def initialize(jar)
  @jar = jar
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

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

Instance Method Details

#actives_countObject



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

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

#any?Boolean

Returns:

  • (Boolean)


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

def any?
  actives_count.positive?
end

#apply(query) ⇒ Object



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

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)


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

def empty?
  !any?
end

#merge(new_filters) ⇒ Object



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

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

#readObject



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

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

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

Returns:

  • (Boolean)


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

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

#strategiesObject



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

def strategies
  self.class::STRATEGIES.with_indifferent_access
end

#write(filters) ⇒ Object



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

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