Sha256: f1562c25b126695ddbe77e4849f448b4e2f706bb64ae0809376a2ce7af25f5ea

Contents?: true

Size: 1.31 KB

Versions: 23

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true
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('::')
        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

      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

23 entries across 23 versions & 1 rubygems

Version Path
mobility-0.8.13 lib/mobility/backend/orm_delegator.rb
mobility-0.8.11 lib/mobility/backend/orm_delegator.rb
mobility-0.8.10 lib/mobility/backend/orm_delegator.rb
mobility-0.8.9 lib/mobility/backend/orm_delegator.rb
mobility-0.8.8 lib/mobility/backend/orm_delegator.rb
mobility-0.8.7 lib/mobility/backend/orm_delegator.rb
mobility-0.8.6 lib/mobility/backend/orm_delegator.rb
mobility-0.8.5 lib/mobility/backend/orm_delegator.rb
mobility-0.8.4 lib/mobility/backend/orm_delegator.rb
mobility-0.8.3 lib/mobility/backend/orm_delegator.rb
mobility-0.8.2 lib/mobility/backend/orm_delegator.rb
mobility-0.8.1 lib/mobility/backend/orm_delegator.rb
mobility-0.8.0 lib/mobility/backend/orm_delegator.rb
mobility-0.7.6 lib/mobility/backend/orm_delegator.rb
mobility-0.7.5 lib/mobility/backend/orm_delegator.rb
mobility-0.7.4 lib/mobility/backend/orm_delegator.rb
mobility-0.7.3 lib/mobility/backend/orm_delegator.rb
mobility-0.7.2 lib/mobility/backend/orm_delegator.rb
mobility-0.7.1 lib/mobility/backend/orm_delegator.rb
mobility-0.7.0 lib/mobility/backend/orm_delegator.rb