Class: Hash

Inherits:
Object show all
Defined in:
lib/eac_ruby_utils/patches/hash/if_key.rb,
lib/eac_ruby_utils/patches/hash/to_struct.rb,
lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb,
lib/eac_ruby_utils/patches/hash/options_consumer.rb

Instance Method Summary collapse

Instance Method Details

#if_key(key, default_value = nil) ⇒ Object



7
8
9
10
11
# File 'lib/eac_ruby_utils/patches/hash/if_key.rb', line 7

def if_key(key, default_value = nil)
  return default_value unless key?(key)

  block_given? ? yield(fetch(key)) : fetch(key)
end

#to_options_consumerObject

Returns an EacRubyUtils::OptionsConsumer out of its receiver.



7
8
9
# File 'lib/eac_ruby_utils/patches/hash/options_consumer.rb', line 7

def to_options_consumer
  ::EacRubyUtils::OptionsConsumer.new(self)
end

#to_structObject

Returns an EacRubyUtils::Struct out of its receiver.



7
8
9
# File 'lib/eac_ruby_utils/patches/hash/to_struct.rb', line 7

def to_struct
  ::EacRubyUtils::Struct.new(self)
end

#to_sym_keys_hashObject



4
5
6
7
8
9
# File 'lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb', line 4

def to_sym_keys_hash
  each_with_object({}) do |(key, value), memo|
    symbol_key = key.respond_to?(:to_sym) ? key.to_sym : key.to_s.to_sym
    memo[symbol_key] = value
  end
end