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 ) ) )
Generate a Query from a hash
hash - Hash of a query
+Comparator or Operator+ - the resultant query
# 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
Generate a Query from json
json - String of JSON of a
query
+Comparator or Operator+ - the resultant query
# File lib/onapp_market/api/query.rb, line 40 def from_json(json) OnappMarket::API::Query::from_hash(JSON.parse(json)) end
Generate a Hash from a Query
query - Comparator or Query
Hash
# File lib/onapp_market/api/query.rb, line 50 def to_a_hash(query) query.to_hash end
Generate JSON from a Query
query - Comparator or Query
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