Class: Agilibox::SmallData::Filter

Inherits:
Object
  • Object
show all
Defined in:
app/filters/agilibox/small_data/filter.rb

Constant Summary collapse

STRATEGIES =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jar) ⇒ Filter

Returns a new instance of Filter.



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

def initialize(jar)
  @jar = jar
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/filters/agilibox/small_data/filter.rb', line 26

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.



4
5
6
# File 'app/filters/agilibox/small_data/filter.rb', line 4

def jar
  @jar
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/filters/agilibox/small_data/filter.rb', line 63

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

#apply(query) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'app/filters/agilibox/small_data/filter.rb', line 14

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)


67
68
69
# File 'app/filters/agilibox/small_data/filter.rb', line 67

def empty?
  !any?
end

#merge(new_filters) ⇒ Object



59
60
61
# File 'app/filters/agilibox/small_data/filter.rb', line 59

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

#readObject



49
50
51
52
53
# File 'app/filters/agilibox/small_data/filter.rb', line 49

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

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/filters/agilibox/small_data/filter.rb', line 45

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

#strategiesObject



10
11
12
# File 'app/filters/agilibox/small_data/filter.rb', line 10

def strategies
  self.class::STRATEGIES.with_indifferent_access
end

#write(filters) ⇒ Object



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

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