Class: Web::Unit::Form

Inherits:
HtmlElem show all
Defined in:
lib/web/unit/form.rb

Instance Attribute Summary collapse

Attributes inherited from HtmlElem

#array, #attrs, #children, #data, #tag

Instance Method Summary collapse

Methods inherited from HtmlElem

#append, #extract, #find, #has?, #inspect, #print, #readlink, #search

Constructor Details

#initialize(ah) ⇒ Form

Returns a new instance of Form.



16
17
18
19
20
21
22
23
24
# File 'lib/web/unit/form.rb', line 16

def initialize( ah )
  super( 'form', ah )
  @parameters = []    # index by int
  @params = {}        # index by name
  @action = ah["name"]
  @action = ah["action"]
  @method = ah["method"] == nil ? "GET" : ah["method"]
  #print @action,":",method,"\n"
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



13
14
15
# File 'lib/web/unit/form.rb', line 13

def action
  @action
end

#encodingObject

Returns the value of attribute encoding.



13
14
15
# File 'lib/web/unit/form.rb', line 13

def encoding
  @encoding
end

#methodObject

Returns the value of attribute method.



13
14
15
# File 'lib/web/unit/form.rb', line 13

def method
  @method
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/web/unit/form.rb', line 13

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



14
15
16
# File 'lib/web/unit/form.rb', line 14

def parameters
  @parameters
end

#paramsObject (readonly)

Returns the value of attribute params.



14
15
16
# File 'lib/web/unit/form.rb', line 14

def params
  @params
end

Instance Method Details

#add_param(param) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/web/unit/form.rb', line 26

def add_param( param )
  @parameters.push param
  if @params.key?param.name then
    @params[param.name].update( param )
    param.update( @params[param.name] )
  else
    @params[param.name] = param
  end
end

#freezeObject



36
37
38
39
# File 'lib/web/unit/form.rb', line 36

def freeze
  @parameters.freeze
  @params.freeze
end

#set_parameters(array) ⇒ Object

— Form#set_parameters( array )

fill up each fields, by index.


59
60
61
62
63
64
# File 'lib/web/unit/form.rb', line 59

def set_parameters( array )
  array.each_index do |i|
    self.parameters[i].value = array[i]
  end
  self
end

#set_params(hash) ⇒ Object

— Form#set_params( hash )

fill up each fields, by name.


46
47
48
49
50
51
52
# File 'lib/web/unit/form.rb', line 46

def set_params( hash )
  hash.each do |k,v|
    raise ArgumentError.new("wrong key: " + k) unless self.params[k]
    self.params[k].value = v
  end
  self
end

#store_test_data(fields, data, s = ',', button = nil) ⇒ Object

— Form#store_test_data(fields,data,s=‘,’,button=nil)

store data to backend ( Database, File, ... ).
input csv data to each fields, and submit.


72
73
74
75
76
77
# File 'lib/web/unit/form.rb', line 72

def store_test_data( fields, data, s=',', button=nil )
  data.split( "\n" ).collect{ |r| r.strip.split( s ) }.each do |r|
    fields.each_index{ |i| @params[fields[i]].value = r[i] }
    self.submit( button )
  end
end

#submit(button = nil, x = nil, y = nil) ⇒ Object

— Form#submit(button=nil,x=nil,y=nil)

submit form, and return a Response.


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/web/unit/form.rb', line 83

def submit( button=nil, x=nil, y=nil )
  data = []
  matched = nil
  multipart = (attrs['enctype'] == 'multipart/form-data')
  # make data from params

  @parameters.each do |p|
    puts "#{p.class}/#{p.name}" if $DEBUG
    if p.class == InputSubmit || p.class == InputImage
      next unless p.name == button || p.value == button
      matched = p
      if p.class  == InputImage
        p.x = x
        p.y = y
      end
    end
    
    if multipart
      data << p.multipart_query_data(BOUNDARY)
      else
      data << p.query_data
    end
  end
  
  if multipart
    data = data.join('') + "--#{BOUNDARY}--\n"
  else
    data = data.compact.join('&')
  end

  Response::new.init_http( @action, @method, data, false, multipart)
end