Sha256: c8f8668feeb0d392f310d9e249c5934aa6d25aa50a77d93630c1b0d4282bc7ab

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# Included into most RTML classes
module Rtml::Assigns
  # 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.
  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

  # Returns the name of each variable that would be returned by #assigns. The options are
  # the same.
  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rtml-2.0.4 lib/rtml/assigns.rb