[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ANN: doodle 0.0.1

Sean O'Halpin

3/8/2008 11:13:00 PM

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

5 Answers

Peña, Botp

3/9/2008 5:44:00 AM

0

From: Sean O'Halpin [mailto:sean.ohalpin@gmail.com]=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

james.adam

3/10/2008 11:08:00 AM

0

On 08/03/2008, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:
> 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!


--
* J *
~

Sean O'Halpin

3/12/2008 2:11:00 AM

0

Hi James!

On Mon, Mar 10, 2008 at 11:08 AM, James Adam <james.adam@gmail.com> wrote:
>
> 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

Aman Gupta

3/14/2008 10:52:00 PM

0

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

Aman Gupta
--
Posted via http://www.ruby-....

Sean O'Halpin

3/15/2008 1:51:00 PM

0

On Fri, Mar 14, 2008 at 10:51 PM, Aman Gupta <ruby-forum@tmm1.net> wrote:
> 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