module OnappMarket::API::Query

Very light weight classes for specifying queries concisely. These queries can be flattened. Loosely based around DataMapper principals for queries. You may construct a query such as:

Query = OnappMarket::API::Query
# We want something less than 5 long which is blue, but is not a square
query = Query::And.new(:length.lt(5), Query::Or.new(:colour.eq("blue"), Query::Not.new(:shape.eq("square"))))
# Print flattened value
query.flatten { |n| print n.to_s+" " }
# Yields : ( length LT 5 AND ( colour EQ blue OR NOT ( shape EQ square ) ) )

Public Instance Methods

from_hash(hash) click to toggle source

Generate a Query from a hash

Attributes

  • hash - Hash of a query

Returns

# File lib/onapp_market/api/query.rb, line 21
def from_hash(hash)
  raise "Invalid type" unless hash["type"] == "Comparator" or hash["type"] == "MonoOperator" or hash["type"] == "MultiOperator" or hash["type"] == "NilOperator"
  type = hash["type"]
  # Get leaves (if we need to)
  leaves = []
  leaves = hash["leaves"].map { |l|  OnappMarket::API::Query::from_hash(l) } unless hash["type"] == "Comparator"
  # Return the appropriate class
  return Comparator.new(hash["field"], hash["comparator"], hash["value"]) if type == "Comparator" 
  return MonoOperator.new(hash["operator"], leaves[0]) if type == "MonoOperator"
  return MultiOperator.new(hash["operator"], leaves) if type == "MultiOperator"
  return NilOperator.new if type == 'NilOperator'
end
from_json(json) click to toggle source

Generate a Query from json

Attributes

  • json - String of JSON of a query

Returns

# File lib/onapp_market/api/query.rb, line 40
def from_json(json)
  OnappMarket::API::Query::from_hash(JSON.parse(json))
end
to_a_hash(query) click to toggle source

Generate a Hash from a Query

Attributes

Returns

  • Hash

# File lib/onapp_market/api/query.rb, line 50
def to_a_hash(query)
  query.to_hash
end
to_json(query) click to toggle source

Generate JSON from a Query

Attributes

Returns

  • String = JSON representation

# File lib/onapp_market/api/query.rb, line 60
def to_json(query)
  JSON.generate(OnappMarket::API::Query::to_a_hash(query))
end