module Embulk::Impl::IndifferentAccess

copied from github.com/intridea/hashie/blob/da232547c29673a0d7a79c7bf2670f1ea76813ed/lib/hashie/extensions/indifferent_access.rb

Public Class Methods

[](*) click to toggle source
Calls superclass method
# File lib/embulk/data_source.rb, line 26
def [](*)
  super.convert!
end
included(base) click to toggle source
# File lib/embulk/data_source.rb, line 7
def self.included(base)
  #Hashie::Extensions::Dash::IndifferentAccess::ClassMethods.tap do |extension|
  #  base.extend(extension) if base <= Hashie::Dash && !base.singleton_class.included_modules.include?(extension)
  #end

  base.class_eval do
    alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
    alias_method :[]=, :indifferent_writer
    alias_method :store, :indifferent_writer
    %w(default update replace fetch delete key? values_at).each do |m|
      alias_method "regular_#{m}", m unless method_defined?("regular_#{m}")
      alias_method m, "indifferent_#{m}"
    end

    %w(include? member? has_key?).each do |key_alias|
      alias_method key_alias, :indifferent_key?
    end

    class << self
      def [](*)
        super.convert!
      end

      def try_convert(*)
        (hash = super) && self[hash]
      end
    end
  end
end
inject(hash) click to toggle source

Injects indifferent access into a duplicate of the hash provided. See inject!

# File lib/embulk/data_source.rb, line 44
def self.inject(hash)
  inject!(hash.dup)
end
inject!(hash) click to toggle source
# File lib/embulk/data_source.rb, line 37
def self.inject!(hash)
  (class << hash; self; end).send :include, IndifferentAccess
  hash.convert!
end
try_convert(*) click to toggle source
Calls superclass method
# File lib/embulk/data_source.rb, line 30
def try_convert(*)
  (hash = super) && self[hash]
end

Public Instance Methods

convert!() click to toggle source
# File lib/embulk/data_source.rb, line 52
def convert!
  keys.each do |k|
    regular_writer convert_key(k), indifferent_value(regular_delete(k))
  end
  self
end
convert_key(key) click to toggle source
# File lib/embulk/data_source.rb, line 48
def convert_key(key)
  key.to_s
end
indifferent_access?() click to toggle source
# File lib/embulk/data_source.rb, line 101
def indifferent_access?
  true
end
indifferent_default(key = nil) click to toggle source
# File lib/embulk/data_source.rb, line 69
def indifferent_default(key = nil)
  return self[convert_key(key)] if key?(key)
  regular_default(key)
end
indifferent_delete(key) click to toggle source
# File lib/embulk/data_source.rb, line 89
def indifferent_delete(key)
  regular_delete convert_key(key)
end
indifferent_fetch(key, *args, &block) click to toggle source
# File lib/embulk/data_source.rb, line 85
def indifferent_fetch(key, *args, &block)
  regular_fetch convert_key(key), *args, &block
end
indifferent_key?(key) click to toggle source
# File lib/embulk/data_source.rb, line 93
def indifferent_key?(key)
  regular_key? convert_key(key)
end
indifferent_replace(other_hash) click to toggle source
# File lib/embulk/data_source.rb, line 105
def indifferent_replace(other_hash)
  (keys - other_hash.keys).each { |key| delete(key) }
  other_hash.each { |key, value| self[key] = value }
  self
end
indifferent_update(other_hash) click to toggle source
# File lib/embulk/data_source.rb, line 74
def indifferent_update(other_hash)
  return regular_update(other_hash) if hash_with_indifference?(other_hash)
  other_hash.each_pair do |k, v|
    self[k] = v
  end
end
indifferent_value(value) click to toggle source
# File lib/embulk/data_source.rb, line 59
def indifferent_value(value)
  if hash_lacking_indifference?(value)
    IndifferentAccess.inject!(value)
  elsif value.is_a?(::Array)
    value.replace(value.map { |e| indifferent_value(e) })
  else
    value
  end
end
indifferent_values_at(*indices) click to toggle source
# File lib/embulk/data_source.rb, line 97
def indifferent_values_at(*indices)
  indices.map { |i| self[i] }
end
indifferent_writer(key, value) click to toggle source
# File lib/embulk/data_source.rb, line 81
def indifferent_writer(key, value)
  regular_writer convert_key(key), indifferent_value(value)
end

Protected Instance Methods

hash_lacking_indifference?(other) click to toggle source
# File lib/embulk/data_source.rb, line 113
def hash_lacking_indifference?(other)
  other.is_a?(::Hash) &&
    !(other.respond_to?(:indifferent_access?) &&
      other.indifferent_access?)
end
hash_with_indifference?(other) click to toggle source
# File lib/embulk/data_source.rb, line 119
def hash_with_indifference?(other)
  other.is_a?(::Hash) &&
    other.respond_to?(:indifferent_access?) &&
    other.indifferent_access?
end