Module: Origen::ModelInitializer::ClassMethods

Defined in:
lib/origen/model_initializer.rb

Instance Method Summary collapse

Instance Method Details

#new(*args, &block) ⇒ Object

This overrides the new method of any class which includes this module to force the newly created instance to be registered as a top-level listener.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/origen/model_initializer.rb', line 9

def new(*args, &block) # :nodoc:
  options = args.find { |a| a.is_a?(Hash) } || {}

  x = allocate
  x.send(:init_top_level) if x.respond_to?(:includes_origen_top_level?)
  x.send(:init_sub_blocks, *args) if x.respond_to?(:init_sub_blocks)
  if x.respond_to?(:version=)
    version = options[:version]
    version ||= args.first if args.first.is_a?(Integer)
    x.version = version
  end
  if x.respond_to?(:parent=)
    parent = options.delete(:parent)
    x.parent = parent if parent
  end

  x.class.included_modules.each do |mod|
    mod.send(:origen_model_init, x) if mod.respond_to?(:origen_model_init)
    mod.constants.each do |constant|
      if mod.const_defined?(constant)
        mod.const_get(constant).send(:origen_model_init, x) if mod.const_get(constant).respond_to?(:origen_model_init)
      end
    end
  end

  options.each do |k, v|
    x.send(:instance_variable_set, "@#{k}", v) if x.respond_to?(k)
  end
  if x.respond_to?(:pre_initialize)
    if x.method(:pre_initialize).arity == 0
      x.send(:pre_initialize, &block)
    else
      x.send(:pre_initialize, *args, &block)
    end
  end
  if x.method(:initialize).arity == 0
    x.send(:initialize, &block)
  else
    x.send(:initialize, *args, &block)
  end
  if x.respond_to?(:is_an_origen_model?)
    x.send(:_initialized)
    Origen::Loader.load_block(x, options)
  end
  if x.respond_to?(:register_callback_listener)
    Origen.after_app_loaded do |app|
      x.register_callback_listener
    end
  end
  # Do this before wrapping, otherwise the respond to method in the controller will
  # be looking for the model to be instantiated when it is not fully done yet
  is_top_level = x.respond_to?(:includes_origen_top_level?)

  if x.respond_to?(:wrap_in_controller)
    x = x.wrap_in_controller
  end
  # If this object has been instantiated after on_create has already been called,
  # then invoke it now
  if Origen.application_loaded? && Origen.app.on_create_called?
    if x.try(:is_a_model_and_controller)
      m = x.model
      c = x.controller
      m.on_create if m.respond_to_directly?(:on_create)
      c.on_create if c.respond_to_directly?(:on_create)
    else
      x.on_create if x.respond_to?(:on_create)
    end
  end
  if is_top_level
    Origen.app.listeners_for(:on_top_level_instantiated, top_level: false).each do |listener|
      listener.on_top_level_instantiated(x)
    end
  end
  x
end