Module: Casting::Context

Defined in:
lib/casting/context.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



32
33
34
# File 'lib/casting/context.rb', line 32

def self.extended(base)
  base.send(:include, InstanceMethods)
end

Instance Method Details

#initialize(*setup_args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/casting/context.rb', line 36

def initialize(*setup_args, &block)
  attr_reader(*setup_args)
  private(*setup_args)

  define_method(:__custom_initialize, &(block || proc {}))

  mod = Module.new
  mod.class_eval <<~INIT, __FILE__, __LINE__ + 1
    def initialize(#{setup_args.map { |name| "#{name}:" }.join(",")})
      @assignments = []
      #{setup_args.map do |name|
        ["assign(", name, ", '", name, "')"].join
      end.join("\n")}
      __custom_initialize
      Thread.current[:context] = self
    end
    attr_reader :assignments
  INIT
  const_set(:Initializer, mod)
  include mod
end