Module: Rtml::Assigns

Included in:
Widget
Defined in:
lib/rtml/assigns.rb

Overview

Included into most RTML classes

Instance Method Summary collapse

Instance Method Details

#assignment_names(options = {}) ⇒ Object

Returns the name of each variable that would be returned by #assigns. The options are the same.



25
26
27
28
29
30
31
# File 'lib/rtml/assigns.rb', line 25

def assignment_names(options = {})
  ivars = instance_variables
  ivars -= options[:without] if options[:without]
  ivars -= protected_instance_variables if respond_to?(:protected_instance_variables)
  ivars -= rtml_protected_instance_variables if respond_to?(:rtml_protected_instance_variables)
  ivars
end

#assigns(options = {}) ⇒ Object

Returns all instance variables EXCEPT those returned by #protected_instance_variables or #rtml_protected_instance_variables. If one of those methods doesn’t exist, it is ignored.

The returned instance variables are mapped to a hash so that they are ready to be passed (for example) into ActionController#render as a value for the :assigns option.

If this object responds to ‘parent’, then the return value is merged with parent#assigns.

If the :without option is specified, the variables in that array are omitted.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rtml/assigns.rb', line 12

def assigns(options = {})
  ivars = assignment_names(options)

  assigns = ivars.inject({}) { |hash, ivar| hash[ivar[1..-1]] = instance_variable_get(ivar); hash }
  if respond_to?(:parent) && parent.respond_to?(:assigns)
    parent.assigns(:without => (options[:without] || []) + ivars).merge(assigns)
  else
    assigns
  end
end