Class: Dynflow::Utils::IndifferentHash
- Inherits:
 - 
      Hash
      
        
- Object
 - Hash
 - Dynflow::Utils::IndifferentHash
 
 
- Defined in:
 - lib/dynflow/utils/indifferent_hash.rb
 
Overview
Heavily inpired by ActiveSupport::HashWithIndifferentAccess, reasons we don’t want to use the original implementation:
1. we don't want any core_ext extensions
2. some users are not happy about seeing the ActiveSupport as
our depednency
  Class Method Summary collapse
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object (also: #store)
 - #deep_stringify_keys ⇒ Object
 - #deep_stringify_keys! ⇒ Object
 - #default(key = nil) ⇒ Object
 - #delete(key) ⇒ Object
 - #dup ⇒ Object
 - #fetch(key, *extras) ⇒ Object
 - 
  
    
      #initialize(constructor = {})  ⇒ IndifferentHash 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of IndifferentHash.
 - #key?(key) ⇒ Boolean (also: #include?, #has_key?, #member?)
 - #merge(hash, &block) ⇒ Object
 - #regular_update ⇒ Object
 - #regular_writer ⇒ Object
 - #reject(*args, &block) ⇒ Object
 - #replace(other_hash) ⇒ Object
 - #reverse_merge(other_hash) ⇒ Object
 - #reverse_merge!(other_hash) ⇒ Object
 - #select(*args, &block) ⇒ Object
 - #stringify_keys ⇒ Object
 - #stringify_keys! ⇒ Object
 - 
  
    
      #to_hash  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Convert to a regular hash with string keys.
 - #to_options! ⇒ Object
 - #update(other_hash) ⇒ Object (also: #merge!)
 - #values_at(*indices) ⇒ Object
 
Constructor Details
#initialize(constructor = {}) ⇒ IndifferentHash
Returns a new instance of IndifferentHash.
      11 12 13 14 15 16 17 18  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 11 def initialize(constructor = {}) if constructor.respond_to?(:to_hash) super() update(constructor) else super(constructor) end end  | 
  
Class Method Details
.[](*args) ⇒ Object
      28 29 30  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 28 def self.[](*args) new.merge!(Hash[*args]) end  | 
  
Instance Method Details
#[]=(key, value) ⇒ Object Also known as: store
      35 36 37  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 35 def []=(key, value) regular_writer(convert_key(key), convert_value(value, for: :assignment)) end  | 
  
#deep_stringify_keys ⇒ Object
      105  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 105 def deep_stringify_keys; dup end  | 
  
#deep_stringify_keys! ⇒ Object
      101  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 101 def deep_stringify_keys!; self end  | 
  
#default(key = nil) ⇒ Object
      20 21 22 23 24 25 26  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 20 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end  | 
  
#delete(key) ⇒ Object
      95 96 97  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 95 def delete(key) super(convert_key(key)) end  | 
  
#dup ⇒ Object
      73 74 75 76 77  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 73 def dup self.class.new(self).tap do |new_hash| new_hash.default = default end end  | 
  
#fetch(key, *extras) ⇒ Object
      65 66 67  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 65 def fetch(key, *extras) super(convert_key(key), *extras) end  | 
  
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
      57 58 59  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 57 def key?(key) super(convert_key(key)) end  | 
  
#merge(hash, &block) ⇒ Object
      79 80 81  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 79 def merge(hash, &block) self.dup.update(hash, &block) end  | 
  
#regular_update ⇒ Object
      33  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 33 alias_method :regular_update, :update  | 
  
#regular_writer ⇒ Object
      32  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 32 alias_method :regular_writer, :[]=  | 
  
#reject(*args, &block) ⇒ Object
      113 114 115  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 113 def reject(*args, &block) dup.tap { |hash| hash.reject!(*args, &block) } end  | 
  
#replace(other_hash) ⇒ Object
      91 92 93  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 91 def replace(other_hash) super(self.class.(other_hash)) end  | 
  
#reverse_merge(other_hash) ⇒ Object
      83 84 85  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 83 def reverse_merge(other_hash) super(self.class.(other_hash)) end  | 
  
#reverse_merge!(other_hash) ⇒ Object
      87 88 89  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 87 def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) end  | 
  
#select(*args, &block) ⇒ Object
      109 110 111  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 109 def select(*args, &block) dup.tap { |hash| hash.select!(*args, &block) } end  | 
  
#stringify_keys ⇒ Object
      103  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 103 def stringify_keys; dup end  | 
  
#stringify_keys! ⇒ Object
      99  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 99 def stringify_keys!; self end  | 
  
#to_hash ⇒ Object
Convert to a regular hash with string keys.
      118 119 120 121 122 123 124  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 118 def to_hash _new_hash = Hash.new(default) each do |key, value| _new_hash[key] = convert_value(value, for: :to_hash) end _new_hash end  | 
  
#to_options! ⇒ Object
      107  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 107 def ; self end  | 
  
#update(other_hash) ⇒ Object Also known as: merge!
      41 42 43 44 45 46 47 48 49 50 51 52 53  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 41 def update(other_hash) if other_hash.is_a? IndifferentHash super(other_hash) else other_hash.to_hash.each_pair do |key, value| if block_given? && key?(key) value = yield(convert_key(key), self[key], value) end regular_writer(convert_key(key), convert_value(value)) end self end end  | 
  
#values_at(*indices) ⇒ Object
      69 70 71  | 
    
      # File 'lib/dynflow/utils/indifferent_hash.rb', line 69 def values_at(*indices) indices.collect { |key| self[convert_key(key)] } end  |