Method: NsOptions::NamespaceData#apply

Defined in:
lib/ns-options/namespace_data.rb

#apply(values = nil) ⇒ Object

The opposite of #to_hash. Takes a hash representation of options and namespaces and mass assigns option values.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ns-options/namespace_data.rb', line 61

def apply(values=nil)
  (values || {}).each do |name, value|
    if has_namespace?(name)
      # recursively apply namespace values if hash given; ignore otherwise.
      get_namespace(name).apply(value) if value.kind_of?(Hash)
    else
      # this is meant as a "value importer", so always apply distinct values
      # to prevent unintentional pass-by-ref shared objects.
      # be sure to use the namespace's writer to write the option value
      @ns.send("#{name}=", NsOptions.distinct_value(value))
    end
  end
end