Class FormTestHelper::FieldsHash
In: lib/form_test_helper.rb
Parent: HashWithIndifferentAccess

A hash of fields to allow infinite nesting of fields named like ‘person[address][street]’

Methods

[]   convert_value   method_missing   new   proxy   update  

Classes and Modules

Class FormTestHelper::FieldsHash::FieldNotFoundError

Public Class methods

Uses merge! instead of update when creating a new FieldsHash so update can update field values, not the field objects themselves.

[Source]

     # File lib/form_test_helper.rb, line 147
147:     def initialize(constructor = {})
148:       if constructor.is_a?(Hash)
149:         # super()
150:         merge!(constructor)
151:       else
152:         super(constructor)
153:       end
154:     end

Public Instance methods

[Source]

     # File lib/form_test_helper.rb, line 172
172:     def [](key)
173:       raise(FieldNotFoundError, "Field named '#{key.to_s}' not found in FieldsHash.") unless self.has_key?(key)
174:       super
175:     end

Ignore requests for a proxy

[Source]

     # File lib/form_test_helper.rb, line 157
157:     def proxy; self end

Allow field values to be merged in from a hash. Example:

  new_book = {
    :name => 'Pickaxe',
    :category => 'Programming',
    :classic => true,
  }
  form.book.update(new_book)

[Source]

     # File lib/form_test_helper.rb, line 167
167:     def update(other_hash)
168:       other_hash.each_pair { |key, value| self[key].update(value) }
169:       self
170:     end

Protected Instance methods

[Source]

     # File lib/form_test_helper.rb, line 178
178:     def convert_value(value)
179:       value.is_a?(Hash) ? FieldsHash.new(value) : value
180:     end

[Source]

     # File lib/form_test_helper.rb, line 182
182:     def method_missing(method, *args)
183:       method = method.to_s
184:       if method.gsub!(/=$/, '') && self.has_key?(method)
185:         self[method].value = *args
186:       else
187:         self[method].proxy
188:       end
189:     end

[Validate]