Class: HashToConditions::HashHelper
- Inherits:
-
Object
- Object
- HashToConditions::HashHelper
- Defined in:
- lib/helpers/hash_helper.rb
Overview
This class performs the bulk of the work. It takes a Hash and return the fully expanded Array condition.
For example:
> helper = HashHelper.new({'age.gt' => 18})
> helper.to_conditions => ['(age>?)', 18]
Boolean AND and OR can be used to join multiple conditions.
Examples:
> helper = HashHelper.new({'AND' => {'name' => 'Lou%', 'age.gt' => 18}})
> helper.to_conditions => ['(name LIKE ? AND age>?)', 'Lou%', 18]
> helper = HashHelper.new({'name.like' => 'Lou%', 'age.gt' => 18}) - boolean AND is implicit here
> helper.to_conditions => ['(name LIKE ? AND age>?)', 'Lou%', 18]
> helper = HashHelper.new({'OR' => {'name.like' => 'Lou%', 'age.gt' => 18}})
> helper.to_conditions => ['(name LIKE ? OR age>?)', 'Lou%', 18]
Nested conditions are also supported:
> nested_h = {'name' => 'Lou%', 'age.gt' => 18}
> helper = HashHelper.new({'OR' => {'AND' => nested_h, 'salary.between' => '50k, 100k'}})
> helper.to_conditions => ['((name LIKE ? AND age>?) OR salary BETWEEN ? AND ?)', 'Lou%', 18, '50k', '100k']
Instance Method Summary collapse
-
#to_conditions ⇒ Object
Returns a fully expaned condition array.
Instance Method Details
#to_conditions ⇒ Object
Returns a fully expaned condition array
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/helpers/hash_helper.rb', line 34 def to_conditions raise "empty_condition" if @hash.empty? result_s = '' result_a = [] join_s = @hash.first.first.to_s if ['AND', 'OR'].index(join_s.upcase) parse(@hash.first.last, join_s.upcase, result_s, result_a) else parse(@hash, 'AND', result_s, result_a) end result_a.unshift(result_s) result_a end |