Module: Hario::Filterable

Defined in:
lib/hario.rb

Constant Summary collapse

HARIO_APPLY_TYPES =
%w( filters pluck ).map(&:to_sym)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hario_attributes_listObject (readonly)

Returns the value of attribute hario_attributes_list.



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

def hario_attributes_list
  @hario_attributes_list
end

Instance Method Details

#apply_filters(filters) ⇒ Object



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

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

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

#apply_pluck(pluck) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/hario.rb', line 27

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

#hario_attributes(types, only: nil, except: nil) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/hario.rb', line 43

def hario_attributes(types, only: nil, except: nil)
  @hario_attributes_list ||= {}
  Array.wrap(types).each do |t|
    raise_if_not_hario_type!(t)
    @hario_attributes_list[t] =
      { only: Array.wrap(only), except: Array.wrap(except) }
  end
end

#hash_pluck(*keys) ⇒ Object



39
40
41
# File 'lib/hario.rb', line 39

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

#remove_local_table_prefix(results) ⇒ Object



35
36
37
# File 'lib/hario.rb', line 35

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

#search(filters, pluck = []) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/hario.rb', line 11

def search(filters, pluck = [])
  pluck ||= []
  pluck = pluck.reject{ |p| p.nil? || p.empty? }

  s = all
  s = s.apply_filters(filters) if filters
  s = s.apply_pluck(pluck) if pluck.any?
  s
end