Module: Querii::QueryObject::ClassMethods
- Defined in:
- lib/querii/query_object.rb
Instance Attribute Summary collapse
-
#model_name ⇒ Object
optionally override the default determined by top level namespace.
Instance Method Summary collapse
- #call(*args, relation: base_class.all, **kargs) ⇒ Object (also: #all)
- #default_scope(scope = nil) ⇒ Object
- #scope(name, body, &block) ⇒ Object
Instance Attribute Details
#model_name ⇒ Object
optionally override the default determined by top level namespace
15 16 17 |
# File 'lib/querii/query_object.rb', line 15 def model_name @model_name end |
Instance Method Details
#call(*args, relation: base_class.all, **kargs) ⇒ Object Also known as: all
17 18 19 |
# File 'lib/querii/query_object.rb', line 17 def call(*args, relation: base_class.all, **kargs) relation.extending(self::Scopes).default(*args, **kargs) end |
#default_scope(scope = nil) ⇒ Object
41 42 43 44 |
# File 'lib/querii/query_object.rb', line 41 def default_scope(scope = nil) scope = Proc.new if block_given? scope(:default, scope) end |
#scope(name, body, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/querii/query_object.rb', line 23 def scope(name, body, &block) unless body.respond_to?(:call) raise ArgumentError, 'The scope body needs to be callable.' end self::Scopes.send(:define_method, name) do |*args, **kargs| # unfortunately empty double splatted kargs still count as arguments # for a lambda's argument arity checking if kargs.empty? scope = all.scoping { instance_exec(*args, &body) } else scope = all.scoping { instance_exec(*args, **kargs, &body) } end scope || all end end |