Class: Stretchr::Bag
- Inherits:
-
Object
- Object
- Stretchr::Bag
- Defined in:
- lib/stretchr/bag.rb
Overview
Bag is a general parameter holder with special functions for converting a hash into a query string for Stretchr
Instance Method Summary collapse
-
#get(param) ⇒ Object
Get will retrieve a stored value ==== Parameters
param
- The key to retrieve the values for. -
#initialize(options = {}) ⇒ Bag
constructor
A new instance of Bag.
-
#query_string ⇒ Object
Returns a url encoded query string of the current stored values.
-
#set(param, value = nil) ⇒ Object
Set will store a value for a given key ==== Parameters
param
- The key to store the value undervalue
- The value to store under that key, will always overwrite, so send an array for multiples.
Constructor Details
#initialize(options = {}) ⇒ Bag
Returns a new instance of Bag.
8 9 10 11 |
# File 'lib/stretchr/bag.rb', line 8 def initialize( = {}) @params = {} # the general param hash @prefix = [:prefix] # the prefix added to every key before saving end |
Instance Method Details
#get(param) ⇒ Object
Get will retrieve a stored value
Parameters
param
- The key to retrieve the values for
Examples
s = Stretchr::Bag.new s.set(“key”, “value”) s.get(“key”) #=> “value”
41 42 43 44 |
# File 'lib/stretchr/bag.rb', line 41 def get(param) param = "#{@prefix}#{param}" if @prefix @params[param.to_s] end |
#query_string ⇒ Object
Returns a url encoded query string of the current stored values
Examples
s = Stretchr::Bag.new s.set(“key”, “value”) s.set(“key2”, [“value1”, “value2”]) s.query_string #=> “key=value&key2=value1&key2=value2”
54 55 56 |
# File 'lib/stretchr/bag.rb', line 54 def query_string URI.encode_www_form(@params) end |
#set(param, value = nil) ⇒ Object
Set will store a value for a given key
Parameters
param
- The key to store the value under value
- The value to store under that key, will always overwrite, so send an array for multiples
Examples
s = Stretchr::Bag.new s.set(“key”, “value”) s.get(“key”) #=> “value”
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/stretchr/bag.rb', line 22 def set(param, value = nil) if param.is_a?(Hash) param.each_pair do |k, v| set(k, v) end else param = "#{@prefix}#{param}" if @prefix @params[param.to_s] = value unless value == nil end end |