class AttributeStruct::Mash

This class has dubious semantics and we only have it so that people can write params instead of params.

Public Class Methods

from_hash(hash) click to toggle source

@return [Mash] Convert a Hash into a Mash The input Hash's default value is maintained

# File lib/attribute_struct/attribute_hash.rb, line 202
def self.from_hash(hash)
  mash = Mash.new(hash)
  mash.default = hash.default
  mash
end
new(constructor = {}) click to toggle source

@param constructor<Object>

The default value for the mash. Defaults to an empty hash.

@details [Alternatives]

If constructor is a Hash, a new mash will be created based on the keys of
the hash and no default value will be set.
Calls superclass method
# File lib/attribute_struct/attribute_hash.rb, line 63
def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor)
  else
    super(constructor)
  end
end

Public Instance Methods

[]=(key, value) click to toggle source

@param key<Object> The key to set. @param value<Object>

The value to set the key to.

@see Mash#convert_key @see Mash#convert_value

# File lib/attribute_struct/attribute_hash.rb, line 112
def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end
deep_merge(hash) click to toggle source

Perform deep merge

@return [AttributeStruct::Mash] merged hash

# File lib/attribute_struct/attribute_hash.rb, line 245
def deep_merge(hash)
  unless (hash.is_a?(Hash))
    raise ArgumentError.new "Expecting `Hash` type. Received: `#{hash.class}`"
  end
  new_self = self.dup
  hash.each do |k, v|
    if (new_self[k].is_a?(Hash) && v.is_a?(Hash))
      new_self[k] = new_self[k].deep_merge(v)
    else
      new_self[k] = v
    end
  end
  new_self
end
deep_merge!(hash) click to toggle source

Perform deep merge and replace contents of self

@return [self]

# File lib/attribute_struct/attribute_hash.rb, line 263
def deep_merge!(hash)
  self.replace(self.deep_merge(hash))
  self
end
default(key = nil) click to toggle source

@param key<Object> The default value for the mash. Defaults to nil.

@details [Alternatives]

If key is a Symbol and it is a key in the mash, then the default value will
be set to the value matching the key.
Calls superclass method
# File lib/attribute_struct/attribute_hash.rb, line 91
def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end
delete(key) click to toggle source

@param key<Object>

The key to delete from the mash.\
Calls superclass method
# File lib/attribute_struct/attribute_hash.rb, line 165
def delete(key)
  super(convert_key(key))
end
except(*keys) click to toggle source

@param *rejected<Array[(String, Symbol)] The mash keys to exclude.

@return [Mash] A new mash without the selected keys.

@example

{ :one => 1, :two => 2, :three => 3 }.except(:one)
  #=> { "two" => 2, "three" => 3 }
Calls superclass method
# File lib/attribute_struct/attribute_hash.rb, line 176
def except(*keys)
  super(*keys.map { |k| convert_key(k) })
end
fetch(key, *extras) click to toggle source

@param key<Object> The key to fetch. This will be run through convert_key. @param *extras<Array> Default value.

@return [Object] The value at key or the default value.

Calls superclass method
# File lib/attribute_struct/attribute_hash.rb, line 144
def fetch(key, *extras)
  super(convert_key(key), *extras)
end
has_key?(key)
Alias for: key?
include?(key)

def include? def has_key? def member?

Alias for: key?
initialize_copy(orig) click to toggle source

@param orig<Object> Mash being copied

@return [Object] A new copied Mash

Calls superclass method
# File lib/attribute_struct/attribute_hash.rb, line 75
def initialize_copy(orig)
  super
  # Handle nested values
  each do |k, v|
    if v.kind_of?(Mash) || v.is_a?(Array)
      self[k] = v.dup
    end
  end
  self
end
key?(key) click to toggle source

@param key<Object> The key to check for. This will be run through convert_key.

@return [Boolean] True if the key exists in the mash.

Calls superclass method
# File lib/attribute_struct/attribute_hash.rb, line 131
def key?(key)
  super(convert_key(key))
end
Also aliased as: include?, has_key?, member?
member?(key)
Alias for: key?
merge(hash) click to toggle source

@param hash<Hash> The hash to merge with the mash.

@return [Mash] A new mash with the hash values merged in.

# File lib/attribute_struct/attribute_hash.rb, line 159
def merge(hash)
  self.dup.update(hash)
end
merge!(other_hash)
Alias for: update
stringify_keys!() click to toggle source

Used to provide the same interface as Hash.

@return [Mash] This mash unchanged.

# File lib/attribute_struct/attribute_hash.rb, line 183
def stringify_keys!; self end
symbolize_keys() click to toggle source

@return [Hash] The mash as a Hash with symbolized keys.

# File lib/attribute_struct/attribute_hash.rb, line 186
def symbolize_keys
  h = Hash.new(default)
  each do |key, val|
    key = key.to_sym if key.is_a?(String) || key.is_a?(Symbol)
    h[key] = val
  end
  h
end
to_hash() click to toggle source

@return [Hash] The mash as a Hash with string keys.

# File lib/attribute_struct/attribute_hash.rb, line 196
def to_hash
  Hash.new(default).merge(self)
end
update(other_hash) click to toggle source

@param other_hash<Hash>

A hash to update values in the mash with. The keys and the values will be
converted to Mash format.

@return [Mash] The updated mash.

# File lib/attribute_struct/attribute_hash.rb, line 121
def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end
Also aliased as: merge!
values_at(*indices) click to toggle source

@param *indices<Array>

The keys to retrieve values for. These will be run through +convert_key+.

@return [Array] The values at each of the provided keys

# File lib/attribute_struct/attribute_hash.rb, line 152
def values_at(*indices)
  indices.collect { |key| self[convert_key(key)] }
end

Protected Instance Methods

convert_key(key) click to toggle source

@param key<Object> The key to convert.

@param [Object]

The converted key. If the key was a symbol, it will be converted to a
string.

@api private

# File lib/attribute_struct/attribute_hash.rb, line 217
def convert_key(key)
  key.kind_of?(Symbol) || key.kind_of?(String) ? CamelString.new(key.to_s) : key
end
convert_value(value) click to toggle source

@param value<Object> The value to convert.

@return [Object]

The converted value. A Hash or an Array of hashes, will be converted to
their Mash equivalents.

@api private

# File lib/attribute_struct/attribute_hash.rb, line 228
def convert_value(value)
  if value.class == Hash
    Mash.from_hash(value)
  elsif value.is_a?(Array)
    value.class.new.replace(value.collect { |e| convert_value(e) })
  else
    value
  end
end