Class: DB::Model::Where

Inherits:
Object
  • Object
show all
Includes:
Countable, Deletable
Defined in:
lib/db/model/where.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Countable

#count, #empty?

Methods included from Deletable

#delete

Constructor Details

#initialize(context, model, *arguments, **options, &block) ⇒ Where

Returns a new instance of Where.



31
32
33
34
35
36
37
# File 'lib/db/model/where.rb', line 31

def initialize(context, model, *arguments, **options, &block)
  @context = context
  @model = model
  @predicate = Statement::Predicate.where(*arguments, **options, &block)
  
  @select = nil
end

Instance Attribute Details

#predicateObject

Returns the value of attribute predicate.



39
40
41
# File 'lib/db/model/where.rb', line 39

def predicate
  @predicate
end

Instance Method Details

#and(*arguments, **options, &block) ⇒ Object



49
50
51
52
53
# File 'lib/db/model/where.rb', line 49

def and(*arguments, **options, &block)
  @predicate &= Statement::Predicate.where(*arguments, **options, &block)
  
  return self
end

#each(&block) ⇒ Object



74
75
76
# File 'lib/db/model/where.rb', line 74

def each(&block)
  self.select.each(@context, &block)
end

#find(*key) ⇒ Object



55
56
57
58
59
60
# File 'lib/db/model/where.rb', line 55

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



78
79
80
81
82
83
84
# File 'lib/db/model/where.rb', line 78

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



43
44
45
46
47
# File 'lib/db/model/where.rb', line 43

def or(*arguments, **options, &block)
  @predicate |= Statement::Predicate.where(*arguments, **options, &block)
  
  return self
end

#selectObject



68
69
70
71
72
# File 'lib/db/model/where.rb', line 68

def select
  @select ||= Statement::Select.new(@model,
    where: @predicate
  )
end

#to_sObject Also known as: inspect



86
87
88
# File 'lib/db/model/where.rb', line 86

def to_s
  "\#<#{self.class} #{@model} #{@predicate}>"
end

#where(*arguments, **options, &block) ⇒ Object



62
63
64
65
66
# File 'lib/db/model/where.rb', line 62

def where(*arguments, **options, &block)
  self.class.new(@context, model,
    @predicate + Statement::Predicate.where(*arguments, **options, &block)
  )
end