Sha256: 3523c85f5c6fa9c442fcde749297cb3b7ed3646d55a02f7b6c96629278ef7734

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Surrounded
  module Context
    module Initializing
      extend Seclusion
      # Shorthand for creating an instance level initialize method which
      # handles the mapping of the given arguments to their named role.
      def initialize_without_keywords(*setup_args, &block)
        parameters = setup_args.join(',')
        default_initializer(parameters, setup_args, &block)
      end

      def initialize(*setup_args, &block)
        parameters = setup_args.map{|a| "#{a}:"}.join(',')
        default_initializer(parameters, setup_args, &block)
      end
      alias keyword_initialize initialize
      alias initialize_with_keywords keyword_initialize

      def initializer_block
        @initializer_block
      end

      def apply_initializer_block(instance)
        instance.instance_eval(&initializer_block) if initializer_block
      end

      def default_initializer(params, setup_args, &block)
        private_attr_reader(*setup_args)
        @initializer_block = block
        mod = Module.new
        line = __LINE__; mod.class_eval %{
          def initialize(#{params})
            @role_map = role_mapper_class.new
            @initializer_arguments = Hash[#{setup_args.to_s}.zip([#{setup_args.join(',')}])]
            map_roles(@initializer_arguments)
            self.class.apply_initializer_block(self)
          end
        }, __FILE__, line
        const_set("ContextInitializer", mod)
        include mod
        private_attr_reader :initializer_arguments
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
surrounded-1.1.0 lib/surrounded/context/initializing.rb