Sha256: 0a19ca14c2363cbb6dbfb9643b95749cbf021c8eec4bc31cf20a8067c06fc456

Contents?: true

Size: 960 Bytes

Versions: 11

Compression:

Stored size: 960 Bytes

Contents

module Mobility
  module Backend
=begin

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

@example KeyValue backend for AR model
  class Post < ActiveRecord::Base
    # ...
  end
  Mobility::Backend::KeyValue.for(Post)
  #=> Mobility::Backend::ActiveRecord::KeyValue

=end
    module OrmDelegator
      # @param [Class] model_class Class of model
      # @return [Class] Class of backend to use for model
      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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mobility-0.1.12 lib/mobility/backend/orm_delegator.rb
mobility-0.1.11 lib/mobility/backend/orm_delegator.rb
mobility-0.1.10 lib/mobility/backend/orm_delegator.rb
mobility-0.1.7 lib/mobility/backend/orm_delegator.rb
mobility-0.1.6 lib/mobility/backend/orm_delegator.rb
mobility-0.1.5 lib/mobility/backend/orm_delegator.rb
mobility-0.1.4 lib/mobility/backend/orm_delegator.rb
mobility-0.1.3 lib/mobility/backend/orm_delegator.rb
mobility-0.1.2 lib/mobility/backend/orm_delegator.rb
mobility-0.1.1 lib/mobility/backend/orm_delegator.rb
mobility-0.1.0 lib/mobility/backend/orm_delegator.rb