class Moonrope::ParamSet

Public Class Methods

new(params = {}) click to toggle source

Initialize a new ParamSet

@param params [Hash or String] the initial params. If string, will be parsed through JSON.

# File lib/moonrope/param_set.rb, line 9
def initialize(params = {})
  @params = (params.is_a?(String) ? JSON.parse(params) : params) || {}
  @defaults = {}
end

Public Instance Methods

[](key)
Alias for: _value_for
_as_hash() click to toggle source

Return the params as a ruby hash

# File lib/moonrope/param_set.rb, line 17
def _as_hash
  @defaults.merge(@params).inject({}) do |hash, (k, v)|
    hash[k.to_s] = v
    hash
  end
end
_defaults=(defaults) click to toggle source

Set the defaults for the param set

@param defaults [Hash] @return [void]

# File lib/moonrope/param_set.rb, line 57
def _defaults=(defaults)
  if defaults.is_a?(Hash)
    @defaults = defaults
  end
end
_set_value(name, value) click to toggle source

Set the value for a given param

@param key [String] @param value [AnyObject]

# File lib/moonrope/param_set.rb, line 48
def _set_value(name, value)
  @params[name.to_s] = value
end
_value_for(key) click to toggle source

Return the value for the given key

@param key [String] the key to lookup @return [Object] the value

# File lib/moonrope/param_set.rb, line 30
def _value_for(key)
  # Get the value from the params and defaults
  value = @params.has_key?(key.to_s) ? @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
Also aliased as: [], method_missing
has?(key) click to toggle source

Does the specified key exist?

@param key [Symbol or String] @return [Boolean]

# File lib/moonrope/param_set.rb, line 69
def has?(key)
  @params.keys.include?(key.to_s) || @defaults.keys.include?(key.to_s)
end
method_missing(key)
Alias for: _value_for