Sha256: 81a0a908ab2b10723c21dddad327b4ff01615c7f4503e30c3f201eb0c157bb26

Contents?: true

Size: 1.32 KB

Versions: 15

Compression:

Stored size: 1.32 KB

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::Backends::KeyValue.for(Post)
  #=> Mobility::Backends::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)
        namespace = name.split('::'.freeze)
        if Loaded::ActiveRecord && model_class < ::ActiveRecord::Base
          require_backend("active_record", namespace.last.underscore)
          const_get(namespace.insert(-2, "ActiveRecord".freeze).join("::".freeze))
        elsif Loaded::Sequel && model_class < ::Sequel::Model
          require_backend("sequel", namespace.last.underscore)
          const_get(namespace.insert(-2, "Sequel".freeze).join("::".freeze))
        else
          raise ArgumentError, "#{namespace.last} backend can only be used by ActiveRecord or Sequel models".freeze
        end
      end

      private

      def require_backend(orm, backend)
        begin
          orm_backend = "mobility/backends/#{orm}/#{backend}"
          require orm_backend
        rescue LoadError => e
          raise unless e.message =~ /#{orm_backend}/
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
mobility-0.4.3 lib/mobility/backend/orm_delegator.rb
mobility-0.4.2 lib/mobility/backend/orm_delegator.rb
mobility-0.4.1 lib/mobility/backend/orm_delegator.rb
mobility-0.4.0 lib/mobility/backend/orm_delegator.rb
mobility-0.3.6 lib/mobility/backend/orm_delegator.rb
mobility-0.3.5 lib/mobility/backend/orm_delegator.rb
mobility-0.3.4 lib/mobility/backend/orm_delegator.rb
mobility-0.3.3 lib/mobility/backend/orm_delegator.rb
mobility-0.3.2 lib/mobility/backend/orm_delegator.rb
mobility-0.3.1 lib/mobility/backend/orm_delegator.rb
mobility-0.3.0 lib/mobility/backend/orm_delegator.rb
mobility-0.2.3 lib/mobility/backend/orm_delegator.rb
mobility-0.2.2 lib/mobility/backend/orm_delegator.rb
mobility-0.2.1 lib/mobility/backend/orm_delegator.rb
mobility-0.2.0 lib/mobility/backend/orm_delegator.rb