Module: Mobility::Backend::OrmDelegator

Overview

Adds #for method to backend to return ORM-specific backend.

Examples:

KeyValue backend for AR model

class Post < ActiveRecord::Base
  # ...
end
Mobility::Backends::KeyValue.for(Post)
#=> Mobility::Backends::ActiveRecord::KeyValue

Instance Method Summary collapse

Instance Method Details

#for(model_class) ⇒ Class


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mobility/backend/orm_delegator.rb', line 19

def for(model_class)
  namespace = name.split('::')
  if Loaded::ActiveRecord && model_class < ::ActiveRecord::Base
    require_backend("active_record", namespace.last.underscore)
    const_get(namespace.insert(-2, "ActiveRecord").join("::"))
  elsif Loaded::Sequel && model_class < ::Sequel::Model
    require_backend("sequel", namespace.last.underscore)
    const_get(namespace.insert(-2, "Sequel").join("::"))
  else
    raise ArgumentError, "#{namespace.last} backend can only be used by ActiveRecord or Sequel models"
  end
end