Class: Conditioner::Condition
- Inherits:
-
String
- Object
- String
- Conditioner::Condition
- Defined in:
- lib/conditioner/condition.rb
Instance Method Summary collapse
- #_and(*args) ⇒ Object
- #_or(*args) ⇒ Object
- #add_condition(unit, *args) ⇒ Object
- #and(*args) ⇒ Object
- #extract(hash) ⇒ Object
-
#initialize(model, options = {}) {|_self| ... } ⇒ Condition
constructor
A new instance of Condition.
- #is_field?(field) ⇒ Boolean
- #or(*args) ⇒ Object
- #with_table_name(field_name) ⇒ Object
Constructor Details
#initialize(model, options = {}) {|_self| ... } ⇒ Condition
Returns a new instance of Condition.
3 4 5 6 7 8 9 |
# File 'lib/conditioner/condition.rb', line 3 def initialize(model, = {}) @model = model @column_names = @model.column_names @result = [] @first_condition = true yield self if block_given? end |
Instance Method Details
#_and(*args) ⇒ Object
15 16 17 |
# File 'lib/conditioner/condition.rb', line 15 def _and(*args) add_condition('AND',*args) end |
#_or(*args) ⇒ Object
23 24 25 |
# File 'lib/conditioner/condition.rb', line 23 def _or(*args) add_condition('OR',*args) end |
#add_condition(unit, *args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/conditioner/condition.rb', line 27 def add_condition(unit,*args) @condition_called_flag= true result= [] unless @first_condition result << ' ' << unit else @first_condition = false end result << @model.send(:sanitize_sql_for_conditions, *args) self << result.join(" ") self end |
#and(*args) ⇒ Object
11 12 13 |
# File 'lib/conditioner/condition.rb', line 11 def and(*args) add_condition('AND',*args) end |
#extract(hash) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/conditioner/condition.rb', line 48 def extract(hash) hash.each do |k,v| @condition_called_flag = false field = k.to_s Conditioner.config.extract_rules.each do |rule| rule.call(field,v,self) break if @condition_called_flag end _and(field=>v) if is_field?(field) and !@condition_called_flag end self end |
#is_field?(field) ⇒ Boolean
40 41 42 |
# File 'lib/conditioner/condition.rb', line 40 def is_field?(field) @column_names.include?(field) end |
#or(*args) ⇒ Object
19 20 21 |
# File 'lib/conditioner/condition.rb', line 19 def or(*args) add_condition('OR',*args) end |
#with_table_name(field_name) ⇒ Object
44 45 46 |
# File 'lib/conditioner/condition.rb', line 44 def with_table_name(field_name) %Q[#{@model.table_name}.#{field_name}] end |