Class: SearchBuilder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_object, options = {}) ⇒ SearchBuilder

Returns a new instance of SearchBuilder.



11
12
13
14
15
# File 'lib/search_builder.rb', line 11

def initialize(target_object, options={})
  self.object = target_object.is_a?(Hash) ? OpenStruct.new(target_object) : target_object
  self.where = options[:append_to] || Where.new
  @table_prefix = ""
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



3
4
5
# File 'lib/search_builder.rb', line 3

def object
  @object
end

#whereObject

Returns the value of attribute where.



4
5
6
# File 'lib/search_builder.rb', line 4

def where
  @where
end

Class Method Details

.cast_to(value, type) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/search_builder.rb', line 111

def self.cast_to(value, type)
  return value if value.nil?
  
  case type
  when nil
    value
  when :array
    value = Array===value ? value : [value]
  when :time
    Time.parse(value)
  when :date
    Time.parse(value).to_date
  when :i, :int, :integer
    value.to_i
  when :f, :float
    value.to_f
  when :string
    value.to_s
  else
    raise "unknown cast type: #{type}"
  end
  
end

.delegate_to(object_name, methods = []) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/search_builder.rb', line 17

def self.delegate_to(object_name, methods = [])
  for method_name in methods
    class_eval <<-EOF, __FILE__, __LINE__ +1
      def #{method_name}(*params, &block)
        #{object_name} && #{object_name}.#{method_name}(*params, &block)
      end
    EOF
  end
end

Instance Method Details

#and_unless_blank(condition, value, options = {}) ⇒ Object



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

def and_unless_blank(condition, value, options={})
  value = value.compact if (Array === value)
  
  # if value is an empty array or a blank string, don't filter on it
  return self if value.blank?
  
  prefix = options[:prefix]
  suffix = options[:suffix]
  if prefix || suffix
    @where.and(condition, [prefix, value, suffix].compact.to_s )
  else
    @where.and(condition, value)
  end
  
  self
end

#cast_to(value, type) ⇒ Object



107
108
109
# File 'lib/search_builder.rb', line 107

def cast_to(value, type)
  self.class.cast_to(value, type)
end

#equal_on(field, options = {}) ⇒ Object



63
64
65
66
67
# File 'lib/search_builder.rb', line 63

def equal_on(field, options={})
  options = options.clone
  options[:cast] = :string
  process_clause(field, "= ?", options)
end

#for_table(table, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/search_builder.rb', line 48

def for_table(table, &block)
  if block_given?
    last_table_prefix = @table_prefix
  end
  
  @table_prefix = "#{table}."
    
  if block_given?
    yield
    
    @table_prefix = last_table_prefix
  end
  
end

#in_on(field, options = {}) ⇒ Object



69
70
71
72
73
# File 'lib/search_builder.rb', line 69

def in_on(field, options={})
  options = options.clone
  options[:cast] ||= :array
  process_clause(field, "in (?)", options)
end

#like_on(field, options = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/search_builder.rb', line 41

def like_on(field, options={})
  options = options.clone
  options[:suffix] ||= "%"
  
  process_clause(field, "like ?", options)
end

#process_clause(field, operator_clause, options = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/search_builder.rb', line 75

def process_clause(field, operator_clause, options={})
  param = options[:param] || field
  self.and_unless_blank("#{@table_prefix}#{field} #{operator_clause}", value_for(param, options[:cast]), options)
  
  self
end

#range_on(field, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/search_builder.rb', line 29

def range_on(field, options={})
  options = options.clone
  min_param = options[:min_param] || "#{options[:param] || field}_min"
  max_param = options[:max_param] || "#{options[:param] || field}_max"
  cast = options[:cast] || :string
  
  process_clause(field, ">= ?", options.merge(:param => min_param))
  process_clause(field, "<= ?", options.merge(:param => max_param))
  
  self
end

#value_for(param, cast = :string) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/search_builder.rb', line 99

def value_for(param, cast=:string)
  param = param.to_s
  if param.include?(".")
    param=param.split(".").last
  end
  cast_to( object.send(param), cast)
end