Module: Origen::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/origen/controller.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Used to proxy all method and attribute requests not implemented on the controller to the model.

On first call of a missing method a method is generated to avoid the missing lookup next time, this should be faster for repeated lookups of the same method, e.g. reg



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/origen/controller.rb', line 122

def method_missing(method, *args, &block)
  if model.respond_to?(method)
    # This method is handled separately since it is important to produce a proxy method
    # that takes no arguments, otherwise the register address lookup system mistakes it
    # for a legacy way of calculating the base address whereby the register itself was
    # given as an argument.
    if method.to_sym == :base_address
      define_singleton_method(method) do
        model.send(method)
      end
      base_address
    else
      define_singleton_method(method) do |*args, &block|
        model.send(method, *args, &block)
      end
      send(method, *args, &block)
    end
  else
    super
  end
end

Instance Method Details

#==(obj, options = {}) ⇒ Object Also known as: equal?

When compared to another object, a controller will consider itself equal if either the controller or its model match the given object



83
84
85
86
87
88
89
90
91
92
# File 'lib/origen/controller.rb', line 83

def ==(obj, options = {})
  if obj.is_a?(Origen::SubBlocks::Placeholder)
    obj = obj.materialize
  end
  if options[:called_from_model]
    super(obj)
  else
    super(obj) || model == obj
  end
end

#controllerObject

Means that when dealing with a controller/model pair, you can always call obj.model and obj.controller to get the one you want, regardless of the one you currently have.



98
99
100
# File 'lib/origen/controller.rb', line 98

def controller
  self
end

#inspectObject



48
49
50
51
52
53
54
# File 'lib/origen/controller.rb', line 48

def inspect
  if model
    "<Model/Controller: #{model.class}:#{model.object_id}/#{self.class}:#{object_id}>"
  else
    "<Controller: #{self.class}:#{object_id}>"
  end
end

#is_a?(*args) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/origen/controller.rb', line 56

def is_a?(*args)
  if model
    super(*args) || model.is_a?(*args)
  else
    super(*args)
  end
end

#modelObject

Returns the controller's model



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/origen/controller.rb', line 65

def model
  @model ||= begin
    if self.class.path_to_model
      m = eval(self.class.path_to_model)
      if m
        if m.respond_to?(:_controller=)
          m.send(:_controller=, self)
        end
      else
        fail "No model object found at path: #{self.class.path_to_model}"
      end
      m
    end
  end
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/origen/controller.rb', line 102

def respond_to?(*args)
  super || !!(!@respond_directly && model && model.respond_to_directly?(*args))
end

#respond_to_directly?(*args) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/origen/controller.rb', line 106

def respond_to_directly?(*args)
  @respond_directly = true
  result = respond_to?(*args)
  @respond_directly = false
  result
end

#to_json(*args) ⇒ Object



113
114
115
# File 'lib/origen/controller.rb', line 113

def to_json(*args)
  model.to_json(*args)
end