Class: OAI::Provider::ActiveRecordWrapper
- Defined in:
- lib/oai/provider/model/activerecord_wrapper.rb
Overview
OAI::Provider::ActiveRecordWrapper
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#timestamp_field ⇒ Object
readonly
Returns the value of attribute timestamp_field.
Instance Method Summary collapse
- #deleted?(record) ⇒ Boolean
- #earliest ⇒ Object
- #find(selector, options = {}) ⇒ Object
-
#initialize(model, options = {}) ⇒ ActiveRecordWrapper
constructor
A new instance of ActiveRecordWrapper.
- #latest ⇒ Object
- #method_missing(m, *args, &block) ⇒ Object
- #respond_to?(m, *args) ⇒ Boolean
-
#sets ⇒ Object
A model class is expected to provide a method Model.sets that returns all the sets the model supports.
Methods inherited from Model
Constructor Details
#initialize(model, options = {}) ⇒ ActiveRecordWrapper
Returns a new instance of ActiveRecordWrapper.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 16 def initialize(model, ={}) @model = model @timestamp_field = .delete(:timestamp_field) || 'updated_at' @limit = .delete(:limit) unless .empty? raise ArgumentError.new( "Unsupported options [#{.keys.join(', ')}]" ) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 76 def method_missing(m, *args, &block) if m =~ /^map_/ model.send(m, *args, &block) else super end end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
14 15 16 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 14 def model @model end |
#timestamp_field ⇒ Object (readonly)
Returns the value of attribute timestamp_field.
14 15 16 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 14 def @timestamp_field end |
Instance Method Details
#deleted?(record) ⇒ Boolean
59 60 61 62 63 64 65 66 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 59 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
28 29 30 31 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 28 def earliest model.find(:first, :order => "#{} asc").send() end |
#find(selector, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 44 def find(selector, ={}) return next_set([:resumption_token]) if [:resumption_token] conditions = sql_conditions() if :all == selector total = model.count(:id, :conditions => conditions) if @limit && total > @limit select_partial(ResumptionToken.new(.merge({:last => 0}))) else model.find(:all, :conditions => conditions) end else model.find(selector, :conditions => conditions) end end |
#latest ⇒ Object
33 34 35 36 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 33 def latest model.find(:first, :order => "#{} desc").send() end |
#respond_to?(m, *args) ⇒ Boolean
68 69 70 71 72 73 74 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 68 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.
40 41 42 |
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 40 def sets model.sets if model.respond_to?(:sets) end |