class AllParams
Public Class Methods
new(params, request_params)
click to toggle source
# File lib/params/all_params.rb, line 4 def initialize(params, request_params) @params = params @request_params = request_params # this class acts as a virtual hash on top of two real hashes for all write-related methods (which need to be overriden) # the values from both hashes are also synchronized in this class to avoid having to override all read-related methods self.merge!(@params) self.merge!(@request_params) end
Public Instance Methods
[](key)
click to toggle source
# File lib/params/all_params.rb, line 20 def [](key) if @params[key] and ! @params[key].empty? @params[key] else @request_params[key] end end
[]=(key,val)
click to toggle source
# File lib/params/all_params.rb, line 16 def []=(key,val) raise RuntimeError.new("This is a virtual hash based on two physical hashes, use the add method instead.") end
Also aliased as: super_add
add(key, value, store = "params")
click to toggle source
Adds a key/value to the params
Attributes¶ ↑
-
key_name
- key name -
value
- value to assign -
store
- place to add params or json (default = params)
Returns¶ ↑
-
value added
# File lib/params/all_params.rb, line 65 def add(key, value, store = "params") if store == "params" @params[key] = value elsif store == "json" @request_params[key] = value end super_add(key, value) end
find_or_add(key_name, value, store = "params")
click to toggle source
Adds a key/value to the params if not found
Attributes¶ ↑
-
key_name
- key name -
value
- value to assign -
store
- place to add params or json (default = params)
Returns¶ ↑
-
value of key
# File lib/params/all_params.rb, line 85 def find_or_add(key_name, value, store = "params") ans = get(key_name) add(key_name, value, store) if ans == "" ans == "" ? value : ans end
present?(key_name, where = false)
click to toggle source
TODO: refactor out the where functionality
Test if a param is present ==== Attributes * +key_name+ - key to look for * +where+ - if true returns the hash where the key was found ==== Returns * the param hash name if where=true, otherwise true/false
# File lib/params/all_params.rb, line 39 def present?(key_name, where = false) ans = nil ans = "params" if @params.has_key?(key_name) ans = "json" if @request_params.has_key?(key_name) where ? ans : !ans.nil? end
present_json?(key_name)
click to toggle source
# File lib/params/all_params.rb, line 46 def present_json?(key_name) @request_params.has_key?(key_name) end
present_local?(key_name)
click to toggle source
# File lib/params/all_params.rb, line 50 def present_local?(key_name) @params.has_key?(key_name) end