Forum: Ruby YAML, ERB & nested maps problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
13 (Guest)
on 2006-04-22 14:47
(Received via mailing list)
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.
13 (Guest)
on 2006-04-22 16:07
(Received via mailing list)
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 ...
Trans (Guest)
on 2006-04-22 19:13
(Received via mailing list)
13 <one.three <at> 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.
unknown (Guest)
on 2006-04-22 19:29
(Received via mailing list)
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!
unknown (Guest)
on 2006-04-22 19:29
(Received via mailing list)
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!
13 (Guest)
on 2006-04-22 20:46
(Received via mailing list)
> 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.
13 (Guest)
on 2006-04-22 20:52
(Received via mailing list)
>   end
> Or there abouts should work. (There's also #tab and #indent methods too, btw).
>
> T.

Thanks for pointing me to this Facets library !
unknown (Guest)
on 2006-04-22 21:20
(Received via mailing list)
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!
13 (Guest)
on 2006-04-22 21:45
(Received via mailing list)
> 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 !
unknown (Guest)
on 2006-04-22 21:51
(Received via mailing list)
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 :-)


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!
13 (Guest)
on 2006-04-22 21:57
(Received via mailing list)
Yeah, forgot that ;)
This topic is locked and can not be replied to.