Class: OAI::Provider::ActiveRecordWrapper

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

Direct Known Subclasses

ActiveRecordCachingWrapper

Instance Attribute Summary collapse

Attributes inherited from Model

#limit

Instance Method Summary collapse

Methods inherited from Model

#about

Constructor Details

#initialize(model, options = {}) ⇒ ActiveRecordWrapper

If custom ‘timestamp_field’ is used, be aware this will be an ActiveRecord attribute that we will limit on, so perhaps should be indexe appropriately.

If custom ‘identifier_field` is used, be aware this will be an ActiveRecord attribute that we will sort on, and use in WHERE clauses with `=` as well as greater than/less than, so should be indexed appropriately.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 21

def initialize(model, options={})
  @model = model
  @timestamp_field = options.delete(:timestamp_field) || 'updated_at'
  @identifier_field = options.delete(:identifier_field) || model.primary_key || "id"
  @limit = options.delete(:limit) || 100

  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



85
86
87
88
89
90
91
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 85

def method_missing(m, *args, &block)
  if m =~ /^map_/
    model.send(m, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#identifier_fieldObject (readonly)

Returns the value of attribute identifier_field.



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

def identifier_field
  @identifier_field
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#timestamp_fieldObject (readonly)

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

Returns:

  • (Boolean)


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

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



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

def earliest
  earliest_obj = model.order("#{model.base_class.table_name}.#{timestamp_field} asc").first
  earliest_obj.nil? ? Time.at(0) : earliest_obj.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
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 50

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.where(conditions).count
    if @limit && total > @limit
      select_partial(find_scope,
        ResumptionToken.new(options.merge({:last => 0})))
    else
      find_scope.where(conditions)
    end
  else
    find_scope.where(conditions).where(identifier_field => selector).first
  end
end

#latestObject



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

def latest
  latest_obj = model.order("#{model.base_class.table_name}.#{timestamp_field} desc").first
  latest_obj.nil? ? Time.now : latest_obj.send(timestamp_field)
end

#respond_to?(m, *args) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/oai/provider/model/activerecord_wrapper.rb', line 77

def respond_to?(m, *args)
  if m =~ /^map_/
    model.respond_to?(m, *args)
  else
    super
  end
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