Class: PYR::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/pyr/param.rb

Overview

Defines the #to_s method for request params. Will handle the conversion differently depending on if it is an ID param e.g. controller/:id

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, id: nil) ⇒ Param

Set the param ‘name` and `value`, and whether it’s an ID



12
13
14
15
16
# File 'lib/pyr/param.rb', line 12

def initialize(name, value, id: nil)
  @id    = !id.nil?
  @name  = name
  @value = value
end

Instance Attribute Details

#nameObject (readonly)



9
10
11
# File 'lib/pyr/param.rb', line 9

def name
  @name
end

#valueObject (readonly)



9
10
11
# File 'lib/pyr/param.rb', line 9

def value
  @value
end

Instance Method Details

#id?Boolean

Is the param an ID param?

Returns:

  • (Boolean)


19
20
21
# File 'lib/pyr/param.rb', line 19

def id?
  @id
end

#to_sObject

Format the param for the URI



24
25
26
27
# File 'lib/pyr/param.rb', line 24

def to_s
  url_value = value.to_s.tr('#;\'""', '').gsub(' ', '%20')
  id? ? "/#{value}" : "#{name}=#{url_value}"
end