Method: UsingYAML.add_array_extensions

Defined in:
lib/using_yaml/array.rb

.add_array_extensions(array, pathname) ⇒ Object

Calls add_extensions on all children. Also defines a save method iff this is a top-level UsingYAML object (see below for details).



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/using_yaml/array.rb', line 4

def self.add_array_extensions(array, pathname)
  # Here we define a Module to extend the array
  extensions = Module.new do
    # Iterate over the items
    array.each do |item|
      # Recursively continue to extend.
      UsingYAML.add_extensions(item)
    end
    
    # Define a save method if a pathname was supplied (only happens
    # on the top-level object - that is, example.foo will have a
    # +save+, but example.foo.bar will not).
    if pathname
      define_method(:save) do
        # Serialise using +to_yaml+
        File.open(pathname, 'w') do |f|
          f.puts self.to_yaml
        end
      end
    end
  end

  # Load in the extensions for this instance
  array.extend(extensions)
end