ANN: doodle 0.0.1

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 – 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 = Event “Festival” do
date ‘2008-04-01’
place “The muddy field”
place “Beer tent” do
event “Drinking”
end
end

pp event

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

from a class definition like this:

require ‘date’
require ‘pp’
require ‘doodle’

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

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

To install:

$ sudo gem install doodle

or

C:> gem install doodle

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

From: Sean O’Halpin [mailto:[email protected]]

* 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

On 08/03/2008, Sean O’Halpin [email protected] 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!

Hi James!

On Mon, Mar 10, 2008 at 11:08 AM, James A. [email protected]
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 :slight_smile: 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 :slight_smile:

Best regards,
Sean

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

Aman G.

On Fri, Mar 14, 2008 at 10:51 PM, Aman G. [email protected]
wrote:

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

Aman G.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs