Class: Leveret::Parameters

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Parameters

Returns a new instance of Parameters.

Parameters:

  • params (Hash) (defaults to: {})

    Hash you wish to access indifferently



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

#paramsObject

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.

Parameters:

  • json (String)

    JSON representation of the parameters we want to access

Returns:

  • (Parameters)

    New instance based on the passed JSON



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.

Parameters:

  • key (Object)

    Key of the item we’re trying to access in #params

Returns:

  • (Object, nil)

    Value related to key, or nil if object is not found



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

Returns:

  • (Boolean)


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

#serializeString

Serialize the current value of #params. Outputs JSON.

Returns:

  • (String)

    JSON encoded representation of the params



45
46
47
# File 'lib/leveret/parameters.rb', line 45

def serialize
  JSON.dump(params)
end