ANN: doodle 0.0.1

S

Sean O'Halpin

Doodle is a Ruby library and gem for simplifying the definition of
Ruby classes by making attributes and their properties more
declarative.

Doodle is eco-friendly =96 it does not globally modify Object, Class or
Module, nor does it pollute instances with its own instance variables
(i.e. it plays nice with yaml). Future versions will try to be even
more unobtrusive.

It is similar to but different from libraries such as Ara Howard's
attributes and traits and likewise owes something to the famous
metakoans Ruby quiz.

Features:

* initialization
o using positional arguments
o with named arguments
o by block
* defaults
* initial values
* validation at attribute and class levels
* conversions for attributes and classes
* collectors to help in defining simple DSLs
* works for classes, instances and singletons

Putting all this together, you can initialize objects like this:

event =3D Event "Festival" do
date '2008-04-01'
place "The muddy field"
place "Beer tent" do
event "Drinking"
end
end

pp event

#<Event:0x595394
@date=3D#<Date: 4909115/2,0,2299161>,
@locations=3D
[#<Location:0x5913d4 @events=3D[], @name=3D"The muddy field">,
#<Location:0x58ef58
@events=3D[#<Event:0x58d630 @locations=3D[], @name=3D"Drinking">],
@name=3D"Beer tent">],
@name=3D"Festival">

from a class definition like this:

require 'date'
require 'pp'
require 'doodle'

class Location < Doodle::Base
has :name, :kind =3D> String
has :events, :init =3D> [], :collect =3D> :Event
end

class Event < Doodle::Base
has :name, :kind =3D> String
has :date do
kind Date
default { Date.today }
must 'be >=3D today' do |value|
value >=3D Date.today
end
from String do |s|
Date.parse(s)
end
end
has :locations, :init =3D> [], :collect =3D> {:place =3D> "Location"}
end

To install:

$ sudo gem install doodle

or

C:\> gem install doodle

For more info, see http://doodle.rubyforge.org/
 
P

Peña, Botp

From: Sean O'Halpin [mailto:[email protected]]=20
# * initialization
# o using positional arguments
# o with named arguments
# o by block
# * defaults
# * initial values
# * validation at attribute and class levels
# * conversions for attributes and classes
# * collectors to help in defining simple DSLs
# * works for classes, instances and singletons

wow, at 0.0.1 version, that is awesome power.
downloading gem now..

kind regards -botp
 
J

James Adam

from a class definition like this:

class Location < Doodle::Base
has :name, :kind => String
has :events, :init => [], :collect => :Event
end

Are there any cases where you'd want to use "collect", but not ":init => []" ?

I think it'd also be really interesting to hear if you were using this
library for anything specific - importing data, for example?

Looks very interesting though!
 
S

Sean O'Halpin

Hi James!

Are there any cases where you'd want to use "collect", but not ":init => []" ?

:collect works for anything that provides a :<< method, though I've
only tested it with Array, String, StringIO and Set.
But initializing with an Array if you use :collect on its own without
specifying an initial value might be useful (though I'm not keen to
have too much 'magic' in there).
I think it'd also be really interesting to hear if you were using this
library for anything specific - importing data, for example?

Funny you should mention that :) One use is for validating
quasi-freeform text records of the form:

field1: value
field2: value

field1: value
field2: value

where both field and value have to be validated as well as the whole
record once assembled.

Another is for config files (sometimes yaml just gets too confusing
and you still have to validate anyway).
A colleague at work is using it for a DSL (which is where the Event
example comes from and what prompted me to release it as a gem).
Looks very interesting though!
Well, it's been keeping me occupied through the cold winter nights :)

Best regards,
Sean
 
S

Sean O'Halpin

Very cool. Any thoughts on serializing/persisting Doodle objects?

Aman Gupta

Well, you can serialize using YAML, e.g. using the example class
definition from the original post:

str = event.to_yaml
loaded_event = YAML::load(str)

However, this bypasses validation (because YAML::load works on the
@instance_variable level rather than accessor level).
I've just uploaded a newer version of doodle (0.0.4) that makes public
the validate! method and adds a option all=true|false to force
validation of all attributes. So you could load a dodgy YAML file and
validate it like this:

another_event = YAML::load(DATA.read)
another_event.validate!(true)

validate! will raise a Doodle::ValidationError if any validations are
violated. (The exclamation mark denotes possible exception ahead.) I'm
not entirely happy about this interface so it will probably change to
just validate! in the next version (with no flag).

The same method should work for any other low level loaders.

The next phase of work on doodle will be to see how well I can
integrate it with other libraries (like Sequel or Ambition for
example).

Regards,
Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top