Class: Specifind::Comparators::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/specifind/comparators/base.rb

Overview

Comparator holds the logic for each type of comparator that is use in MethodBuilder definition.

The data are held in the class definition as [identifier (String), number of parameters (int), parameter suffixes (list of String), and sql creators (Procs)].

Direct Known Subclasses

Mysql2, PostgreSQL, SQLite3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



46
47
48
49
50
51
52
53
# File 'lib/specifind/comparators/base.rb', line 46

def initialize(args)
  @pattern = args[:pattern]
  @num_params = args[:num_params]
  @param_suffixes = @num_params > 0 ? args[:param_suffixes] : []
  @sql_proc = args[:sql_proc]
  @@encoding = ActiveRecord::Base.connection.instance_variable_get('@config')[:encoding]
  self.class.comparators += [self]
end

Instance Attribute Details

#num_paramsObject

Returns the value of attribute num_params.



9
10
11
# File 'lib/specifind/comparators/base.rb', line 9

def num_params
  @num_params
end

#param_suffixesObject

Returns the value of attribute param_suffixes.



9
10
11
# File 'lib/specifind/comparators/base.rb', line 9

def param_suffixes
  @param_suffixes
end

#patternObject

Returns the value of attribute pattern.



9
10
11
# File 'lib/specifind/comparators/base.rb', line 9

def pattern
  @pattern
end

#sql_procObject

Returns the value of attribute sql_proc.



9
10
11
# File 'lib/specifind/comparators/base.rb', line 9

def sql_proc
  @sql_proc
end

#valuesObject

Returns the value of attribute values.



9
10
11
# File 'lib/specifind/comparators/base.rb', line 9

def values
  @values
end

Class Method Details

.comparators_dataObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/specifind/comparators/base.rb', line 25

def self.comparators_data
  [
    ['_in_list',              1, %w(_list),                     Proc.new{|v| "in (#{v[0]})"}],
    ['_less_than_equals',     1, %w(_val),                      Proc.new{|v| "<= #{v[0]}"}],
    ['_less_than',            1, %w(_val),                      Proc.new{|v| "< #{v[0]}"}],
    ['_greater_than_equals',  1, %w(_val),                      Proc.new{|v| ">= #{v[0]}"}],
    ['_greater_than',         1, %w(_val),                      Proc.new{|v| "> #{v[0]}"}],
    ['_not_equal',            1, %w(_val),                      Proc.new{|v| "!= #{v[0]}"}],
    ['_between',              2, %w(_bound_one _bound_two),     Proc.new{|v| "between #{v[0]} and #{v[1]}"}],
    ['_is_not_null',          0, [],                            Proc.new{|v| "is not null"}],
    ['_is_null',              0, [],                            Proc.new{|v| "is null"}],
    ['_equals',               1, %w(_val),                      Proc.new{|v| "= #{v[0]}"}]
  ]
end

.find(s) ⇒ Object

find and return a (new) comparator by it’s identifying string



20
21
22
23
# File 'lib/specifind/comparators/base.rb', line 20

def self.find(s)
  self.comparators.each{|c| return c.clone if c.pattern == s}
  nil
end

.generate_comparatorsObject

From the data listed in the Comparator class definition, Comparator.generate_comparators constructs each type of Comparators and adds it to Comparator.comparators



42
43
44
# File 'lib/specifind/comparators/base.rb', line 42

def self.generate_comparators
  self.comparators_data.each{|c| c = self.new :pattern => c[0], :num_params => c[1], :param_suffixes => c[2], :sql_proc => c[3] }
end

.patternsObject

list of comparator’s patterns



13
14
15
16
17
# File 'lib/specifind/comparators/base.rb', line 13

def self.patterns
  a = []
  self.comparators.each{|c| a << c.pattern}
  a
end

Instance Method Details

#set_values(property_name, attribute_hash) ⇒ Object

Creates the values list for this comparator so that when it’s build_sql method is called, the sql building proc will know how to build the appropriate where clause



57
58
59
60
# File 'lib/specifind/comparators/base.rb', line 57

def set_values(property_name, attribute_hash)
  @values = []
  param_suffixes.each{|s| @values << attribute_hash["#{property_name}#{s}".to_sym]} if num_params > 0
end

#to_params(name) ⇒ Object

builds hash component (the parameters for the search_by method) based on the type of suffixes that are required for this type of Comparator. See AttributePhrase#to_params for further detail



78
79
80
# File 'lib/specifind/comparators/base.rb', line 78

def to_params(name)
  param_suffixes.map { |p| ":#{name}#{p} => #{name}#{p}" }.join(',')
end

#to_rearrangement(name, type) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/specifind/comparators/base.rb', line 98

def to_rearrangement(name, type)
  out = ''
  if @pattern == '_in_list'
    out += "#{name}#{param_suffixes[0]} = #{name}#{param_suffixes[0]}.map{|el| \"'\"+el+\"'\"}.join ','"
  end
  out
end

#to_signature(name) ⇒ Object

builds signature (the parameter definitions for a method) based on the type of suffixes that are required for this type of Comparator. See AttributePhrase#to_signature for further detail



72
73
74
# File 'lib/specifind/comparators/base.rb', line 72

def to_signature(name)
  param_suffixes.map{|p| "#{name}#{p}" }.join(',')
end

#to_type_test(name, type) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/specifind/comparators/base.rb', line 82

def to_type_test(name, type)
  out = ""
  # assert that each variable associated with this comparator is of type passed.
  # if its not a list
  if @pattern != '_in_list'
    param_suffixes.each do |p|
      out += "#{name}#{p} = Type.assert_#{type}(#{name}#{p})\n"
    end
  else # is list - will only have one parameter, which is a list
    out += "#{name}#{param_suffixes[0]}.each do |v|
              v = Type.assert_#{type}(v)
            end"
  end
  out
end

#to_where(name, remove_quotes = false) ⇒ Object

builds sql (through @sql_proc) for values that corresponds with this type of comparator



63
64
65
66
67
68
# File 'lib/specifind/comparators/base.rb', line 63

def to_where(name, remove_quotes = false)
  #raise 'values for comparator are not defined' if !values
  response = @sql_proc.call param_suffixes.map{|p| "\#{#{name}#{p}}" }
  response.delete! "'" if remove_quotes
  response
end