Class: Jinx::Hasher::MultiHash

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

Overview

Combines hashes. See #union for details.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Jinx::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, #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(*hashes) ⇒ MultiHash

Returns a new instance of MultiHash.



437
438
439
440
# File 'lib/jinx/helpers/hasher.rb', line 437

def initialize(*hashes)
  if hashes.include?(nil) then raise ArgumentError.new("MultiHash is missing a component hash.") end
  @components = hashes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Returns the union of the results of calling the given method symbol on each component.



465
466
467
# File 'lib/jinx/helpers/hasher.rb', line 465

def method_missing(symbol, *args)
  @components.map { |hash| hash.send(symbol, *args) }.inject { |value, result| result.union(value) }
end

Instance Attribute Details

#components<Hasher> (readonly)

Returns the enumerated hashes.

Returns:

  • (<Hasher>)

    the enumerated hashes



435
436
437
# File 'lib/jinx/helpers/hasher.rb', line 435

def components
  @components
end

Instance Method Details

#[](key) ⇒ Object



442
443
444
445
# File 'lib/jinx/helpers/hasher.rb', line 442

def [](key)
  @components.each { |hash| return hash[key] if hash.has_key?(key) }
  nil
end

#eachObject



455
456
457
458
459
460
461
462
# File 'lib/jinx/helpers/hasher.rb', line 455

def each
  @components.each_with_index do |hash, index|
    hash.each do |k, v|
       yield(k, v) unless (0...index).any? { |i| @components[i].has_key?(k) }
    end
  end
  self
end

#has_key?(key) ⇒ Boolean

Returns:



447
448
449
# File 'lib/jinx/helpers/hasher.rb', line 447

def has_key?(key)
  @components.any? { |hash| hash.has_key?(key) }
end

#has_value?(value) ⇒ Boolean

Returns:



451
452
453
# File 'lib/jinx/helpers/hasher.rb', line 451

def has_value?(value)
  @components.any? { |hash| hash.has_value?(value) }
end