Class: UriTemplate::Var
- Inherits:
-
Object
- Object
- UriTemplate::Var
- Defined in:
- lib/uri_template.rb
Instance Attribute Summary collapse
-
#exp ⇒ Object
readonly
Returns the value of attribute exp.
Instance Method Summary collapse
-
#initialize(exp, tree) ⇒ Var
constructor
A new instance of Var.
-
#value(params) ⇒ Object
Algorithm taken directly from the spec.
Constructor Details
#initialize(exp, tree) ⇒ Var
Returns a new instance of Var.
93 94 95 |
# File 'lib/uri_template.rb', line 93 def initialize(exp, tree) @exp, @tree = exp, tree end |
Instance Attribute Details
#exp ⇒ Object (readonly)
Returns the value of attribute exp.
91 92 93 |
# File 'lib/uri_template.rb', line 91 def exp @exp end |
Instance Method Details
#value(params) ⇒ Object
Algorithm taken directly from the spec. TODO: refactor to be Ruby-like
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/uri_template.rb', line 99 def value(params) value = params[name] || params[name.to_sym] out = "" case value when nil return nil when String if exp.named? out << "#{encode(name)}#{value.empty? ? exp.str_if_empty : "="}" end out << encode(trim(value)) when Array if !explode? if exp.named? out << "#{encode(name)}#{value.empty? ? exp.str_if_empty : "="}" end return if value.empty? out << value.map{|v| encode(v)}.join(",") else return if value.empty? out << value.map{|v| encode(v)}.join(exp.separator) end when Hash if !explode? if exp.named? out << "#{encode(name)}#{value.empty? ? exp.str_if_empty : "="}" end return if value.empty? out << value.map{|k,v| [encode(k), encode(v)]}.flatten.join(",") else return if value.empty? out << value.map{|k,v| "#{encode(k)}=#{encode(v)}"}.join(exp.separator) end end out end |