Module: Minitwin::Initialization

Included in:
Minitwin
Defined in:
lib/minitwin/initialization.rb

Overview

Instance construction and low-level helpers used by the DSL-generated accessors. Filters unknown keys on initialize and seeds nested block properties so that validations on nested twins can run.

Constant Summary collapse

ALIASES_VAR =

Cache constant references for JIT optimization

Minitwin::DYNAMIC_ALIASES_VAR
ALIASES_REV_VAR =
Minitwin::DYNAMIC_ALIASES_REV_VAR
FORBIDDEN_ALIAS_NAMES =

Forbidden method names that should never be aliased for security reasons

%i[
  eval instance_eval class_eval module_eval
  send __send__ public_send
  method_missing respond_to_missing?
  define_method remove_method undef_method
  instance_variable_get instance_variable_set
  instance_variables instance_variable_defined?
  const_get const_set
  class_variable_get class_variable_set
  binding tap then yield_self to_proc
  freeze __id__ object_id
  == equal? eql? hash <=>
].freeze

Instance Method Summary collapse

Instance Method Details

#initialize(**args) ⇒ Object

: (**untyped) -> instance



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/minitwin/initialization.rb', line 29

def initialize(**args)
  allowed_keys = self.class.send(:allowed_attribute_keys)

  args.select! { |arg, _| allowed_keys.include?(arg.to_sym) }

  getter_defaults = {}
  self.class.block_properties.each do |method|
    getter_defaults[method] = {} if allowed_keys.include?(method)
  end

  attrs = getter_defaults.merge(args)

  # Skip per-setter alias recomputation during bulk init; recompute once after
  @__skip_alias_recompute__ = true
  if Minitwin.send(:active_model_initialized?, self.class)
    super(attrs)
  else
    # :nocov: (exercised only without ActiveModel; covered by subprocess test)
    attrs.each { |k, v| assign_attribute(method: k, value: v) }
    # :nocov:
  end
  @__skip_alias_recompute__ = false

  __recompute_dynamic_aliases__ if self.class.dynamic_aliases?
end