YAML, ERB & nested maps problem


#1

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.


#2

On 4/22/06, 13 removed_email_address@domain.invalid wrote:

<%= { :dates => ['2006-04-20'], :currencies => ['USD'] }.to_yaml %>

:currencies:


Martins

Ok. I’ve managed to create a quick hack:

<%
def fix(lines)

strip yaml document separator and pad each line with 4 spaces

lines.select { |s| s !~ /^—/ }.collect { |s| s.insert(0, ’ ')
}.join
end
%>

another:
id: 2
filename: dummy2.png
params:
<%= fix({ :dates => [‘2006-04-20’], :currencies => [‘RUB’, ‘HKD’]
}.to_yaml) %>

I know there are some better ways to solve my problem …


#3

13 <one.three gmail.com> writes:

params:

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.


#4

Hi –

On Sat, 22 Apr 2006, 13 wrote:

<%= { :dates => [‘2006-04-20’], :currencies => [‘USD’] }.to_yaml %>
: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.

Can you just do this?

first:
id: 1
filename: dummy1.png
params:
dates: [‘2006-04-20’]
currencies: [‘USD’]

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!


#5

Can you just do this?

first:
id: 1
filename: dummy1.png
params:
dates: [‘2006-04-20’]
currencies: [‘USD’]

David

I need to generate random data for testing. Dates will be between
first and last date of current month, and random currency code.


#6

Hi –

On Sun, 23 Apr 2006, Trans wrote:

id: 1
filename: dummy1.png
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?

In Rails, the test fixture files are YAML files that get pre-processed
through ERb.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!


#7

Hi –

On Sun, 23 Apr 2006, 13 wrote:

I need to generate random data for testing. Dates will be between
first and last date of current month, and random currency code.

How about:


params:
currencies: <%= random_array.inspect %>

or something like that.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!


#8

How about:
params:
currencies: <%= random_array.inspect %>

or something like that.

David

Wow. Thanks, David. This is simple and straight forward, just what I
need !

somewhere outside fixture do the initialization of date array

today = Date.today
first = Date.new(today.year, today.mon, 1)
last = (first >> 1) - 1
dates = (first…last).collect { |d| d.to_s }

Then in *.yml

params:
<%= dates.rand_subset %> # rand_subset is from facets library

Again, thanks !


#9

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

T.

Thanks for pointing me to this Facets library !


#10

Hi –

On Sun, 23 Apr 2006, 13 wrote:

somewhere outside fixture do the initialization of date array

today = Date.today
first = Date.new(today.year, today.mon, 1)
last = (first >> 1) - 1
dates = (first…last).collect { |d| d.to_s }

Then in *.yml

params:
<%= dates.rand_subset %> # rand_subset is from facets library

I think you’ll want to call .inspect; otherwise it will all be strung
together as a string.

Again, thanks !

Glad to help :slight_smile:

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!


#11

Yeah, forgot that :wink: