Class: ParamsBase
- Inherits:
-
Hash
- Object
- Hash
- ParamsBase
- Defined in:
- lib/params/params_base.rb
Direct Known Subclasses
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#add(key, value) ⇒ Object
Adds a key/value to the params.
-
#find_or_add(key, value) ⇒ Object
Adds a key/value to the params if not found.
-
#get(key, default_value = "") ⇒ Object
Gets a params.
-
#method_missing(key, *args) ⇒ Object
Allows you to specify a key like a method call.
-
#required(key) ⇒ Object
Raises an error if a key is not found.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args) ⇒ Object
Allows you to specify a key like a method call
Attributes
-
key_name
- key name note: you must use get if keyname has spaces -
*args - allows you to send a default value
Returns
-
value of key - including resolved properties that may be embedded
Examples
@p = Params.new(params)
@p.SS_application
=> "Sales"
61 62 63 64 65 |
# File 'lib/params/params_base.rb', line 61 def method_missing(key, *args) ans = get(key.to_s) ans = args[0] if ans == "" && args[0] ans end |
Instance Method Details
#[](key) ⇒ Object
2 3 4 |
# File 'lib/params/params_base.rb', line 2 def [] key BrpmAuto.substitute_tokens(super(key)) end |
#add(key, value) ⇒ Object
Adds a key/value to the params
Attributes
-
key_name
- key name -
value
- value to assign
Returns
-
value added
25 26 27 |
# File 'lib/params/params_base.rb', line 25 def add(key, value) self[key] = value end |
#find_or_add(key, value) ⇒ Object
Adds a key/value to the params if not found
Attributes
-
key_name
- key name -
value
- value to assign
Returns
-
value of key
39 40 41 42 43 |
# File 'lib/params/params_base.rb', line 39 def find_or_add(key, value) ans = get(key) add(key, value) if ans == "" ans == "" ? value : ans end |
#get(key, default_value = "") ⇒ Object
Gets a params
Attributes
-
key
- key to find
11 12 13 |
# File 'lib/params/params_base.rb', line 11 def get(key, default_value = "") self[key] || default_value end |
#required(key) ⇒ Object
Raises an error if a key is not found
Attributes
-
key_name
- key name
Returns
-
value of key
76 77 78 79 |
# File 'lib/params/params_base.rb', line 76 def required(key) raise "ParamsError: param #{key} must be present" unless self.has_key?(key) get(key) end |