Class: Configurable::OrderedHashPatch

Inherits:
Hash
  • Object
show all
Defined in:
lib/configurable/ordered_hash_patch.rb

Overview

Beginning with ruby 1.9, Hash tracks the order of insertion and methods like each_pair return pairs in order. Configurable leverages this feature to keep configurations in order for the command line documentation produced by ConfigParser.

Pre-1.9 ruby implementations require a patched Hash that tracks insertion order. This very thin subclass of hash does that for ASET insertions and each_pair. OrderedHashPatches are used as the configurations object in Configurable classes for pre-1.9 ruby implementations and for nothing else.

OrderedHashPatch is only loaded for pre-1.9 ruby implementations.

Instance Method Summary collapse

Constructor Details

#initializeOrderedHashPatch

Returns a new instance of OrderedHashPatch.



15
16
17
18
# File 'lib/configurable/ordered_hash_patch.rb', line 15

def initialize
  super
  @insertion_order = []
end

Instance Method Details

#[]=(key, value) ⇒ Object

ASET insertion, tracking insertion order.



21
22
23
24
# File 'lib/configurable/ordered_hash_patch.rb', line 21

def []=(key, value)
  @insertion_order << key unless @insertion_order.include?(key)
  super
end

#each_pairObject

Yields each key-value pair to the block in insertion order.



34
35
36
37
38
# File 'lib/configurable/ordered_hash_patch.rb', line 34

def each_pair
  keys.each do |key|
    yield(key, fetch(key))
  end
end

#initialize_copy(orig) ⇒ Object

Ensures the insertion order of duplicates is separate from parents.



48
49
50
51
# File 'lib/configurable/ordered_hash_patch.rb', line 48

def initialize_copy(orig)
  super
  @insertion_order = orig.instance_variable_get(:@insertion_order).dup
end

#keysObject

Keys, sorted into insertion order



27
28
29
30
31
# File 'lib/configurable/ordered_hash_patch.rb', line 27

def keys
  super.sort_by do |key|
    @insertion_order.index(key) || length
  end
end

#merge!(another) ⇒ Object

Merges another into self in a way that preserves insertion order.



41
42
43
44
45
# File 'lib/configurable/ordered_hash_patch.rb', line 41

def merge!(another)
  another.each_pair do |key, value|
    self[key] = value
  end
end

#to_yaml(opts = {}) ⇒ Object

Overridden to preserve insertion order by serializing self as an array of [key, value] pairs.



70
71
72
73
74
75
76
77
78
# File 'lib/configurable/ordered_hash_patch.rb', line 70

def to_yaml( opts = {} )
  YAML::quick_emit( object_id, opts ) do |out|
    out.seq( taguri, to_yaml_style ) do |seq|
      each_pair do |key, value|
        seq.add( [key, value] )
      end
    end
  end
end

#yaml_initialize(tag, val) ⇒ Object

Overridden to load an array of [key, value] pairs in order (see to_yaml). The default behavior for loading from a hash of key-value pairs is preserved, but the insertion order will not be preserved.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/configurable/ordered_hash_patch.rb', line 56

def yaml_initialize( tag, val )
  @insertion_order ||= []
 
  if Array === val
    val.each do |k, v|
      self[k] = v
    end
  else
    super
  end
end