Class: Mashery::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mashery/query_builder.rb

Defined Under Namespace

Classes: From, Items, Order, Page, Select, Where

Instance Method Summary collapse

Constructor Details

#initialize(klass = nil, options = {}) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



3
4
5
6
7
8
# File 'lib/mashery/query_builder.rb', line 3

def initialize(klass = nil, options = {})
  @klass      = klass
  @options    = options
  @conditions = Where.new
  @select     = Select.new
end

Instance Method Details

#allObject



57
58
59
# File 'lib/mashery/query_builder.rb', line 57

def all
  Mashery::RpcClient::Response.new(self)
end

#find_each(&block) ⇒ Object



61
62
63
# File 'lib/mashery/query_builder.rb', line 61

def find_each(&block)
  Mashery::RpcClient::Response.new(self).find_each(&block)
end

#firstObject



65
66
67
# File 'lib/mashery/query_builder.rb', line 65

def first
  Mashery::RpcClient::Response.new(items(1)).to_objects.first
end

#from(argument) ⇒ Object



15
16
17
18
# File 'lib/mashery/query_builder.rb', line 15

def from(argument)
  @from = From.new(argument)
  clone
end

#items(argument) ⇒ Object



30
31
32
33
# File 'lib/mashery/query_builder.rb', line 30

def items(argument)
  @items = Items.new(argument)
  clone
end

#order(column, order) ⇒ Object



25
26
27
28
# File 'lib/mashery/query_builder.rb', line 25

def order(column, order)
  @order = Order.new(column, order)
  clone
end

#page(argument) ⇒ Object



35
36
37
38
# File 'lib/mashery/query_builder.rb', line 35

def page(argument)
  @page = Page.new(argument)
  clone
end

#queryObject Also known as: to_s



40
41
42
43
44
# File 'lib/mashery/query_builder.rb', line 40

def query
  [@select, @from, @order, @conditions, @page, @items].map(&:to_s).reject {|s|
    s.blank?
  }.compact.join(" ").strip
end

#reify(*args) ⇒ Object

Raises:



46
47
48
49
# File 'lib/mashery/query_builder.rb', line 46

def reify(*args)
  raise NoClassGiven.new if @klass.blank?
  @klass.new(*args)
end

#select(argument) ⇒ Object



10
11
12
13
# File 'lib/mashery/query_builder.rb', line 10

def select(argument)
  @select = Select.new(argument)
  clone
end

#to_jsonObject



53
54
55
# File 'lib/mashery/query_builder.rb', line 53

def to_json
  Mashery.rpc.query(query)
end

#where(argument) ⇒ Object



20
21
22
23
# File 'lib/mashery/query_builder.rb', line 20

def where(argument)
  @conditions.add argument
  clone
end