[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML to objects - how?

Bill Guindon

3/6/2005 9:19:00 PM

I'm trying to read in a YAML file that has a list of database specs
(name, fields, indexes, etc.) Once that's read in, I'd like to turn
them into an array of db objects. In the end, I want to loop through
the array and generate some code based on each database spec.

Is there an easy way to do this?

I've been staring at: http://whytheluckystiff.net/ruby/yaml-default...

It seems to be about the only thing I can find that discusses
object_maker and add_domain_type which seem relevant, but may very
well have nothing to do with what I'm after.

Any suggestions? Should I point out that I've never done anything
with YAML, or is that obvious by now?

thx in advance.

--
Bill Guindon (aka aGorilla)


8 Answers

Joel VanderWerf

3/6/2005 9:45:00 PM

0

Bill Guindon wrote:
> I'm trying to read in a YAML file that has a list of database specs
> (name, fields, indexes, etc.) Once that's read in, I'd like to turn
> them into an array of db objects. In the end, I want to loop through
> the array and generate some code based on each database spec.

Is the YAML file something like this?

---
-
name: Foo
fields:
- :x
- :y
- :z
indexes:
-
name: Bar
fields:
- :a
- :b
- :c
indexes:

If so, then you can just YAML.load() the file, and you'll get an array
of hashes, which you can iterate over.


Bill Guindon

3/6/2005 10:03:00 PM

0

On Mon, 7 Mar 2005 06:44:59 +0900, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:
> Bill Guindon wrote:
> > I'm trying to read in a YAML file that has a list of database specs
> > (name, fields, indexes, etc.) Once that's read in, I'd like to turn
> > them into an array of db objects. In the end, I want to loop through
> > the array and generate some code based on each database spec.
>
> Is the YAML file something like this?
>
> ---
> -
> name: Foo
> fields:
> - :x
> - :y
> - :z
> indexes:
> -
> name: Bar
> fields:
> - :a
> - :b
> - :c
> indexes:

Pretty close (should've included it)
Do I need the colons before the field names? I'm not doing that now.

Here's a couple table's worth:
---
-
name: PM_Static_SettingsDB
file: settings.dbf
fdel: false
dnoc: false
fields:
- key CHAR 254
- redir CHAR 254
- admin CHAR 254
- hidehelp BOOL
- last_tab CHAR 50

indexes:
-
file: cats.mvx
expr: PM_Static_catsDB.d.cat_id
flag: nounique, ascending
-
name: PM_Static_KeysDB
file: keys.dbf
fdel: false
dnoc: true

fields:
- name CHAR 50
- value NUMBER
indexes:
-
file: keys.mvx
expr: PM_Static_KeysDB.d.name
flag: nounique, ascending


> If so, then you can just YAML.load() the file, and you'll get an array
> of hashes, which you can iterate over.

I'll give that a shot, was wondering if there was a way to auto create
objects from the YAML load, but yeah, I could loop through them and do
it that way. I'll see how it goes.

--
Bill Guindon (aka aGorilla)


Joel VanderWerf

3/6/2005 10:47:00 PM

0

Bill Guindon wrote:
> Do I need the colons before the field names? I'm not doing that now.

Oh, not at all. I just did that because I tend to use symbols rather
than strings when I am describing attr_accessors and the like. But you
can use strings for better readability in the yaml file.

> I'll give that a shot, was wondering if there was a way to auto create
> objects from the YAML load, but yeah, I could loop through them and do
> it that way. I'll see how it goes.

If you want to autocreate, you can use the syntax for defining the class
of objects, but the YAML file will look messier. You can see how it will
look by creating some objects in ruby code, and them calling #to_yaml on
them. F'rexample:

require 'yaml'

class C
attr_accessor :x
end

c = C.new
c.x = {:foo => ["bar"] }

puts c.to_yaml

The output is

--- !ruby/object:C
x:
:foo:
- bar

If you YAML.load this, it will create an instance of C.

Alternately maybe there is some hook in the YAML parser that lets you
say "instantiate all hashes with objects of class C", but that seems
problematic if you use hashes elsewhere in the data.

Another option is to extend the YAML parser to recognize some custom
types that have less cumbersome syntax than "!ruby/object:YourClass".
You can find examples in yaml/types.rb in your ruby lib dir.


Bill Guindon

3/7/2005 7:50:00 AM

0

On Mon, 7 Mar 2005 07:46:42 +0900, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:
> Bill Guindon wrote:
> > Do I need the colons before the field names? I'm not doing that now.
>
> Oh, not at all. I just did that because I tend to use symbols rather
> than strings when I am describing attr_accessors and the like. But you
> can use strings for better readability in the yaml file.
>
> > I'll give that a shot, was wondering if there was a way to auto create
> > objects from the YAML load, but yeah, I could loop through them and do
> > it that way. I'll see how it goes.
>
> If you want to autocreate, you can use the syntax for defining the class
> of objects, but the YAML file will look messier. You can see how it will
> look by creating some objects in ruby code, and them calling #to_yaml on
> them. F'rexample:
>
> require 'yaml'
>
> class C
> attr_accessor :x
> end
>
> c = C.new
> c.x = {:foo => ["bar"] }
>
> puts c.to_yaml
>
> The output is
>
> --- !ruby/object:C
> x:
> :foo:
> - bar
>
> If you YAML.load this, it will create an instance of C.
>
> Alternately maybe there is some hook in the YAML parser that lets you
> say "instantiate all hashes with objects of class C", but that seems
> problematic if you use hashes elsewhere in the data.
>
> Another option is to extend the YAML parser to recognize some custom
> types that have less cumbersome syntax than "!ruby/object:YourClass".
> You can find examples in yaml/types.rb in your ruby lib dir.

I ended up going with your first suggestion (after getting a slightly
firmer grasp of YAML). The alternatives seem to carry too high a
price for "auto" object creation.

In the long term, I hope that changes, in the short term, looping
through what I have will work fine.

Thx for you input.

--
Bill Guindon (aka aGorilla)


Ernesto Hernandez

10/11/2009 8:15:00 PM

0

RLunfa wrote:
> Oiga, PeLotudo, para qu? mierda escribe tanto?

Oiga, Chupavarga.
Defiendes a un racista como T.Schmidt porque el es un besaculo
deFidel.
¿No tienes orgullo?

Compay Libre

John Tordillo

10/11/2009 9:23:00 PM

0

On 11 oct, 14:35, "RLunfa" <mitialagordasinestor...@gmail.com> wrote:
> Oiga, PeLotudo, para qué mierda escribe tanto?
>

Esa P mayùscula y la L del medio me dejaron intrigado . Se trata de
una nueva variedad? Exquisitez taxonòmica?

J.T.

Eugene Paul

10/12/2009 2:32:00 PM

0

On Oct 11, 8:21 am, PL <pl.nos...@pandora.be> wrote:

> Por eso  Herr SSchmidt llama "subhumano" a otras personas, especialmente
> si piensa que la persona puede ser judío.

Leonard Peikoff, un judío eminente, dice que un fetos es un 'pre-
human'. Para mí, la ironía es increíble.

Eugene Paul

PL

10/12/2009 8:16:00 PM

0

Eugene Paul wrote:
> On Oct 11, 8:21 am, PL <pl.nos...@pandora.be> wrote:
>
>> Por eso Herr SSchmidt llama "subhumano" a otras personas, especialmente
>> si piensa que la persona puede ser jud?o.
>
> Leonard Peikoff, un jud?o eminente, dice que un fetos es un 'pre-
> human'. Para m?, la iron?a es incre?ble.

para mi es asqueroso llamar a gente subhumano.
La palabra de la ideolog?a que mato a millones de gente abusando de esta
palabra.

PL