Class: Dommy::URLSearchParams
- Inherits:
-
Object
- Object
- Dommy::URLSearchParams
- Includes:
- Bridge::Methods, Enumerable
- Defined in:
- lib/dommy/url.rb
Instance Method Summary collapse
- #__internal_replace__(query_string) ⇒ Object
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #append(name, value) ⇒ Object
-
#delete(name, value = UNSET) ⇒ Object
WHATWG delete(name) / delete(name, value): with a value, only removes pairs whose value also matches.
- #each(&block) ⇒ Object
- #entries ⇒ Object
- #for_each(&block) ⇒ Object (also: #forEach)
- #get(name) ⇒ Object
- #get_all(name) ⇒ Object (also: #getAll)
-
#has(name, value = UNSET) ⇒ Object
(also: #has?)
WHATWG has(name) / has(name, value): with a value, only matches a pair whose value also equals it.
-
#initialize(input = "", owner: nil) ⇒ URLSearchParams
constructor
A new instance of URLSearchParams.
- #keys ⇒ Object
- #set(name, value) ⇒ Object
- #size ⇒ Object (also: #length)
-
#sort ⇒ Object
WHATWG: sort by comparison of the names' UTF-16 code units (not code points — so a surrogate-pair character sorts by its leading 0xD800–0xDBFF unit), preserving the relative order of pairs with equal names (Ruby's sort_by is not stable, hence the index tiebreak).
- #to_s ⇒ Object
- #values ⇒ Object
Methods included from Bridge::Methods
Constructor Details
#initialize(input = "", owner: nil) ⇒ URLSearchParams
Returns a new instance of URLSearchParams.
337 338 339 340 |
# File 'lib/dommy/url.rb', line 337 def initialize(input = "", owner: nil) @owner = owner @pairs = parse(input) end |
Instance Method Details
#__internal_replace__(query_string) ⇒ Object
456 457 458 459 |
# File 'lib/dommy/url.rb', line 456 def __internal_replace__(query_string) @pairs = parse(query_string) nil end |
#__js_call__(method, args) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/dommy/url.rb', line 472 def __js_call__(method, args) case method when "get" get(args[0]) when "getAll" get_all(args[0]) when "has" value_given?(args) ? has(args[0], args[1]) : has(args[0]) when "set" set(args[0], args[1]) when "append" append(args[0], args[1]) when "delete" value_given?(args) ? delete(args[0], args[1]) : delete(args[0]) when "sort" sort when "toString" to_s when "forEach" # The callback is a live JS function (HostCallback), not a Ruby Proc, so # invoke it through the bridge ABI rather than `&block` (which would try # to to_proc it). callback(value, key, this) per WHATWG. cb = args[0] @pairs.each do |k, v| if cb.respond_to?(:__js_call__) cb.__js_call__("call", [v, k, self]) elsif cb.respond_to?(:call) cb.call(v, k, self) end end nil when "keys" keys when "values" values when "entries" entries end end |
#__js_get__(key) ⇒ Object
461 462 463 464 465 466 467 468 |
# File 'lib/dommy/url.rb', line 461 def __js_get__(key) case key when "size", "length" size else Bridge::ABSENT end end |
#append(name, value) ⇒ Object
388 389 390 391 392 |
# File 'lib/dommy/url.rb', line 388 def append(name, value) @pairs << [stringify(name), stringify(value)] notify nil end |
#delete(name, value = UNSET) ⇒ Object
WHATWG delete(name) / delete(name, value): with a value, only removes pairs whose value also matches.
396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/dommy/url.rb', line 396 def delete(name, value = UNSET) key = stringify(name) if UNSET.equal?(value) @pairs.reject! { |k, _| k == key } else val = stringify(value) @pairs.reject! { |k, vv| k == key && vv == val } end notify nil end |
#each(&block) ⇒ Object
429 430 431 |
# File 'lib/dommy/url.rb', line 429 def each(&block) @pairs.each(&block) end |
#entries ⇒ Object
441 442 443 |
# File 'lib/dommy/url.rb', line 441 def entries @pairs.dup end |
#for_each(&block) ⇒ Object Also known as: forEach
445 446 447 448 |
# File 'lib/dommy/url.rb', line 445 def for_each(&block) @pairs.each { |k, v| block.call(v, k, self) } nil end |
#get(name) ⇒ Object
342 343 344 345 346 |
# File 'lib/dommy/url.rb', line 342 def get(name) key = stringify(name) pair = @pairs.find { |k, _| k == key } pair && pair[1] end |
#get_all(name) ⇒ Object Also known as: getAll
348 349 350 351 |
# File 'lib/dommy/url.rb', line 348 def get_all(name) key = stringify(name) @pairs.select { |k, _| k == key }.map { |_, v| v } end |
#has(name, value = UNSET) ⇒ Object Also known as: has?
WHATWG has(name) / has(name, value): with a value, only matches a pair whose value also equals it.
357 358 359 360 361 362 363 |
# File 'lib/dommy/url.rb', line 357 def has(name, value = UNSET) key = stringify(name) return @pairs.any? { |k, _| k == key } if UNSET.equal?(value) val = stringify(value) @pairs.any? { |k, v| k == key && v == val } end |
#keys ⇒ Object
433 434 435 |
# File 'lib/dommy/url.rb', line 433 def keys @pairs.map { |k, _| k } end |
#set(name, value) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/dommy/url.rb', line 367 def set(name, value) key = stringify(name) first_done = false @pairs = @pairs.reject do |k, _| next false unless k == key if first_done true else first_done = true false end end val = stringify(value) @pairs.map! { |pair| pair[0] == key ? [key, val] : pair } @pairs << [key, val] unless first_done notify nil end |
#size ⇒ Object Also known as: length
423 424 425 |
# File 'lib/dommy/url.rb', line 423 def size @pairs.length end |
#sort ⇒ Object
WHATWG: sort by comparison of the names' UTF-16 code units (not code points — so a surrogate-pair character sorts by its leading 0xD800–0xDBFF unit), preserving the relative order of pairs with equal names (Ruby's sort_by is not stable, hence the index tiebreak).
414 415 416 417 418 419 420 421 |
# File 'lib/dommy/url.rb', line 414 def sort @pairs = @pairs .each_with_index .sort_by { |(name, _value), idx| [name.encode(Encoding::UTF_16BE).unpack("n*"), idx] } .map(&:first) notify nil end |
#to_s ⇒ Object
452 453 454 |
# File 'lib/dommy/url.rb', line 452 def to_s @pairs.map { |k, v| "#{encode(k)}=#{encode(v)}" }.join("&") end |
#values ⇒ Object
437 438 439 |
# File 'lib/dommy/url.rb', line 437 def values @pairs.map { |_, v| v } end |