Module: Embulk::Impl::IndifferentAccess

Included in:
DataSource
Defined in:
embulk-core/src/main/ruby/embulk/data_source.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'embulk-core/src/main/ruby/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) ⇒ Object

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



44
45
46
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 44

def self.inject(hash)
  inject!(hash.dup)
end

.inject!(hash) ⇒ Object



37
38
39
40
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 37

def self.inject!(hash)
  (class << hash; self; end).send :include, IndifferentAccess
  hash.convert!
end

Instance Method Details

#convert!Object



52
53
54
55
56
57
# File 'embulk-core/src/main/ruby/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) ⇒ Object



48
49
50
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 48

def convert_key(key)
  key.to_s
end

#indifferent_access?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 101

def indifferent_access?
  true
end

#indifferent_default(key = nil) ⇒ Object



69
70
71
72
# File 'embulk-core/src/main/ruby/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) ⇒ Object



89
90
91
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 89

def indifferent_delete(key)
  regular_delete convert_key(key)
end

#indifferent_fetch(key, *args, &block) ⇒ Object



85
86
87
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 85

def indifferent_fetch(key, *args, &block)
  regular_fetch convert_key(key), *args, &block
end

#indifferent_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 93

def indifferent_key?(key)
  regular_key? convert_key(key)
end

#indifferent_replace(other_hash) ⇒ Object



105
106
107
108
109
# File 'embulk-core/src/main/ruby/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) ⇒ Object



74
75
76
77
78
79
# File 'embulk-core/src/main/ruby/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) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'embulk-core/src/main/ruby/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) ⇒ Object



97
98
99
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 97

def indifferent_values_at(*indices)
  indices.map { |i| self[i] }
end

#indifferent_writer(key, value) ⇒ Object



81
82
83
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 81

def indifferent_writer(key, value)
  regular_writer convert_key(key), indifferent_value(value)
end