Class: YAML::Syck::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/yip.rb

Instance Method Summary collapse

Instance Method Details

#old_transferObject



58
# File 'lib/yip.rb', line 58

alias_method :old_transfer, :transfer

#transfer(yaml_type, value) ⇒ Object

A redefinition of Syck::Loader#transfer to allow for interpolation of nodes



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yip.rb', line 62

def transfer( yaml_type, value )
  new_value = value.dup
  # If a node tree has been given (second pass), and it's a string, interpolate
  if @nodes and yaml_type =~ /str$/
    value.scan(YAML::INTERPOLATION_PATTERN) do |field,sprintf_ypath,sprintf_format,plain_ypath|
      ypath = sprintf_ypath || plain_ypath
      format = "%#{sprintf_format || 's'}"
      results = @nodes.select!( ypath )
      replace = /#{Regexp::escape field}/
      case results[0]
        when nil
          raise YAML::MissingInterpolationError::new( "Could not find any nodes matching YPATH '#{ypath}'" )
        when String
          new_value.gsub!( replace, format % results[0].to_s )
        when Numeric
          new_value.gsub!( replace, format % results[0].to_s )
      else
          new_value.gsub!( replace, format % results[0].to_yaml.sub(/^---\s*/, "\n") )
      end
    end
  end
  old_transfer( yaml_type, new_value )
end