class Pokan::Query
The Query
class is used to find Entities that obey restrictions. It is commonly used to get a Entity
according with a id, get all dead Peers and so on.
Public Class Methods
new(entity_class)
click to toggle source
# File lib/pokan/query.rb, line 9 def initialize(entity_class) @entity_class = entity_class @db = Connection.redis end
Public Instance Methods
where(query)
click to toggle source
Returns a array of entities respecting the given restrictions Structure: {id: ‘234’, role: ‘dead’, status:[‘alive’, ‘dead’], random: true…}
# File lib/pokan/query.rb, line 17 def where(query) result = [] candidates = @db.smembers('entities') if query.has_key?(:id) query[:id] = [query[:id]] unless query[:id].is_a?(Array) candidates = candidates & query[:id] end candidates = candidates - query[:not] if query.has_key?(:not) candidates.each do |entity_id| entity = @entity_class.new entity.id = entity_id entity.reload result << entity if entity.match?(query.reject {|k, v| k == :not || k == :random || k == :id}) end result = [result.sample] if query[:random] result end