Class: SearchBuilder
- Inherits:
-
Object
- Object
- SearchBuilder
- Defined in:
- lib/search_builder.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#where ⇒ Object
Returns the value of attribute where.
Class Method Summary collapse
Instance Method Summary collapse
- #and_unless_blank(condition, value, options = {}) ⇒ Object
- #cast_to(value, type) ⇒ Object
- #equal_on(field, options = {}) ⇒ Object
- #for_table(table, &block) ⇒ Object
- #in_on(field, options = {}) ⇒ Object
-
#initialize(target_object, options = {}) ⇒ SearchBuilder
constructor
A new instance of SearchBuilder.
- #like_on(field, options = {}) ⇒ Object
- #process_clause(field, operator_clause, options = {}) ⇒ Object
- #range_on(field, options = {}) ⇒ Object
- #value_for(param, cast = :string) ⇒ Object
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, ={}) self.object = target_object.is_a?(Hash) ? OpenStruct.new(target_object) : target_object self.where = [:append_to] || Where.new @table_prefix = "" end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
3 4 5 |
# File 'lib/search_builder.rb', line 3 def object @object end |
#where ⇒ Object
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, ={}) 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 = [:prefix] suffix = [: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, ={}) = .clone [:cast] = :string process_clause(field, "= ?", ) 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, ={}) = .clone [:cast] ||= :array process_clause(field, "in (?)", ) end |
#like_on(field, options = {}) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/search_builder.rb', line 41 def like_on(field, ={}) = .clone [:suffix] ||= "%" process_clause(field, "like ?", ) 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, ={}) param = [:param] || field self.and_unless_blank("#{@table_prefix}#{field} #{operator_clause}", value_for(param, [:cast]), ) 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, ={}) = .clone min_param = [:min_param] || "#{[:param] || field}_min" max_param = [:max_param] || "#{[:param] || field}_max" cast = [:cast] || :string process_clause(field, ">= ?", .merge(:param => min_param)) process_clause(field, "<= ?", .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 |