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


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

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:


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

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


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

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

#delete(key) ⇒ Object

Delete a field from this FormData instance


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

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

#each(&block) ⇒ Object

Iterate over all elements of this FormData


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

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)

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

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


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

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


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

def to_a
  to_h.to_a
end

#to_hObject

Convert to hash


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

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