13 <one.three <at> gmail.com> writes:

> 
> Hi,
> 
> I'm trying to genereate YAML fixtures with ERB, but I can't get it to
> work with nested maps. Here is my code:
> 
> $ cat currencies.yml
> first:
>   id: 1
>   filename: dummy1.png
>   params:
>     <%= { :dates => ['2006-04-20'], :currencies => ['USD'] }.to_yaml %>
> 
> Here is what i get:
> 
> $ erb -r yaml currencies.yml
> 
> first:
>   id: 1
>   filename: dummy1.png
>   params:
>     ---
> :currencies:
> - USD
> :dates:
> - "2006-04-20"
> 
> In the nested map (params column) only the first line is indented. How
> can I configure  YAML to properly indent other lines too ? I have
> tried to set param :Indent => 4, but without any success. Or maybe
> there ar some other methods how can I generate fixtures such files ?
> 
> Help me, please.

You've picked an interesting way to be generating YAML --with ERB?

Beyond that you'll need to get rid of the initial '---' and then indent
properly. Facets has the #tabto method you can use, though I'm not sure in where
you have to put the code:

  require 'facet/string/tabto'

  class String
    def to_yaml_fragment(n)
      self.to_yaml.sub('---','').tabto(n)
    end
  end

then

  first:
    id: 1
    filename: dummy1.png
    params:
      <%= { :dates => ['2006-04-20'], :currencies => ['USD']
}.to_yaml_fragment(4) %>

Or there abouts should work. (There's also #tab and #indent methods too, btw).

T.