Class: Virsandra::ModelQuery

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

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ModelQuery

Returns a new instance of ModelQuery.


4
5
6
# File 'lib/virsandra/model_query.rb', line 4

def initialize(model)
  @model = model
end

Instance Method Details

#deleteObject


19
20
21
22
# File 'lib/virsandra/model_query.rb', line 19

def delete
  query = Query.delete.from(@model.table).where(@model.key)
  query.fetch
end

#find_by_keyObject


8
9
10
11
12
# File 'lib/virsandra/model_query.rb', line 8

def find_by_key
  return {} unless @model.valid?
  query = Query.select.from(@model.table).where(@model.key)
  query.fetch
end

#saveObject


14
15
16
17
# File 'lib/virsandra/model_query.rb', line 14

def save
  query = Query.insert.into(@model.table).values(@model.attributes)
  query.fetch
end

#where(params) ⇒ Object


24
25
26
27
28
29
30
31
32
33
# File 'lib/virsandra/model_query.rb', line 24

def where(params)
  query = Query.select.from(@model.table)

  unless params.empty?
    raise ArgumentError.new("Invalid search terms") unless valid_search_params?(params)
    query.where(params)
  end

  query_enumerator(query)
end