Class: AllParams

Inherits:
ParamsBase show all
Defined in:
lib/params/all_params.rb

Instance Method Summary collapse

Methods inherited from ParamsBase

#[], #get, #method_missing, #required

Constructor Details

#initialize(params, request_params) ⇒ AllParams



2
3
4
5
6
7
8
9
10
# File 'lib/params/all_params.rb', line 2

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ParamsBase

Instance Method Details

#[]=(key, val) ⇒ Object

Raises:

  • (RuntimeError)


14
15
16
# File 'lib/params/all_params.rb', line 14

def []=(key,val)
  raise RuntimeError.new("This is a virtual hash based on two physical hashes, use the add method instead.")
end

#add(key, value, store = "params") ⇒ Object

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



55
56
57
58
59
60
61
62
# File 'lib/params/all_params.rb', line 55

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") ⇒ Object

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



75
76
77
78
79
# File 'lib/params/all_params.rb', line 75

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) ⇒ Boolean

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



29
30
31
32
33
34
# File 'lib/params/all_params.rb', line 29

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) ⇒ Boolean



36
37
38
# File 'lib/params/all_params.rb', line 36

def present_json?(key_name)
  @request_params.has_key?(key_name)
end

#present_local?(key_name) ⇒ Boolean



40
41
42
# File 'lib/params/all_params.rb', line 40

def present_local?(key_name)
  @params.has_key?(key_name)
end

#super_addObject



12
# File 'lib/params/all_params.rb', line 12

alias :super_add :[]=