Class: Dorsale::SmallData::Filter

Inherits:
Object
  • Object
show all
Defined in:
app/filters/dorsale/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/dorsale/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/dorsale/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/dorsale/small_data/filter.rb', line 4

def jar
  @jar
end

Instance Method Details

#apply(query) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'app/filters/dorsale/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

#merge(new_filters) ⇒ Object



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

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

#readObject



45
46
47
48
49
# File 'app/filters/dorsale/small_data/filter.rb', line 45

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

#strategiesObject



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

def strategies
  self.class::STRATEGIES
end

#write(filters) ⇒ Object



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

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