Sha256: 5646c9dd2323d41606c1dad599f424465cc9d71ad4a2edc499a62067b0fabf45

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

require "mixture/extensions/attributable"
require "mixture/extensions/coercable"
require "mixture/extensions/hashable"
require "mixture/extensions/validatable"

module Mixture
  # All of the extensions of mixture.  Handles registration of
  # extensions, so that extensions can be referend by a name instead
  # of the constant.
  module Extensions
    # Registers an extension with the module.  This maps a name to
    # an extension.  Extensions may be registered multiple times,
    # with different names.
    #
    # @param name [Symbol] The name of the extension to register.
    # @param extension [Module] The module to register.
    def self.register(name, extension)
      extensions[name.to_s.downcase.intern] = extension
    end

    # Loads an extension with the given name.  If it cannot find the
    # matching extension, it raises a `KeyError`.
    #
    # @param name [Symbol] The name of the extension.
    # @return [Module] The corresponding extension.
    # @raise [KeyError]
    def self.[](name)
      extensions.fetch(name)
    end

    # The extensions that are registered with this module.
    #
    # @return [Hash{Symbol => Module}]
    def self.extensions
      @_extensions ||= {}
    end

    # Finalizes the extension module.  It registers the extensions in
    # Mixture.
    #
    # @return [void]
    def self.finalize
      register :attribute, Attributable
      register :coerce, Coercable
      register :hash, Hashable
      register :validate, Validatable
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mixture-0.7.1 lib/mixture/extensions.rb
mixture-0.7.0 lib/mixture/extensions.rb