Class: DB::Model::Where
- Inherits:
-
Object
- Object
- DB::Model::Where
- Defined in:
- lib/db/model/where.rb
Instance Attribute Summary collapse
-
#predicate ⇒ Object
Returns the value of attribute predicate.
Instance Method Summary collapse
- #and(*arguments, **options, &block) ⇒ Object
- #each(&block) ⇒ Object
- #find(*key) ⇒ Object
- #first(count = nil) ⇒ Object
-
#initialize(context, model, *arguments, **options, &block) ⇒ Where
constructor
A new instance of Where.
- #or(*arguments, **options, &block) ⇒ Object
- #select ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #where(*arguments, **options, &block) ⇒ Object
Methods included from Countable
Methods included from Deletable
Constructor Details
#initialize(context, model, *arguments, **options, &block) ⇒ Where
Returns a new instance of Where.
14 15 16 17 18 19 20 |
# File 'lib/db/model/where.rb', line 14 def initialize(context, model, *arguments, **, &block) @context = context @model = model @predicate = Statement::Predicate.where(*arguments, **, &block) @select = nil end |
Instance Attribute Details
#predicate ⇒ Object
Returns the value of attribute predicate.
22 23 24 |
# File 'lib/db/model/where.rb', line 22 def predicate @predicate end |
Instance Method Details
#and(*arguments, **options, &block) ⇒ Object
32 33 34 35 36 |
# File 'lib/db/model/where.rb', line 32 def and(*arguments, **, &block) @predicate &= Statement::Predicate.where(*arguments, **, &block) return self end |
#each(&block) ⇒ Object
57 58 59 |
# File 'lib/db/model/where.rb', line 57 def each(&block) self.select.each(@context, &block) end |
#find(*key) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/db/model/where.rb', line 38 def find(*key) return Statement::Select.new(@model, where: @predicate & @model.find_predicate(*key), limit: Statement::Limit::ONE ).to_a(context) end |
#first(count = nil) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/db/model/where.rb', line 61 def first(count = nil) if count self.select.first(@context, count) else self.select.first(@context, 1).first end end |
#or(*arguments, **options, &block) ⇒ Object
26 27 28 29 30 |
# File 'lib/db/model/where.rb', line 26 def or(*arguments, **, &block) @predicate |= Statement::Predicate.where(*arguments, **, &block) return self end |
#select ⇒ Object
51 52 53 54 55 |
# File 'lib/db/model/where.rb', line 51 def select @select ||= Statement::Select.new(@model, where: @predicate ) end |
#to_s ⇒ Object Also known as: inspect
69 70 71 |
# File 'lib/db/model/where.rb', line 69 def to_s "\#<#{self.class} #{@model} #{@predicate}>" end |