Sha256: d81cc83a0edfff281287008983c2af7bd8fb58b29e0de2d27137d55574585096

Contents?: true

Size: 1.75 KB

Versions: 16

Compression:

Stored size: 1.75 KB

Contents

module Vedeu

  # 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::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 # Vedeu

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
vedeu-0.5.6 lib/vedeu/repositories/registerable.rb
vedeu-0.5.5 lib/vedeu/repositories/registerable.rb
vedeu-0.5.4 lib/vedeu/repositories/registerable.rb
vedeu-0.5.3 lib/vedeu/repositories/registerable.rb
vedeu-0.5.2 lib/vedeu/repositories/registerable.rb
vedeu-0.5.1 lib/vedeu/repositories/registerable.rb
vedeu-0.5.0 lib/vedeu/repositories/registerable.rb
vedeu-0.4.65 lib/vedeu/repositories/registerable.rb
vedeu-0.4.64 lib/vedeu/repositories/registerable.rb
vedeu-0.4.63 lib/vedeu/repositories/registerable.rb
vedeu-0.4.62 lib/vedeu/repositories/registerable.rb
vedeu-0.4.61 lib/vedeu/repositories/registerable.rb
vedeu-0.4.60 lib/vedeu/repositories/registerable.rb
vedeu-0.4.59 lib/vedeu/repositories/registerable.rb
vedeu-0.4.58 lib/vedeu/repositories/registerable.rb
vedeu-0.4.57 lib/vedeu/repositories/registerable.rb