Class: LazyRecords::PredicateToSql

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

Instance Method Summary collapse

Constructor Details

#initializePredicateToSql

Returns a new instance of PredicateToSql.



4
5
6
7
8
9
10
11
12
# File 'lib/predicate_to_sql.rb', line 4

def initialize
  @pred_map = {
      equals: '=',
      equal_to: '=',
      greater_than: '>',
      less_than: '<',
      like: 'like'
  }
end

Instance Method Details

#convert(predicate) ⇒ Object



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

def convert(predicate)
  predicate.predicates.map do |pred|
    operation = option(@pred_map[pred.value.name]).get_or_throw(NoSuchElementException, "Operation not supported: #{pred.value.name}")
    column = pred.key.to_s
    value = pred.value.value
    "#{column} #{operation} '#{value}'"
  end.to_a.join(' and ')
end

#custom(pred_map) ⇒ Object



14
15
16
# File 'lib/predicate_to_sql.rb', line 14

def custom(pred_map)
  @pred_map = @pred_map.merge(pred_map)
end