Class: OAI::Provider::ActiveRecordWrapper

Inherits:
Model
  • Object
show all
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

ActiveRecordCachingWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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

Instance Attribute Details

#modelObject (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_fieldObject (readonly)

Returns the value of attribute timestamp_field.



14
15
16
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 14

def timestamp_field
  @timestamp_field
end

Instance Method Details

#deleted?(record) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 69

def deleted?(record)
  if record.respond_to?(:deleted_at)
    return record.deleted_at
  elsif record.respond_to?(:deleted)
    return record.deleted
  end
  false
end

#earliestObject



28
29
30
31
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 28

def earliest
  model.find(:first, 
    :order => "#{timestamp_field} asc").send(timestamp_field)
end

#find(selector, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 50

def find(selector, options={})
  return next_set(options[:resumption_token]) if options[:resumption_token]
  conditions = sql_conditions(options)
  if :all == selector
    total = model.count(:id, :conditions => conditions)
    if(@limit && total > @limit)
      select_partial(ResumptionToken.new(last_id(conditions), options.merge({:last => 0}), nil, total))
    else
      model.find(:all, :conditions => conditions)
    end
  else
    begin
      model.find(selector, :conditions => conditions)
    rescue ActiveRecord::RecordNotFound
      raise OAI::IdException.new
    end
  end
end

#last_id(conditions) ⇒ Object



38
39
40
41
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 38

def last_id(conditions)
  model.find(:first, :conditions => conditions,
    :order => "#{timestamp_field} desc").id
end

#latestObject



33
34
35
36
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 33

def latest
  model.find(:first, 
    :order => "#{timestamp_field} desc").send(timestamp_field)
end

#setsObject

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.



46
47
48
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 46

def sets
  model.sets if model.respond_to?(:sets)
end