Class: Moonrope::ParamSet
- Inherits:
-
Object
- Object
- Moonrope::ParamSet
- Defined in:
- lib/moonrope/param_set.rb
Instance Method Summary collapse
-
#_defaults=(defaults) ⇒ void
Set the defaults for the param set.
-
#_value_for(key) ⇒ Object
(also: #[], #method_missing)
Return the value for the given key.
-
#has?(key) ⇒ Boolean
Does the specified key exist?.
-
#initialize(params = {}) ⇒ ParamSet
constructor
Initialize a new ParamSet.
Constructor Details
#initialize(params = {}) ⇒ ParamSet
Initialize a new ParamSet
9 10 11 12 |
# File 'lib/moonrope/param_set.rb', line 9 def initialize(params = {}) @params = (params.is_a?(String) ? JSON.parse(params) : params) || {} @defaults = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
Return the value for the given key
30 31 32 33 34 35 36 37 |
# File 'lib/moonrope/param_set.rb', line 30 def _value_for(key) # Get the value from the params and defaults value = (@params[key.to_s] || @defaults[key.to_s]) # Ensure that empty strings are actually nil. value = nil if value.is_a?(String) && value.length == 0 # Return the value value end |
Instance Method Details
#_defaults=(defaults) ⇒ void
This method returns an undefined value.
Set the defaults for the param set
37 38 39 40 41 |
# File 'lib/moonrope/param_set.rb', line 37 def _defaults=(defaults) if defaults.is_a?(Hash) @defaults = defaults end end |
#_value_for(key) ⇒ Object Also known as: [], method_missing
Return the value for the given key
20 21 22 23 24 25 26 27 |
# File 'lib/moonrope/param_set.rb', line 20 def _value_for(key) # Get the value from the params and defaults value = (@params[key.to_s] || @defaults[key.to_s]) # Ensure that empty strings are actually nil. value = nil if value.is_a?(String) && value.length == 0 # Return the value value end |
#has?(key) ⇒ Boolean
Does the specified key exist?
49 50 51 |
# File 'lib/moonrope/param_set.rb', line 49 def has?(key) @params.keys.include?(key.to_s) || @defaults.keys.include?(key.to_s) end |