Sha256: 6fb25eb8f93afa36cf9e66c8154efb2f8be5832c88b9bafac327d426bb0d0fe8

Contents?: true

Size: 1.95 KB

Versions: 16

Compression:

Stored size: 1.95 KB

Contents

module Trestle
  class Resource
    module AdapterMethods
      extend ActiveSupport::Concern

      # Adapter instance bound to the current resource's context.
      def adapter
        @adapter ||= adapter_class.new(self, @context)
      end

      module ClassMethods
        # Declares a method that is handled by the admin's adapter class.
        def adapter_method(name)
          delegate name, to: :adapter

          singleton_class.class_eval do
            delegate name, to: :adapter
          end
        end

        # Defines an admin-specific adapter method.
        #
        # The given block is wrapped rather than passed to #define_method directly, so that
        # adapter methods can be defined with incomplete block parameters. Unfortunately
        # this means we lose the ability to call super from within a custom adapter method.
        def define_adapter_method(name, &block)
          return unless block_given?

          adapter_methods.module_eval do
            define_method(name) do |*args|
              instance_exec(*args, &block)
            end
          end
        end

        # Returns the adapter class for this admin.
        #
        # Defaults to a subclass of `Trestle.config.default_adapter` with
        # the admin-specific adapter methods module included.
        def adapter_class
          @adapter_class ||= Class.new(Trestle.config.default_adapter).include(adapter_methods)
        end

        # Sets an explicit adapter class for this admin.
        # A subclass is created with the admin-specific adapter methods module included.
        def adapter_class=(klass)
          @adapter_class = Class.new(klass).include(adapter_methods)
        end

        # Module container for admin-specific adapter methods.
        def adapter_methods
          @adapter_methods ||= Module.new
        end

        # Unbound instance of adapter.
        def adapter
          @adapter ||= adapter_class.new(self)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
trestle-0.10.1 lib/trestle/resource/adapter_methods.rb
trestle-0.10.0 lib/trestle/resource/adapter_methods.rb
trestle-0.10.0.pre2 lib/trestle/resource/adapter_methods.rb
trestle-0.10.0.pre lib/trestle/resource/adapter_methods.rb
trestle-0.9.8 lib/trestle/resource/adapter_methods.rb
trestle-0.9.7 lib/trestle/resource/adapter_methods.rb
trestle-0.9.6 lib/trestle/resource/adapter_methods.rb
trestle-0.9.5 lib/trestle/resource/adapter_methods.rb
trestle-0.9.4 lib/trestle/resource/adapter_methods.rb
trestle-0.9.3 lib/trestle/resource/adapter_methods.rb
trestle-0.9.2 lib/trestle/resource/adapter_methods.rb
trestle-0.9.1 lib/trestle/resource/adapter_methods.rb
trestle-0.9.0 lib/trestle/resource/adapter_methods.rb
trestle-0.8.13 lib/trestle/resource/adapter_methods.rb
trestle-0.8.12 lib/trestle/resource/adapter_methods.rb
trestle-0.8.11 lib/trestle/resource/adapter_methods.rb