class RecordsRip::WhereEpitaph

Public Class Methods

new(tomb_model_class, attributes) click to toggle source
# File lib/records_rip/where_epitaph.rb, line 3
def initialize(tomb_model_class, attributes)
  @tomb_model_class = tomb_model_class
  @attributes = attributes
end

Public Instance Methods

execute() click to toggle source
# File lib/records_rip/where_epitaph.rb, line 8
def execute
  column = @tomb_model_class.columns_hash["epitaph"]
  raise "where_epitaph can't be called without an epitaph column" unless column

  case column.type
  when :jsonb
    jsonb
  when :json
    json
  else
    text
  end
end

Private Instance Methods

json() click to toggle source
# File lib/records_rip/where_epitaph.rb, line 24
def json
  predicates = []
  values = []
  @attributes.each do |field, value|
    predicates.push "epitaph->>? = ?"
    values.concat([field, value.to_s])
  end
  sql = predicates.join(" and ")
  @tomb_model_class.where(sql, *values)
end
jsonb() click to toggle source
# File lib/records_rip/where_epitaph.rb, line 35
def jsonb
  @tomb_model_class.where("epitaph @> ?", @attributes.to_json)
end
text() click to toggle source
# File lib/records_rip/where_epitaph.rb, line 39
def text
  arel_field = @tomb_model_class.arel_table[:epitaph]
  where_conditions = @attributes.map { |field, value|
    where_epitaph_condition(arel_field, field, value)
  }
  where_conditions = where_conditions.reduce { |a, e| a.and(e) }
  @tomb_model_class.where(where_conditions)
end
where_epitaph_condition(arel_field, field, value) click to toggle source
# File lib/records_rip/where_epitaph.rb, line 48
def where_epitaph_condition(arel_field, field, value)
  arel_field.matches(%Q(%"#{field}"=>"#{value}"%)).or(arel_field.matches(%Q(%"#{field}"=>#{value}%)))
end