Class: IseshimaStore::WhereClause

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ WhereClause



5
6
7
8
9
# File 'lib/iseshima_store/where_clause.rb', line 5

def initialize(klass)
  # whereチェーンされているモデルのクラス
  @klass = klass
  @conditions = []
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



3
4
5
# File 'lib/iseshima_store/where_clause.rb', line 3

def conditions
  @conditions
end

Instance Method Details

#+(condition) ⇒ Object



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/iseshima_store/where_clause.rb', line 11

def +(condition)

  # conditions like where(name: 'taro', email: '[email protected]')
  if condition.is_a?(Hash)
    condition.each do |property, value|
      property = property.to_s
      if property == 'id'
        datastore = IseshimaStore::Connection.current
        key = datastore.key(@klass.to_s, value.to_i)
        @conditions << ['__key__', '=', key]
      else
        @conditions << [property, '=', value]
      end
    end
  # condisions like where('age', '>=', 16)
  elsif condition.is_a?(Array) && condition.length == 3
    @conditions << condition
  # Other
  else
    raise ArgumentError.new("wrong format of arguments")
  end

  self
end