Module: Hario::Filterable

Defined in:
lib/hario.rb

Instance Method Summary collapse

Instance Method Details

#apply_filters(filters) ⇒ Object



14
15
16
17
18
# File 'lib/hario.rb', line 14

def apply_filters(filters)
  fp = FilterParser.new(filters, self)

  fp.where_clauses.reduce(joins(fp.join_clause), &:where)
end

#apply_pluck(pluck) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/hario.rb', line 20

def apply_pluck(pluck)
  pp = PluckParser.new(pluck, self)

  results = joins(pp.join_clause).hash_pluck(*pp.pluck_clause)

  remove_local_table_prefix(results)
end

#hash_pluck(*keys) ⇒ Object



32
33
34
# File 'lib/hario.rb', line 32

def hash_pluck(*keys)
  pluck(*keys).map{ |vals| Hash[keys.zip(Array(vals))] }
end

#remove_local_table_prefix(results) ⇒ Object



28
29
30
# File 'lib/hario.rb', line 28

def remove_local_table_prefix(results)
  results.map{ |r| r.transform_keys!{ |k| k.gsub(/^#{table_name}\./, '') } }
end

#search(filters, pluck = nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/hario.rb', line 7

def search(filters, pluck = nil)
  s = all
  s = s.apply_filters(filters) if filters
  s = s.apply_pluck(pluck) if pluck
  s
end