class Opium::Model::Criteria
Attributes
model_name[R]
Public Class Methods
new( model_name )
click to toggle source
# File lib/opium/model/criteria.rb, line 10 def initialize( model_name ) @model_name = model_name.respond_to?(:name) ? model_name.name : model_name constraints[:count] = 1 end
Public Instance Methods
==( other )
click to toggle source
# File lib/opium/model/criteria.rb, line 69 def ==( other ) other.is_a?( self.class ) && self.model_name == other.model_name && self.constraints == other.constraints && self.variables == other.variables end
chain()
click to toggle source
# File lib/opium/model/criteria.rb, line 25 def chain Marshal.load( Marshal.dump( self ) ).tap {|m| m.instance_variable_set( :@cache, nil )} end
constraints()
click to toggle source
# File lib/opium/model/criteria.rb, line 29 def constraints @constraints ||= {}.with_indifferent_access end
constraints?()
click to toggle source
# File lib/opium/model/criteria.rb, line 41 def constraints? !constraints.except(:count).empty? end
criteria()
click to toggle source
# File lib/opium/model/criteria.rb, line 65 def criteria self end
each(&block)
click to toggle source
# File lib/opium/model/criteria.rb, line 73 def each(&block) if !block_given? to_enum(:each) elsif cached? && @cache @cache.each(&block) else response = self.model.http_get( query: self.constraints ) @cache = [] if response && response['results'] variables[:total_count] = response['count'] response['results'].each do |attributes| instance = self.model.new( attributes ) @cache << instance if cached? block.call instance end end end end
empty?()
click to toggle source
# File lib/opium/model/criteria.rb, line 61 def empty? count == 0 end
inspect()
click to toggle source
# File lib/opium/model/criteria.rb, line 92 def inspect inspected_constraints = constraints.map {|k, v| [k, v.inspect].join(': ')}.join(', ') inspected_constraints.prepend ' ' if inspected_constraints.size > 0 "#<#{ self.class.name }<#{ model_name }>#{ inspected_constraints }>" end
model()
click to toggle source
# File lib/opium/model/criteria.rb, line 21 def model models[model_name] ||= model_name.constantize end
to_parse()
click to toggle source
# File lib/opium/model/criteria.rb, line 98 def to_parse {}.with_indifferent_access.tap do |result| result[:query] = { where: constraints[:where], className: model_name } if constraints[:where] result[:key] = constraints[:keys] if constraints[:keys] end end
to_partial_path()
click to toggle source
# File lib/opium/model/criteria.rb, line 17 def to_partial_path model._to_partial_path end
total_count()
click to toggle source
# File lib/opium/model/criteria.rb, line 111 def total_count count && variables[:total_count] end
Also aliased as: size
uncache()
click to toggle source
Calls superclass method
Opium::Model::Queryable::ClassMethods#uncache
# File lib/opium/model/criteria.rb, line 105 def uncache super.tap do |criteria| criteria.instance_variable_set(:@cache, nil) end end
update_constraint( constraint, value )
click to toggle source
# File lib/opium/model/criteria.rb, line 33 def update_constraint( constraint, value ) chain.tap {|c| c.update_constraint!( constraint, value )} end
update_constraint!( constraint, value )
click to toggle source
# File lib/opium/model/criteria.rb, line 37 def update_constraint!( constraint, value ) update_hash_value :constraints, constraint, value end
update_variable( variable, value )
click to toggle source
# File lib/opium/model/criteria.rb, line 49 def update_variable( variable, value ) chain.tap {|c| c.update_variable!( variable, value )} end
update_variable!( variable, value )
click to toggle source
# File lib/opium/model/criteria.rb, line 53 def update_variable!( variable, value ) update_hash_value :variables, variable, value end
variables()
click to toggle source
# File lib/opium/model/criteria.rb, line 45 def variables @variables ||= {}.with_indifferent_access end
variables?()
click to toggle source
# File lib/opium/model/criteria.rb, line 57 def variables? !variables.empty? end
Private Instance Methods
update_hash_value( hash_name, key, value )
click to toggle source
# File lib/opium/model/criteria.rb, line 120 def update_hash_value( hash_name, key, value ) hash = self.send( hash_name ) if hash[key].nil? || !value.is_a?(Hash) hash[key] = value elsif hash[key].is_a?(Hash) || value.is_a?(Hash) hash[key].deep_merge!( value ) end end