Sha256: bb5d73e9853472a635a5479e62bcc2d602936d9dd656ec4c279cb8ca10576925

Contents?: true

Size: 979 Bytes

Versions: 1

Compression:

Stored size: 979 Bytes

Contents

module Surrounded
  module Context
    module Initializing
      def new(*args, &block)
        instance = allocate
        instance.send(:preinitialize)
        instance.send(:initialize, *args, &block)
        instance.send(:postinitialize)
        instance
      end

      # Shorthand for creating an instance level initialize method which
      # handles the mapping of the given arguments to their named role.
      def initialize(*setup_args)
        private_attr_reader(*setup_args)
      
        mod = Module.new
        line = __LINE__
        mod.class_eval "
          def initialize(#{setup_args.join(',')})
            preinitialize
            arguments = method(__method__).parameters.map{|arg| eval(arg[1].to_s) }
            @role_map = RoleMap.new
            map_roles(#{setup_args}.zip(arguments))
            postinitialize
          end
        ", __FILE__, line
        const_set("ContextInitializer", mod)
        include mod
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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