Class: FormReader::FormElements

Inherits:
Object
  • Object
show all
Defined in:
lib/web/formreader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormElements

Returns a new instance of FormElements.



170
171
172
# File 'lib/web/formreader.rb', line 170

def initialize
  @fields = {}
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



168
169
170
# File 'lib/web/formreader.rb', line 168

def action
  @action
end

#fieldsObject (readonly)

Returns the value of attribute fields.



167
168
169
# File 'lib/web/formreader.rb', line 167

def fields
  @fields
end

Instance Method Details

#find_select(name) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/web/formreader.rb', line 297

def find_select( name )
  select = if (fields[name])
             fields[name].find{ |e|
               e.kind_of? Select
             }
           end
  unless select
    select = Select.new
    push name, select
  end
  select
end

#get_fieldsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/web/formreader.rb', line 174

def get_fields
  crushedFields = { }
  @fields.each{ |k,v|
    v = v.collect { |v1|
      if v1.kind_of? Select
        v1.to_a.sort
      else
        v1
      end
    }.flatten
    crushedFields[k] = v
  }
  Web::Testing::MultiHashTree.new(crushedFields).fields
  #crushedFields
end

#get_optionsObject



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/web/formreader.rb', line 190

def get_options
  all_options = { }
  @fields.each{ |k,v|
    v.each { |field|
      if (field.kind_of? Select)
        all_options[k] = field.options.collect{ |o|
          o.value
        }
      end
    }
  }
  all_options
end

#merge_fields(edited_fields) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/web/formreader.rb', line 204

def merge_fields( edited_fields )
  merged = Marshal.load( Marshal.dump( @fields ) )
  
  edited_fields.each{ |k,v|
    # normalize inputs
    k = k.to_s
    v = [v] unless v.kind_of? Array

    # guard against errors
    if merged[k].nil?
      raise Web::Testing::FieldNotFoundException.new( "Element named #{k} not present on form" )
    end

    if (v.size > merged[k].size)
      raise FormMergeError.new( "Only #{merged[k].size} elements on form; " +
                                        "Can't merge with #{v.size} new values" )
    end

    v.each_with_index{ |v, i|
      pristine = @fields[k][i]
      target   = merged[k][i]

      if (pristine.kind_of? Select)
        # guard against improperly submitted values
        unless v.kind_of? Hash
          raise FormMergeError.new( "Values for select elements must be merged in by using merge('#{k}'=>select('#{v}')" )
        end

        # guard against merging multiple
        # values into single select
        if (pristine.multiple == false) & (v.keys.length > 1 )
          raise FormMergeError.new( "Trying to merge multiple [#{ v.inspect }] into single select[#{ k }]" )
        end

        # selects must be merged with a hash
        v.each{ |value, flag|
          # guard against merging nonexistent option
          unless pristine.options.find{ |e|
              e.value == value
            }
            raise FormMergeError. \
            new("Form doesn't have an option for #{value}")
          end
          
          
          # turn off everything else for single selects
          
          unless target.multiple || flag == false
            target.options.each { |o|
              o.selected = false
            }
          end
        
          # merge select fields
          target.options.find{ |e|
            e.value == value
          }.selected = flag
        }
        
      else
        merged[k][i] = v
      end
    }
  }

  # delete unchecked checkboxes
  merged.delete_if{ |k,v|
    v.delete_if{ |e|
      e == false
    }
    v.size == 0
  }



  # flatten out select elements
  merged.each{ |k,v|
    merged[k] = v.collect{ |e|
      if (e.kind_of? Select)
        e.to_a
      else
        e
      end
    }.flatten
  }
  merged
end

#push(name, value) ⇒ Object Also known as: push_text, push_select



292
293
294
295
# File 'lib/web/formreader.rb', line 292

def push( name, value )
  @fields[name] ||= []
  @fields[name].push value
end