Sha256: 7a7e4cd2cf5f83616d6ae82c6620020102e644a1608374814caaaa0ea05cba7b

Contents?: true

Size: 1.93 KB

Versions: 47

Compression:

Stored size: 1.93 KB

Contents

module Vedeu

  module Repositories

    # Repositories contain registerables, this module provides
    # convenience methods for them.
    #
    module Registerable

      # These class methods are mixed into the repository.
      module ClassMethods

        # The null model is used when the repository cannot be found.
        #
        # @param klass [Class]
        # @return [Symbol]
        def null(klass)
          define_method(:null_model) { klass }
        end

        # The real model is the usual model to use for a given
        # repository.
        #
        # @param klass [Class]
        # @return [Symbol]
        def real(klass)
          define_method(:model) { instance_variable_set('@model', klass) }
        end

        # Register a repository for storing models.
        #
        # @param model [Class]
        # @param storage [Class|Hash]
        # @return [Vedeu::Repositories::Repository]
        def register(model = nil, storage = {})
          new(model, storage).tap do |klass|
            Vedeu::Repositories.register(klass.repository)
          end
        end

        # Returns the repositories registered.
        #
        # @note
        #   If the repository is 'Geometries', for example, then
        #   @models will be either an empty Geometries repository or
        #   the collection of stored models.
        #
        # @return [void]
        def repository
          @models ||= reset!
        end

        # Remove all stored models from the repository.
        #
        # @return [void]
        def reset!
          @models = register
        end
        alias_method :reset, :reset!

      end # ClassMethods

      # When this module is included in a class, provide ClassMethods
      # as class methods for the class.
      #
      # @param klass [Class]
      # @return [void]
      def self.included(klass)
        klass.send(:extend, ClassMethods)
      end

    end # Registerable

  end # Repositories

end # Vedeu

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
vedeu-0.6.58 lib/vedeu/repositories/registerable.rb
vedeu-0.6.57 lib/vedeu/repositories/registerable.rb
vedeu-0.6.56 lib/vedeu/repositories/registerable.rb
vedeu-0.6.54 lib/vedeu/repositories/registerable.rb
vedeu-0.6.53 lib/vedeu/repositories/registerable.rb
vedeu-0.6.52 lib/vedeu/repositories/registerable.rb
vedeu-0.6.51 lib/vedeu/repositories/registerable.rb
vedeu-0.6.50 lib/vedeu/repositories/registerable.rb
vedeu-0.6.49 lib/vedeu/repositories/registerable.rb
vedeu-0.6.48 lib/vedeu/repositories/registerable.rb
vedeu-0.6.47 lib/vedeu/repositories/registerable.rb
vedeu-0.6.46 lib/vedeu/repositories/registerable.rb
vedeu-0.6.45 lib/vedeu/repositories/registerable.rb
vedeu-0.6.44 lib/vedeu/repositories/registerable.rb
vedeu-0.6.43 lib/vedeu/repositories/registerable.rb
vedeu-0.6.42 lib/vedeu/repositories/registerable.rb
vedeu-0.6.41 lib/vedeu/repositories/registerable.rb
vedeu-0.6.40 lib/vedeu/repositories/registerable.rb
vedeu-0.6.39 lib/vedeu/repositories/registerable.rb
vedeu-0.6.38 lib/vedeu/repositories/registerable.rb