Module: Mixture::Extensions::Hashable

Extended by:
Forwardable
Includes:
Comparable, Enumerable
Defined in:
lib/mixture/extensions/hashable.rb

Overview

Has the mixture respond to #[] and #[]=.

Constant Summary collapse

MAPPED_METHODS =

The methods that are mapped directly to the #to_hash method.

Returns:

  • (Array<Symbol>)
%w(
  each <=> keys values each_key each_value has_value? value?
  size length empty? each_pair
).map(&:intern)

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



28
29
30
# File 'lib/mixture/extensions/hashable.rb', line 28

def [](key)
  attribute(key.to_s.intern)
end

#[]=(key, value) ⇒ Object Also known as: store



36
37
38
# File 'lib/mixture/extensions/hashable.rb', line 36

def []=(key, value)
  attribute(key.to_s.intern, value)
end

#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key) {|key| ... } ⇒ Object

Overloads:

  • #fetch(key) ⇒ Object

    Performs a fetch. This acts just like Hash's fetch.

    Parameters:

    • key (Symbol)

      The key to check for an attribute.

    Returns:

    • (Object)

      The attribute's value.

    Raises:

    • (KeyError)

      If the key isn't present.

  • #fetch(key, default) ⇒ Object

    Performs a fetch. This acts just like Hash's fetch. If the attribute doesn't exist, the default argument value is used as a value instead.

    Parameters:

    • key (Symbol)

      The key to check for an attribute.

    • default (Object)

      The value to use instead of an error.

    Returns:

    • (Object)

      The attribute's value, or the default value instead.

  • #fetch(key) {|key| ... } ⇒ Object

    Performs a fetch. This acts just like Hash's fetch. If the attribute doesn't exist, the value of the block is used as a value instead.

    Parameters:

    • key (Symbol)

      The key to check for an attribute.

    Yields:

    • (key)

      The block is called if an attribute does not exist.

    Yield Parameters:

    • key (Symbol)

      The key of the non-existant attribute.

    Yield Returns:

    • (Object)

      Return value of the method.

    Returns:

    • (Object)

      The attribute's value, or the block's value instead.



85
86
87
88
89
90
91
# File 'lib/mixture/extensions/hashable.rb', line 85

def fetch(key, default = Undefined)
  if key?(key.to_s.intern) then attribute(key.to_s.intern)
  elsif block_given?          then yield(key.to_s.intern)
  elsif default != Undefined  then default
  else fail KeyError, "Undefined attribute #{key.to_s.intern}"
  end
end

#key?(key) ⇒ Boolean Also known as: has_key?

Checks for an attribute with the given name.

Parameters:

  • key (Symbol)

    The name of the attribute to check.

Returns:

  • (Boolean)


51
52
53
# File 'lib/mixture/extensions/hashable.rb', line 51

def key?(key)
  self.class.attributes.key?(key.to_s.intern)
end

#to_hashHash{Symbol => Object} Also known as: to_h

The attributes defined on this instance. It returns a hash containing the key-value pairs for each attribute.

Returns:

  • (Hash{Symbol => Object})


42
43
44
# File 'lib/mixture/extensions/hashable.rb', line 42

def to_hash
  attributes
end