class Hayfork::Statement

Attributes

attributes[R]
dictionary[RW]
haystack[R]
relation[R]
value[R]
weight[RW]

Public Class Methods

new(haystack, relation, field, options={}) click to toggle source
# File lib/hayfork/statement.rb, line 11
def initialize(haystack, relation, field, options={})
  @haystack = haystack
  @relation = relation.all
  @weight = options.fetch(:weight, Hayfork.default_weight) # TODO: validate weight
  @dictionary = options.fetch(:dictionary, Hayfork.default_dictionary)
  @attributes = {}.with_indifferent_access
  @unnest = false
  @unsearchable = false

  case field
  when Arel::Predications
    @value = field
  when String
    @value = model.arel_table[field]
  when Symbol
    @value = model.arel_table[field.to_s]
  when Hash
    reflection = reflection_for(field.keys.first)
    joins field.keys.first
    @value = reflection.klass.arel_table[field.values.first.to_s]
  else
    fail ArgumentError, "Unrecognized value for `field`: #{field.inspect}"
  end
end

Public Instance Methods

bindings() click to toggle source
# File lib/hayfork/statement.rb, line 97
def bindings
  @bindings ||= (haystack.columns.each_with_object([]) do |column, bindings|
    next if column.name == Hayfork::SEARCH_VECTOR && unsearchable?

    value = attributes.fetch column.name do
      case column.name
      when Hayfork::SEARCH_RESULT_TYPE then model.name
      when Hayfork::SEARCH_RESULT_ID then model.arel_table["id"]
      when Hayfork::SEARCH_VECTOR, Hayfork::TEXT then self.value
      when Hayfork::SOURCE_TYPE then self.value.relation.send(:type_caster).send(:types).name
      when Hayfork::SOURCE_ID then self.value.relation["id"]
      when Hayfork::FIELD then self.value.name
      end
    end

    next unless value
    value = model.arel_table[value] if value.is_a? Symbol
    bindings.push Hayfork::Binding.new(self, column, value)
  end)
end
delete() click to toggle source
# File lib/hayfork/statement.rb, line 91
def delete
  DeleteSql.new(haystack, relation, bindings)
end
insert() click to toggle source
# File lib/hayfork/statement.rb, line 83
def insert
  InsertSql.new(haystack, relation, bindings)
end
joins(join_value) click to toggle source
# File lib/hayfork/statement.rb, line 38
def joins(join_value)
  @relation = Hayfork.join(relation, join_value)
  self
end
may_change_on_update?() click to toggle source
# File lib/hayfork/statement.rb, line 77
def may_change_on_update?
  !update.values_to_check_on_update.empty?
end
merge(attrs = {}) click to toggle source
# File lib/hayfork/statement.rb, line 48
def merge(attrs = {})
  attributes.merge! attrs.stringify_keys.except(
    Hayfork::SEARCH_VECTOR,
    Hayfork::TEXT,
    Hayfork::SOURCE_TYPE,
    Hayfork::SOURCE_ID)
  self
end
unnest() click to toggle source
# File lib/hayfork/statement.rb, line 62
def unnest
  @unnest = true
  self
end
unnest?() click to toggle source
# File lib/hayfork/statement.rb, line 73
def unnest?
  @unnest == true
end
unsearchable() click to toggle source
# File lib/hayfork/statement.rb, line 57
def unsearchable
  @unsearchable = true
  self
end
unsearchable?() click to toggle source
# File lib/hayfork/statement.rb, line 69
def unsearchable?
  @unsearchable == true
end
update() click to toggle source
# File lib/hayfork/statement.rb, line 87
def update
  UpdateSql.new(haystack, relation, bindings)
end
where(where_value) click to toggle source
# File lib/hayfork/statement.rb, line 43
def where(where_value)
  @relation = relation.where(where_value)
  self
end

Private Instance Methods

model() click to toggle source
# File lib/hayfork/statement.rb, line 123
def model
  relation.model
end
reflection_for(association) click to toggle source
# File lib/hayfork/statement.rb, line 127
def reflection_for(association)
  Hayfork.reflection_for(model, association)
end