Module: Mobility::Backend::OrmDelegator

Included in:
Column, Hstore, Jsonb, KeyValue, Serialized, Table
Defined in:
lib/mobility/backend/orm_delegator.rb

Overview

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

Examples:

KeyValue backend for AR model

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



28
29
30
# File 'lib/mobility/backend/orm_delegator.rb', line 28

def self.included(base)
  base.extend(self)
end

Instance Method Details

#for(model_class) ⇒ Class

Returns Class of backend to use for model.

Parameters:

  • model_class (Class)

    Class of model

Returns:

  • (Class)

    Class of backend to use for model



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

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