Class: DB::Model::Relation
- Inherits:
-
Object
- Object
- DB::Model::Relation
- Defined in:
- lib/db/model/relation.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #cache_key ⇒ Object
- #create(**attributes) ⇒ Object
- #each(cache: @cache, &block) ⇒ Object
- #find(*key) ⇒ Object
- #first(count = nil) ⇒ Object
-
#initialize(context, model, cache = nil) ⇒ Relation
constructor
A new instance of Relation.
- #insert(keys, rows, **attributes) ⇒ Object
- #inspect ⇒ Object
- #new(**attributes) ⇒ Object
- #predicate ⇒ Object
- #preload(name) ⇒ Object
- #select ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #update_cache(result) ⇒ Object
- #where(*arguments, **options, &block) ⇒ Object
Methods included from Countable
Methods included from Deletable
Constructor Details
#initialize(context, model, cache = nil) ⇒ Relation
Returns a new instance of Relation.
12 13 14 15 16 17 18 |
# File 'lib/db/model/relation.rb', line 12 def initialize(context, model, cache = nil) @context = context @model = model @cache = cache @select = nil end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
22 23 24 |
# File 'lib/db/model/relation.rb', line 22 def cache @cache end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
20 21 22 |
# File 'lib/db/model/relation.rb', line 20 def context @context end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
21 22 23 |
# File 'lib/db/model/relation.rb', line 21 def model @model end |
Instance Method Details
#cache_key ⇒ Object
122 123 124 |
# File 'lib/db/model/relation.rb', line 122 def cache_key [@model, self.predicate] end |
#create(**attributes) ⇒ Object
26 27 28 |
# File 'lib/db/model/relation.rb', line 26 def create(**attributes) self.new(**attributes).save end |
#each(cache: @cache, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/db/model/relation.rb', line 94 def each(cache: @cache, &block) if @cache @cache.fetch(self.cache_key) do self.select.to_a(@context, @cache) end.each(&block) else self.select.each(@context, &block) end end |
#find(*key) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/db/model/relation.rb', line 38 def find(*key) if predicate = self.predicate predicate = predicate & @model.find_predicate(*key) else predicate = @model.find_predicate(*key) end return Statement::Select.new(@model, where: predicate, limit: Statement::Limit::ONE ).to_a(@context).first end |
#first(count = nil) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/db/model/relation.rb', line 104 def first(count = nil) if count self.select.first(@context, count, @cache) else self.select.first(@context, 1, @cache).first end end |
#insert(keys, rows, **attributes) ⇒ Object
34 35 36 |
# File 'lib/db/model/relation.rb', line 34 def insert(keys, rows, **attributes) @model.insert(@context, keys, rows, **attributes) end |
#inspect ⇒ Object
138 139 140 |
# File 'lib/db/model/relation.rb', line 138 def inspect to_s end |
#new(**attributes) ⇒ Object
30 31 32 |
# File 'lib/db/model/relation.rb', line 30 def new(**attributes) @model.new(@context, {}, @cache).assign(attributes) end |
#predicate ⇒ Object
61 62 63 |
# File 'lib/db/model/relation.rb', line 61 def predicate nil end |
#preload(name) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/db/model/relation.rb', line 65 def preload(name) @cache ||= Cache.new scopes = [] self.each do |record| scopes << record.send(name) end # Build a buffer of queries: query = @context.query first = true scopes.each do |scope| query.clause(";") unless first first = false scope.select.append_to(query) end query.call do |connection| scopes.each do |scope| result = connection.next_result scope.update_cache(result) end end return self end |
#select ⇒ Object
130 131 132 |
# File 'lib/db/model/relation.rb', line 130 def select @select ||= Statement::Select.new(@model, where: self.predicate) end |
#to_a ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/db/model/relation.rb', line 112 def to_a records = [] self.each do |record| records << record end return records end |
#to_s ⇒ Object
134 135 136 |
# File 'lib/db/model/relation.rb', line 134 def to_s "\#<#{self.class} #{@model}>" end |
#update_cache(result) ⇒ Object
126 127 128 |
# File 'lib/db/model/relation.rb', line 126 def update_cache(result) @cache.update(self.cache_key, self.select.apply(@context, result)) end |
#where(*arguments, **options, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/db/model/relation.rb', line 51 def where(*arguments, **, &block) where = @model.where(@context, *arguments, **, &block) if predicate = self.predicate where.predicate &= predicate end return where end |