Class: Hayfork::Statement

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(haystack, relation, field, options = {}) ⇒ Statement

Returns a new instance of Statement.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



9
10
11
# File 'lib/hayfork/statement.rb', line 9

def dictionary
  @dictionary
end

#haystackObject (readonly)

Returns the value of attribute haystack.



8
9
10
# File 'lib/hayfork/statement.rb', line 8

def haystack
  @haystack
end

#relationObject (readonly)

Returns the value of attribute relation.



8
9
10
# File 'lib/hayfork/statement.rb', line 8

def relation
  @relation
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/hayfork/statement.rb', line 8

def value
  @value
end

#weightObject

Returns the value of attribute weight.



9
10
11
# File 'lib/hayfork/statement.rb', line 9

def weight
  @weight
end

Instance Method Details

#bindingsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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

#deleteObject



91
92
93
# File 'lib/hayfork/statement.rb', line 91

def delete
  DeleteSql.new(haystack, relation, bindings)
end

#insertObject



83
84
85
# File 'lib/hayfork/statement.rb', line 83

def insert
  InsertSql.new(haystack, relation, bindings)
end

#joins(join_value) ⇒ Object



38
39
40
41
# File 'lib/hayfork/statement.rb', line 38

def joins(join_value)
  @relation = Hayfork.join(relation, join_value)
  self
end

#may_change_on_update?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/hayfork/statement.rb', line 77

def may_change_on_update?
  !update.values_to_check_on_update.empty?
end

#merge(attrs = {}) ⇒ Object



48
49
50
51
52
53
54
55
# 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

#unnestObject



62
63
64
65
# File 'lib/hayfork/statement.rb', line 62

def unnest
  @unnest = true
  self
end

#unnest?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/hayfork/statement.rb', line 73

def unnest?
  @unnest == true
end

#unsearchableObject



57
58
59
60
# File 'lib/hayfork/statement.rb', line 57

def unsearchable
  @unsearchable = true
  self
end

#unsearchable?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/hayfork/statement.rb', line 69

def unsearchable?
  @unsearchable == true
end

#updateObject



87
88
89
# File 'lib/hayfork/statement.rb', line 87

def update
  UpdateSql.new(haystack, relation, bindings)
end

#where(where_value) ⇒ Object



43
44
45
46
# File 'lib/hayfork/statement.rb', line 43

def where(where_value)
  @relation = relation.where(where_value)
  self
end