Class: Jinx::ValueTransformerHash

Inherits:
Object
  • Object
show all
Includes:
Hasher
Defined in:
lib/jinx/helpers/hasher.rb

Overview

The ValueTransformerHash class pipes the value from a base Hasher into a transformer block.

Instance Method Summary collapse

Methods included from Hasher

#==, #assoc_values, #compact, #compose, #copy_recursive, #detect_hash_value, #detect_key, #detect_key_with_value, #detect_value, #difference, #each_key, #each_pair, #each_value, #enum_keys, #enum_keys_with_value, #enum_values, #filter, #filter_on_key, #filter_on_value, #flatten, #has_key?, #has_value?, #inspect, #join, #keys, #pretty_print, #pretty_print_cycle, #qp, #reject_keys, #reject_values, #select_keys, #select_values, #sort, #split, #to_hash, #to_s, #to_set, #transform_key, #transform_value, #union, #values

Methods included from Enumerable

#enumerate, #pp_s, #pretty_print, #pretty_print_cycle, #qp, #to_enum, #transitive_closure

Methods included from Collection

#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #to_series, #transform, #union

Constructor Details

#initialize(base) {|value| ... } ⇒ ValueTransformerHash

Creates a ValueTransformerHash on the base hash and value transformer block.

Parameters:

  • base (Hash, nil)

    the hash to transform

Yields:

  • (value)

    transforms the base value

Yield Parameters:

  • value

    the base value to transform



481
482
483
484
# File 'lib/jinx/helpers/hasher.rb', line 481

def initialize(base, &transformer)
  @base = base
  @xfm = transformer
end

Instance Method Details

#[](key) ⇒ Object

Returns the value at key after this ValueTransformerHash’s transformer block is applied, or nil if this hash does not contain key.

Parameters:

  • key

    the hash key

Returns:

  • the value at key after this ValueTransformerHash’s transformer block is applied, or nil if this hash does not contain key



489
490
491
# File 'lib/jinx/helpers/hasher.rb', line 489

def [](key)
  @xfm.call(@base[key]) if @base.has_key?(key)
end

#each {|key, value| ... } ⇒ Object

Yields:

  • (key, value)

    operate on the key and transformed value

Yield Parameters:

  • key

    the hash key

  • value

    the transformed hash value



496
497
498
# File 'lib/jinx/helpers/hasher.rb', line 496

def each
  @base.each { |k, v| yield(k, @xfm.call(v)) }
end