Class: BarefootJS::SearchParams

Inherits:
Object
  • Object
show all
Defined in:
lib/barefoot_js/search_params.rb

Overview

Request-scoped SSR view of the query string behind the reactive searchParams() environment signal (router v0.5, #1922). The framework integration builds one per request from the request URL and threads it into the template scope as v[:searchParams] (the camelCase JS name the adapters keep, like every other signal/prop var); the compiled ERB template reads it via v[:searchParams].get('key').

This runtime is template-engine- and framework-agnostic (Ruby stdlib only), matching the rest of BarefootJS, so it can ship standalone.

Semantics mirror the browser's URLSearchParams#get exactly under the adapters' ?? -> ||=-style lowering: get() returns the first value for a key, or nil when the key is absent. A present-but-empty value (?sort=) keeps the empty string -- the same distinction JS ?? draws between null and ''.

Instance Method Summary collapse

Constructor Details

#initialize(query = '') ⇒ SearchParams

Parse a raw query string into the reader. A leading '?' is tolerated, '+' decodes to a space, and %XX escapes are decoded -- mirroring URLSearchParams's application/x-www-form-urlencoded parsing. A malformed pair never raises; it simply contributes nothing, matching the browser's lenient parsing.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/barefoot_js/search_params.rb', line 25

def initialize(query = '')
  query ||= ''
  query = query.sub(/\A\?/, '')
  @values = Hash.new { |h, k| h[k] = [] }
  query.split(/[&;]/).each do |pair|
    next if pair.empty?

    key, val = pair.split('=', 2)
    key = decode(key)
    val = val.nil? ? '' : decode(val)
    @values[key] << val
  end
end

Instance Method Details

#get(key) ⇒ Object

First value for key, or nil when the key is absent (see the class docstring for why nil -- not '' -- is the right "missing" sentinel). A present-but-empty value returns ''.



42
43
44
45
46
47
# File 'lib/barefoot_js/search_params.rb', line 42

def get(key)
  vals = @values[key]
  return nil if vals.nil? || vals.empty?

  vals.first
end