Class: Leveret::Parameters
- Inherits:
-
Object
- Object
- Leveret::Parameters
- Extended by:
- Forwardable
- Defined in:
- lib/leveret/parameters.rb
Overview
Provides basic indifferent hash access for jobs, allows strings to be used to access symbol keyed values, or symbols to be used to access string keyed values.
Overrides the [] method of the hash, all other calls (except #serialize) are delegated to the #params object
Beware of using both strings and symbols with the same value in the same hash e.g. {:one => 1, ‘one’ => 1} only one of these values will ever be returned.
Instance Attribute Summary collapse
-
#params ⇒ Object
The parameters hash wrapped up by this object.
Class Method Summary collapse
-
.from_json(json) ⇒ Parameters
Create a new instance of this class from a a serialized JSON object.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Access #params indifferently.
-
#initialize(params = {}) ⇒ Parameters
constructor
A new instance of Parameters.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Delegate any unknown methods to the #params hash.
-
#respond_to?(method_name, include_private = false) ⇒ Boolean
Check the #params hash as well as this class for a method’s existence.
-
#serialize ⇒ String
Serialize the current value of #params.
Constructor Details
#initialize(params = {}) ⇒ Parameters
Returns a new instance of Parameters.
18 19 20 |
# File 'lib/leveret/parameters.rb', line 18 def initialize(params = {}) self.params = params || {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Delegate any unknown methods to the #params hash
33 34 35 |
# File 'lib/leveret/parameters.rb', line 33 def method_missing(method_name, *arguments, &block) params.send(method_name, *arguments, &block) end |
Instance Attribute Details
#params ⇒ Object
The parameters hash wrapped up by this object
13 14 15 |
# File 'lib/leveret/parameters.rb', line 13 def params @params end |
Class Method Details
.from_json(json) ⇒ Parameters
Create a new instance of this class from a a serialized JSON object.
54 55 56 57 |
# File 'lib/leveret/parameters.rb', line 54 def self.from_json(json) params = JSON.load(json) new(params) end |
Instance Method Details
#[](key) ⇒ Object?
Access #params indifferently. Tries the passed key directly first, then tries it as a symbol, then tries it as a string.
28 29 30 |
# File 'lib/leveret/parameters.rb', line 28 def [](key) params[key] || (key.respond_to?(:to_sym) && params[key.to_sym]) || (key.respond_to?(:to_s) && params[key.to_s]) end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
Check the #params hash as well as this class for a method’s existence
38 39 40 |
# File 'lib/leveret/parameters.rb', line 38 def respond_to?(method_name, include_private = false) params.respond_to?(method_name, include_private) || super end |
#serialize ⇒ String
Serialize the current value of #params. Outputs JSON.
45 46 47 |
# File 'lib/leveret/parameters.rb', line 45 def serialize JSON.dump(params) end |