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


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

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

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


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

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.


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

def fetch(key, default = Undefined)
  case
  when key?(key.to_s.intern) then attribute(key.to_s.intern)
  when block_given?          then yield(key.to_s.intern)
  when 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)

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

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})

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

def to_hash
  attributes
end