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.



14
15
16
17
18
19
20
# File 'lib/db/model/where.rb', line 14

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.



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, **options, &block)
	@predicate &= Statement::Predicate.where(*arguments, **options, &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, **options, &block)
	@predicate |= Statement::Predicate.where(*arguments, **options, &block)
	
	return self
end

#selectObject



51
52
53
54
55
# File 'lib/db/model/where.rb', line 51

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

#to_sObject Also known as: inspect



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

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

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



45
46
47
48
49
# File 'lib/db/model/where.rb', line 45

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