Class: DbAgile::Tools::OrderedHash
- Inherits:
-
Object
- Object
- DbAgile::Tools::OrderedHash
- Defined in:
- lib/dbagile/tools/ordered_hash.rb
Instance Method Summary collapse
-
#[]=(name, value) ⇒ Object
Sets a (name,value) pair.
-
#initialize(hash = {}) ⇒ OrderedHash
constructor
Decorates a hash as an ordered hash.
-
#method_missing(name, *args, &block) ⇒ Object
Delegated to hash.
-
#to_yaml(opts = {}) ⇒ Object
Puts as YAML, maintaining order.
Constructor Details
#initialize(hash = {}) ⇒ OrderedHash
Decorates a hash as an ordered hash
6 7 8 9 |
# File 'lib/dbagile/tools/ordered_hash.rb', line 6 def initialize(hash = {}) @hash = hash @keys = hash.keys end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Delegated to hash
12 13 14 15 |
# File 'lib/dbagile/tools/ordered_hash.rb', line 12 def method_missing(name, *args, &block) @hash.send(name, *args, &block) @keys = (@keys & @hash.keys) end |
Instance Method Details
#[]=(name, value) ⇒ Object
Sets a (name,value) pair
18 19 20 21 22 23 24 |
# File 'lib/dbagile/tools/ordered_hash.rb', line 18 def []=(name, value) @hash[name] = value unless @keys.include?(name) @keys << name end value end |
#to_yaml(opts = {}) ⇒ Object
Puts as YAML, maintaining order
27 28 29 30 31 32 33 34 35 |
# File 'lib/dbagile/tools/ordered_hash.rb', line 27 def to_yaml(opts = {}) YAML::quick_emit(self, opts){|out| out.map("tag:yaml.org,2002:map") do |map| @keys.each{|key| map.add(key.to_s, @hash[key]) } end } end |