YAML, ERB & nested maps problem

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.

--
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 ...

···

On 4/22/06, 13 <one.three@gmail.com> wrote:

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.

--
Martins

--
Martins

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.

Hi --

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.

Can you just do this?

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

David

···

On Sat, 22 Apr 2006, 13 wrote:

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale! Ruby for Rails
Paper version coming in early May!

Hi --

···

On Sun, 23 Apr 2006, Trans wrote:

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?

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

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale! Ruby for Rails
Paper version coming in early May!

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.

···

--
Martins

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.

Thanks for pointing me to this Facets library !

···

--
Martins

Hi --

···

On Sun, 23 Apr 2006, 13 wrote:

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.

How about:

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

or something like that.

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale! Ruby for Rails
Paper version coming in early May!

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 !

···

--
Martins

Hi --

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

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

···

On Sun, 23 Apr 2006, 13 wrote:

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale! Ruby for Rails
Paper version coming in early May!

Yeah, forgot that :wink:

···

On 4/22/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Sun, 23 Apr 2006, 13 wrote:

>> 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

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 (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale! Ruby for Rails
Paper version coming in early May!