Class: SpeedyAF::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/speedy_af/base.rb
Defined Under Namespace
Classes: NotAvailable
Constant Summary
collapse
- SOLR_ALL =
10_000_000
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(solr_document, instance_defaults = {}) ⇒ Base
Returns a new instance of Base.
167
168
169
170
171
172
173
174
175
|
# File 'lib/speedy_af/base.rb', line 167
def initialize(solr_document, instance_defaults = {})
instance_defaults ||= {}
@model = Base.model_for(solr_document)
@attrs = self.class.defaults.merge(instance_defaults)
solr_document.each_pair do |k, v|
attr_name, value = parse_solr_field(k, v)
@attrs[attr_name.to_sym] = value
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/speedy_af/base.rb', line 207
def method_missing(sym, *args)
return real_object.send(sym, *args) if real?
if @attrs.key?(sym)
@attrs[sym] = SpeedyAF::Base.for(@attrs[sym]) if @attrs[sym].is_a?(ActiveFedora::SolrHit)
@attrs[sym] = @attrs[sym].map { |doc| SpeedyAF::Base.for(doc) } if @attrs[sym].is_a?(Array) && @attrs[sym].all? { |d| d.is_a?(ActiveFedora::SolrHit) }
return @attrs[sym]
end
reflection = reflection_for(sym)
unless reflection.nil?
begin
return load_from_reflection(reflection, sym.to_s =~ /_ids?$/)
rescue NotAvailable => e
ActiveFedora::Base.logger.warn(e.message)
end
end
if model.instance_methods.include?(sym)
ActiveFedora::Base.logger.warn("Reifying #{model} because #{sym} called from #{caller.first}")
return real_object.send(sym, *args)
end
super
end
|
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
15
16
17
|
# File 'lib/speedy_af/base.rb', line 15
def attrs
@attrs
end
|
#model ⇒ Object
Returns the value of attribute model.
15
16
17
|
# File 'lib/speedy_af/base.rb', line 15
def model
@model
end
|
Class Method Details
.config(model, &block) ⇒ Object
40
41
42
43
|
# File 'lib/speedy_af/base.rb', line 40
def config(model, &block)
proxy_class = proxy_class_for(model) { Class.new(self) }
proxy_class.class_eval(&block) if block_given?
end
|
.defaults ⇒ Object
18
19
20
|
# File 'lib/speedy_af/base.rb', line 18
def defaults
@defaults ||= {}
end
|
.defaults=(value) ⇒ Object
22
23
24
25
|
# File 'lib/speedy_af/base.rb', line 22
def defaults=(value)
raise ArgumentError unless value.respond_to?(:merge)
@defaults = value
end
|
.find(id, opts = {}) ⇒ Object
45
46
47
48
49
|
# File 'lib/speedy_af/base.rb', line 45
def find(id, opts = {})
result = where(%(id:"#{id}"), opts.merge(rows: 1)).first
raise SpeedyAF::ModelMismatch if self != result.class && self != SpeedyAF::Base
result
end
|
.for(doc, opts = {}) ⇒ Object
56
57
58
59
|
# File 'lib/speedy_af/base.rb', line 56
def for(doc, opts = {})
proxy = proxy_class_for(model_for(doc))
proxy.new(doc, opts[:defaults])
end
|
.from(docs, opts = {}) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/speedy_af/base.rb', line 61
def from(docs, opts = {})
hash = docs.each_with_object({}) do |doc, h|
h[doc['id']] = self.for(doc, opts)
end
if opts[:load_reflections]
reflections_hash = gather_reflections(hash, opts)
reflections_hash.each { |parent_id, reflections| hash[parent_id].attrs.merge!(reflections) }
end
return hash.values if opts[:order].nil?
opts[:order].call.collect { |id| hash[id] }.to_a
end
|
.model_for(solr_document) ⇒ Object
75
76
77
|
# File 'lib/speedy_af/base.rb', line 75
def model_for(solr_document)
solr_document[:has_model_ssim].first.safe_constantize
end
|
.proxy_class_for(model) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/speedy_af/base.rb', line 27
def proxy_class_for(model)
klass = "::SpeedyAF::Proxy::#{model.name}".safe_constantize
if klass.nil?
namespace = model.name.deconstantize
name = model.name.demodulize
klass_module = namespace.split(/::/).inject(::SpeedyAF::Proxy) do |mod, ns|
mod.const_defined?(ns, false) ? mod.const_get(ns, false) : mod.const_set(ns, Module.new)
end
klass = klass_module.const_set(name, Class.new(self))
end
klass
end
|
.where(query, opts = {}) ⇒ Object
51
52
53
54
|
# File 'lib/speedy_af/base.rb', line 51
def where(query, opts = {})
docs = ActiveFedora::SolrService.query(query, rows: opts[:rows] || SOLR_ALL)
from(docs, opts)
end
|
Instance Method Details
#belongs_to_reflections(only: nil) ⇒ Object
rubocop:enable Naming/PredicateName
252
253
254
255
256
257
258
259
|
# File 'lib/speedy_af/base.rb', line 252
def belongs_to_reflections(only: nil)
SpeedyAF::Base.model_reflections[:belongs_to][model] ||= model.reflections.select { |_name, reflection| reflection.belongs_to? && reflection.respond_to?(:predicate_for_solr) }
if only.is_a?(Array) && only.present?
SpeedyAF::Base.model_reflections[:belongs_to][model].select { |name, _reflection| only.include? name }
else
SpeedyAF::Base.model_reflections[:belongs_to][model]
end
end
|
#has_many_reflections(only: nil) ⇒ Object
rubocop:disable Naming/PredicateName
242
243
244
245
246
247
248
249
|
# File 'lib/speedy_af/base.rb', line 242
def has_many_reflections(only: nil)
SpeedyAF::Base.model_reflections[:has_many][model] ||= model.reflections.select { |_name, reflection| reflection.has_many? && reflection.respond_to?(:predicate_for_solr) }
if only.is_a?(Array) && only.present?
SpeedyAF::Base.model_reflections[:has_many][model].select { |name, _reflection| only.include? name }
else
SpeedyAF::Base.model_reflections[:has_many][model]
end
end
|
#real? ⇒ Boolean
185
186
187
|
# File 'lib/speedy_af/base.rb', line 185
def real?
!@real_object.nil?
end
|
#real_object ⇒ Object
177
178
179
180
181
182
183
|
# File 'lib/speedy_af/base.rb', line 177
def real_object
if @real_object.nil?
@real_object = model.find(id)
@attrs.clear
end
@real_object
end
|
#reload ⇒ Object
189
190
191
192
193
194
195
|
# File 'lib/speedy_af/base.rb', line 189
def reload
dup = Base.find(id)
@attrs = dup.attrs
@model = dup.model
@real_object = nil
self
end
|
#respond_to_missing?(sym, _include_private = false) ⇒ Boolean
201
202
203
204
205
|
# File 'lib/speedy_af/base.rb', line 201
def respond_to_missing?(sym, _include_private = false)
@attrs.key?(sym) ||
model.respond_to?(:reflections) && model.reflections[sym].present? ||
model.instance_methods.include?(sym)
end
|
#subresource_reflections(only: nil) ⇒ Object
232
233
234
235
236
237
238
239
|
# File 'lib/speedy_af/base.rb', line 232
def subresource_reflections(only: nil)
SpeedyAF::Base.model_reflections[:subresource][model] ||= model.reflections.select { |_name, reflection| reflection.is_a? ActiveFedora::Reflection::HasSubresourceReflection }
if only.is_a?(Array) && only.present?
SpeedyAF::Base.model_reflections[:subresource][model].select { |name, _reflection| only.include? name }
else
SpeedyAF::Base.model_reflections[:subresource][model]
end
end
|
#to_query(key) ⇒ Object
197
198
199
|
# File 'lib/speedy_af/base.rb', line 197
def to_query(key)
"#{key}=#{id}"
end
|