Sha256: da60dacd2c3daa229d1a19a968f369c661b6ddea29e065f1cc8906bffdb5ec28

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8
require_relative "dependency"

module Hexx
  module Creators

    # @api hide
    # Module dependency constructor.
    #
    # Adds the dependency to selected module.
    #
    # @example
    #   ModuleDependency.add(MyModule, :some_module, DefaultImplementation)
    #
    #   MyModule.some_module
    #   # => DefaultImplementation
    #
    #   MyModule.some_module = AnotherImplementation
    #   # => AnotherImplementation
    #
    #   MyModule.some_module = 1
    #   # fails with #<TypeError @message="1 is not a module" >
    class ModuleDependency < Dependency

      # @api hide
      # Adds the dependency to given module
      # @example (see Hexx::Creators::ModuleDependency)
      # @param [Module] target The module to declare the dependency of.
      # @param [String, Symbol] name The name of the dependency.
      # @param [Module] default The default implementation of the dependency.

      # @api hide
      # Adds the module dependency getter
      # @return [ModuleDependency] +self+.
      def add_getter
        target.instance_eval getter
        self
      end

      # @api hide
      # Adds the module dependency setter
      # @return [ModuleDependency] +self+.
      def add_setter
        target.instance_eval setter
        self
      end

      private

      # @raise [TypeError] if a target to add the dependency is not a module.
      def check_target
        return if target.is_a? Module
        fail TypeError.new "#{ target.inspect } is not a module"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hexx-7.1.0 lib/hexx/creators/module_dependency.rb
hexx-7.0.1 lib/hexx/creators/module_dependency.rb
hexx-7.0.0 lib/hexx/creators/module_dependency.rb