Class: Montage::Query
Instance Attribute Summary collapse
-
#query ⇒ Object
Returns the value of attribute query.
Instance Method Summary collapse
-
#index(field) ⇒ Object
Specifies and index to use on a query.
-
#initialize ⇒ Query
constructor
A new instance of Query.
-
#limit(max = nil) ⇒ Object
Defines the limit to apply to the query, defaults to nil.
-
#offset(value = nil) ⇒ Object
Defines the offset to apply to the query, defaults to nil.
-
#order(clause = {}) ⇒ Object
Defines the order clause for the query and merges it into the query hash.
-
#pluck(column_name) ⇒ Object
Pluck just one column from the result set.
-
#select(*args) ⇒ Object
Select a set of columns from the result set.
-
#to_json ⇒ Object
Parses the current query hash and returns a JSON string.
-
#where(clause) ⇒ Object
Adds a where clause to the query filter hash.
Methods included from Support
Constructor Details
#initialize ⇒ Query
Returns a new instance of Query.
12 13 14 |
# File 'lib/montage/query.rb', line 12 def initialize @query = { filter: {} } end |
Instance Attribute Details
#query ⇒ Object
Returns the value of attribute query.
10 11 12 |
# File 'lib/montage/query.rb', line 10 def query @query end |
Instance Method Details
#index(field) ⇒ Object
Specifies and index to use on a query. RethinkDB isn’t as smart as some other database engines when selecting a query plan, but it does let you specify which index to use
103 104 105 |
# File 'lib/montage/query.rb', line 103 def index(field) clone.tap { |r| r.query.merge!(index: field) } end |
#limit(max = nil) ⇒ Object
Defines the limit to apply to the query, defaults to nil
Merges a hash:
{ limit: 10 }
Returns self
23 24 25 |
# File 'lib/montage/query.rb', line 23 def limit(max = nil) clone.tap { |r| r.query.merge!(limit: max) } end |
#offset(value = nil) ⇒ Object
Defines the offset to apply to the query, defaults to nil
Merges a hash:
{ offset: 10 }
Returns a copy of self
34 35 36 |
# File 'lib/montage/query.rb', line 34 def offset(value = nil) clone.tap { |r| r.query.merge!(offset: value) } end |
#order(clause = {}) ⇒ Object
Defines the order clause for the query and merges it into the query hash
Accepts either a string or a hash:
order("foo asc") or
order(:foo => :asc) or
order(:foo => "asc")
Defaults the direction to asc if no value is passed in for that, or if it is not a valid value
Merges a hash:
{ order: "foo asc" }
Returns a copy of self
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/montage/query.rb', line 52 def order(clause = {}) if clause.is_a?(Hash) direction = clause.values.first.to_s field = clause.keys.first.to_s else direction = clause.split(" ")[1] field = clause.split(" ")[0] direction = "asc" unless %w(asc desc).include?(direction) end clone.tap{ |r| r.query.merge!(order_by: field, ordering: direction) } end |
#pluck(column_name) ⇒ Object
Pluck just one column from the result set
109 110 111 |
# File 'lib/montage/query.rb', line 109 def pluck(column_name) clone.tap { |r| r.query.merge!(pluck: [column_name.to_s]) } end |
#select(*args) ⇒ Object
Select a set of columns from the result set
95 96 97 |
# File 'lib/montage/query.rb', line 95 def select(*args) clone.tap { |r| r.query.merge!(pluck: args.map(&:to_s))} end |
#to_json ⇒ Object
Parses the current query hash and returns a JSON string
115 116 117 |
# File 'lib/montage/query.rb', line 115 def to_json @query.to_json end |
#where(clause) ⇒ Object
Adds a where clause to the query filter hash
Accepts either a Hash or a String
where(foo: 1)
where("foo > 1")
Merges a hash:
{ foo: 1 }
Returns a copy of self
89 90 91 |
# File 'lib/montage/query.rb', line 89 def where(clause) clone.tap { |r| r.query[:filter].merge!(QueryParser.new(clause).parse) } end |