Class: OAI::Provider::ActiveRecordWrapper
- Inherits:
-
Model
- Object
- Model
- OAI::Provider::ActiveRecordWrapper
show all
- Defined in:
- lib/oai/provider/model/activerecord_wrapper.rb
Overview
This class wraps an ActiveRecord model and delegates all of the record selection/retrieval to the AR model. It accepts options for specifying the update timestamp field, a timeout, and a limit. The limit option is used for doing pagination with resumption tokens. The expiration timeout is ignored, since all necessary information is encoded in the token.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Model
#about
Constructor Details
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 15
def initialize(model, options={})
@model = model
@timestamp_field = options.delete(:timestamp_field) || 'updated_at'
@limit = options.delete(:limit)
unless options.empty?
raise ArgumentError.new(
"Unsupported options [#{options.keys.join(', ')}]"
)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 78
def method_missing(m, *args, &block)
if m =~ /^map_/
model.send(m, *args, &block)
else
super
end
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
13
14
15
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 13
def model
@model
end
|
#timestamp_field ⇒ Object
Returns the value of attribute timestamp_field.
13
14
15
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 13
def timestamp_field
@timestamp_field
end
|
Instance Method Details
#deleted?(record) ⇒ Boolean
61
62
63
64
65
66
67
68
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 61
def deleted?(record)
if record.respond_to?(:deleted_at)
return record.deleted_at
elsif record.respond_to?(:deleted)
return record.deleted
end
false
end
|
#earliest ⇒ Object
27
28
29
30
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 27
def earliest
earliest_obj = model.find(:first, :order => "#{timestamp_field} asc")
earliest_obj.nil? ? Time.at(0) : earliest_obj.send(timestamp_field)
end
|
#find(selector, options = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 43
def find(selector, options={})
find_scope = find_scope(options)
return next_set(find_scope,
options[:resumption_token]) if options[:resumption_token]
conditions = sql_conditions(options)
if :all == selector
total = find_scope.count(:id, :conditions => conditions)
if @limit && total > @limit
select_partial(find_scope,
ResumptionToken.new(options.merge({:last => 0})))
else
find_scope.find(:all, :conditions => conditions)
end
else
find_scope.find(selector, :conditions => conditions)
end
end
|
#latest ⇒ Object
32
33
34
35
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 32
def latest
latest_obj = model.find(:first, :order => "#{timestamp_field} desc")
latest_obj.nil? ? Time.now : latest_obj.send(timestamp_field)
end
|
#respond_to?(m, *args) ⇒ Boolean
70
71
72
73
74
75
76
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 70
def respond_to?(m, *args)
if m =~ /^map_/
model.respond_to?(m, *args)
else
super
end
end
|
#sets ⇒ Object
A model class is expected to provide a method Model.sets that returns all the sets the model supports. See the activerecord_provider tests for an example.
39
40
41
|
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 39
def sets
model.sets if model.respond_to?(:sets)
end
|