Class: QueryHelper::SqlFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/query_helper/sql_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_values: [], column_maps: []) ⇒ SqlFilter

Returns a new instance of SqlFilter.



8
9
10
11
# File 'lib/query_helper/sql_filter.rb', line 8

def initialize(filter_values: [], column_maps: [])
  @column_maps = column_maps
  @filter_values = filter_values
end

Instance Attribute Details

#column_mapsObject

Returns the value of attribute column_maps.



6
7
8
# File 'lib/query_helper/sql_filter.rb', line 6

def column_maps
  @column_maps
end

#filter_valuesObject

Returns the value of attribute filter_values.



6
7
8
# File 'lib/query_helper/sql_filter.rb', line 6

def filter_values
  @filter_values
end

Instance Method Details

#bind_variablesObject



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

def bind_variables
  Hash[@filters.collect { |f| [f.bind_variable, f.criterion] }]
end

#create_filtersObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/query_helper/sql_filter.rb', line 13

def create_filters
  @filters = []

  @filter_values.each do |comparate_alias, criteria|
    # Find the sql mapping if it exists
    map = @column_maps.find { |m| m.alias_name == comparate_alias }
    raise InvalidQueryError.new("cannot filter by #{comparate_alias}") unless map

    # create the filter
    @filters << QueryHelper::Filter.new(
      operator_code: criteria.keys.first,
      criterion: criteria.values.first,
      comparate: map.sql_expression,
      aggregate: map.aggregate
    )
  end
end

#having_clausesObject



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

def having_clauses
  @filters.select{ |f| f.aggregate == true }.map(&:sql_string)
end

#where_clausesObject



31
32
33
# File 'lib/query_helper/sql_filter.rb', line 31

def where_clauses
  @filters.select{ |f| f.aggregate == false }.map(&:sql_string)
end