Class: Params

Inherits:
Object
  • Object
show all
Defined in:
lib/fresco/runtime/runtime.rb

Overview

Params wraps a single Symbol→String hash that the parsers and dispatcher all write into. Folding (path > form > query) happens at write time — the dispatcher writes path captures after the parsers have already folded form and query, so path wins by virtue of write order. Writes use #set_sym instead of []= because Spinel widens user-defined []= keys to sp_RbVal on the HTTP dispatch path. Typed accessors (#int / #float / #bool / #str) coerce on read with a positional scalar default (kwargs-with-defaults collapse to mrb_int under Spinel — see [[spinel_initialize_kwargs]]).

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Params

Returns a new instance of Params.



526
527
528
# File 'lib/fresco/runtime/runtime.rb', line 526

def initialize(raw)
  @raw = raw
end

Instance Method Details

#[](key = :__t) ⇒ Object



530
531
532
# File 'lib/fresco/runtime/runtime.rb', line 530

def [](key = :__t)
  @raw[key]
end

#bool(key = :__t, default = false) ⇒ Object



579
580
581
582
583
584
585
586
# File 'lib/fresco/runtime/runtime.rb', line 579

def bool(key = :__t, default = false)
  v = @raw[key]
  return default if v.nil?
  return true  if v == "true"
  return true  if v == "1"
  return true  if v == "on"
  false
end

#float(key = :__t, default = 0.0) ⇒ Object



572
573
574
575
576
577
# File 'lib/fresco/runtime/runtime.rb', line 572

def float(key = :__t, default = 0.0)
  v = @raw[key]
  return default if v.nil?
  return default if v.length == 0
  v.to_f
end

#has?(key = :__t) ⇒ Boolean

Returns:

  • (Boolean)


541
542
543
# File 'lib/fresco/runtime/runtime.rb', line 541

def has?(key = :__t)
  @raw.key?(key)
end

#int(key = :__t, default = 0) ⇒ Object



565
566
567
568
569
570
# File 'lib/fresco/runtime/runtime.rb', line 565

def int(key = :__t, default = 0)
  v = @raw[key]
  return default if v.nil?
  return default if v.length == 0
  v.to_i
end

#lengthObject



545
546
547
# File 'lib/fresco/runtime/runtime.rb', line 545

def length
  @raw.length
end

#rawObject

Underlying hash for monomorphic callers (logger, parsers). Returning the real reference is the point — the parsers mutate it in place. Callers that want to iterate go through ‘.raw.each` — proxying `each(&blk)` here doesn’t lower under Spinel (the block param’s type collapses and codegen emits an undefined block variable).



554
555
556
# File 'lib/fresco/runtime/runtime.rb', line 554

def raw
  @raw
end

#set_sym(key = :__t, value = "") ⇒ Object

Defaults are type hints for Spinel. Without them, user-defined setters can widen the key to sp_RbVal even when every call passes a symbol, and codegen then emits a bad SymStrHash_set call.



537
538
539
# File 'lib/fresco/runtime/runtime.rb', line 537

def set_sym(key = :__t, value = "")
  @raw[key] = value
end

#str(key = :__t, default = "") ⇒ Object



558
559
560
561
562
563
# File 'lib/fresco/runtime/runtime.rb', line 558

def str(key = :__t, default = "")
  v = @raw[key]
  return default if v.nil?
  return default if v.length == 0
  v
end