Class: Middleman::S3Sync::IndifferentHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/middleman/s3_sync/indifferent_hash.rb

Overview

A simple hash wrapper that provides string/symbol indifferent access This replaces the Map gem dependency with native Ruby functionality

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Provide dot notation access to hash values



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 43

def method_missing(method, *args, &block)
  key = method.to_s
  if key.end_with?('=')
    # Handle setter: hash.key = value
    self[key.chop] = args.first
  elsif has_key?(key)
    # Handle getter: hash.key
    self[key]
  else
    super
  end
end

Class Method Details

.from_hash(hash) ⇒ Object

Create an IndifferentHash from a regular hash



34
35
36
37
38
39
40
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 34

def self.from_hash(hash)
  new_hash = new
  hash.each do |key, value|
    new_hash[key] = value
  end
  new_hash
end

Instance Method Details

#[](key) ⇒ Object

Override [] to provide indifferent access



12
13
14
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 12

def [](key)
  super(normalize_key(key))
end

#[]=(key, value) ⇒ Object

Override []= to store with normalized keys



17
18
19
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 17

def []=(key, value)
  super(normalize_key(key), value)
end

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

Override fetch to provide indifferent access



22
23
24
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 22

def fetch(key, *args, &block)
  super(normalize_key(key), *args, &block)
end

#has_key?(key) ⇒ Boolean Also known as: key?, include?

Override has_key? to work with normalized keys

Returns:

  • (Boolean)


27
28
29
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 27

def has_key?(key)
  super(normalize_key(key))
end

#normalize_key(key) ⇒ Object

Convert keys to strings for consistent access



7
8
9
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 7

def normalize_key(key)
  key.to_s
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/middleman/s3_sync/indifferent_hash.rb', line 56

def respond_to_missing?(method, include_private = false)
  key = method.to_s.sub(/=$/, '')
  has_key?(key) || super
end