Class: Browser::FormData

Inherits:
Object show all
Extended by:
Converter
Includes:
NativeCachedWrapper, Enumerable
Defined in:
opal/browser/form_data.rb

Defined Under Namespace

Modules: Converter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Converter

build_form_data, build_query, contain_files?, decode, decode_uri, encode, encode_uri, flatten, from_native, parse_query, unflatten

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Class Method Details

.create(hash = nil) ⇒ Object

Create a new FormData instance



150
151
152
153
154
155
156
157
158
# File 'opal/browser/form_data.rb', line 150

def self.create(hash=nil)
  if Hash === hash
    FormData.build_form_data(hash)
  elsif DOM::Element::Form === hash
    new(`new FormData(#{hash.to_n})`)
  else
    new(`new FormData()`)
  end
end

Instance Method Details

#<<(tuple) ⇒ Object

Append a tuple to this FormData instance

   a tuple of a key, value and possibly a filename

Parameters:



165
166
167
168
169
170
171
172
173
# File 'opal/browser/form_data.rb', line 165

def <<(tuple)
  key, value, filename = tuple

  unless filename
    `#@native.append(#{key}, #{Native.convert(value)})`
  else
    `#@native.append(#{key}, #{Native.convert(value)}, #{filename})`
  end
end

#[](key) ⇒ Object

Get a field from this FormData instance with a given name



176
177
178
# File 'opal/browser/form_data.rb', line 176

def [](key)
  FormData.from_native(`#@native.get(#{key})`)
end

#delete(key) ⇒ Object

Delete a field from this FormData instance



221
222
223
# File 'opal/browser/form_data.rb', line 221

def delete(key)
  `#@native.delete(#{key})`
end

#each(&block) ⇒ Object

Iterate over all elements of this FormData



211
212
213
# File 'opal/browser/form_data.rb', line 211

def each(&block)
  to_h.each(&block)
end

#include?(key) ⇒ Boolean

Checks if a field of this name exists in this FormData instance

Returns:

  • (Boolean)


216
217
218
# File 'opal/browser/form_data.rb', line 216

def include?(key)
  `#@native.has(#{key})`
end

#set(key, value, filename = nil) ⇒ Object Also known as: []=

Set a field in this FormData instance with a given name



181
182
183
184
185
186
187
# File 'opal/browser/form_data.rb', line 181

def set(key, value, filename = nil)
  unless filename
    `#@native.set(#{key}, #{Native.convert(value)})`
  else
    `#@native.set(#{key}, #{Native.convert(value)}, #{filename})`
  end
end

#to_aObject

Convert to array



206
207
208
# File 'opal/browser/form_data.rb', line 206

def to_a
  to_h.to_a
end

#to_hObject

Convert to hash



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'opal/browser/form_data.rb', line 191

def to_h
  hash = {}
  %x{
    var pair, v, e = #@native.entries();
    while (true) {
      v = e.next();
      if (v.done) break;
      pair = v.value;
      #{hash[`pair[0]`] = FormData.from_native(`pair[1]`)}
    }
  }
  hash
end