[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ANN] fattr-1.0.2

ara.t.howard

3/8/2008 4:09:00 AM


NAME
fattr.rb

INSTALL
gem install fattrs

URIS
http://codeforpeople.co...
http://rubyforge.org/projects/code...
http://codeforpeople.rubyforg...

SYNOPSIS
fattr.rb is a "fatter attr" for ruby.

fattr.rb supercedes attributes.rb as that library, even though it
added only
one method to the global namespace, collided too frequently with
user code -
in particular rails' code.

the implementation of fattr.rb borrows many of the best ideas from
the
metakoans.rb ruby quiz

http://www.rubyquiz.com/q...

in particular the solutions of Christian Neukirchen and Florian
Gross along
with concepts from the original traits.rb lib

key features provided by fattrs are

- ability to specify default values for attrs and definition
time. values
can be literal objects or blocks, which are evaluated in the
context of
self to initialize the variable

- classes remember which fattrs they've defined and this
information is
available to client code

- a whole suite of methods is defined by calls to #fattrs including
getter, setter, query (var?) and banger (var! - which forces
re-initialization from the default value/block)

- ability to define multiple fattrs at once using key => value
pairs

- fast lookup of whether or not a class has defined a certain fattr

- fattrs can be defined on objects on a per singleton basis

- getters acts as setters if an argument is given to them

- block caching, calling an fattr with a block sets the instance
variable to that block

- shortcuts for adding class/module level fattrs

all this in < 100 lines of code

HISTORY
1.0.2:
added Fattr shortcut for adding class/module level fattrs

class C
Fattr 'children' => []

def C.inherited other
(children << other).uniq!
super
end
end

class B < C
end

p C.children #=> B

1.0.0:
port from attributes.rb retaining all the same features of that
version of
attributes.rb

SAMPLES

<========< samples/a.rb >========>

~ > cat samples/a.rb

#
# basic usage is like attr, but note that fattr defines a suite
of methods
#
require 'fattr'

class C
fattr 'a'
end

c = C.new

c.a = 42
p c.a #=> 42
p 'forty-two' if c.a? #=> 'forty-two'

#
# fattrs works on object too
#
o = Object.new
o.fattr 'answer' => 42
p o.answer #=> 42

~ > ruby samples/a.rb

42
"forty-two"
42


<========< samples/b.rb >========>

~ > cat samples/b.rb

#
# default values may be given either directly or as a block which
will be
# evaluated in the context of self. in both cases (value or
block) the
# default is set only once and only if needed - it's a lazy
evaluation. the
# 'banger' method can be used to re-initialize a variable at any
point whether
# or not it's already been initialized.
#
require 'fattr'

class C
fattr :a => 42
fattr(:b){ Float a }
end

c = C.new
p c.a #=> 42
p c.b #=> 42.0

c.a = 43
p c.a #=> 43
c.a!
p c.a #=> 42

~ > ruby samples/b.rb

42
42.0
43
42


<========< samples/c.rb >========>

~ > cat samples/c.rb

#
# multiple values may by given, plain names and key/val pairs may
be mixed.
#
require 'fattr'

class C
fattrs 'x', 'y' => 0b101000, 'z' => 0b10
end

c = C.new
c.x = c.y + c.z
p c.x #=> 42

~ > ruby samples/c.rb

42


<========< samples/d.rb >========>

~ > cat samples/d.rb

#
# a nice feature is that all fattrs are enumerated in the class.
this,
# combined with the fact that the getter method is defined so as
to delegate
# to the setter when an argument is given, means bulk
initialization and/or
# fattr traversal is very easy.
#
require 'fattr'

class C
fattrs %w( x y z )

def fattrs
self.class.fattrs
end

def initialize
fattrs.each_with_index{|a,i| send a, i}
end

def to_hash
fattrs.inject({}){|h,a| h.update a => send(a)}
end

def inspect
to_hash.inspect
end
end

c = C.new
p c.fattrs
p c

c.x 'forty-two'
p c.x

~ > ruby samples/d.rb

["x", "y", "z"]
{"x"=>0, "y"=>1, "z"=>2}
"forty-two"


<========< samples/e.rb >========>

~ > cat samples/e.rb

#
# my favourite element of fattrs is that getters can also be
setters.
# this allows incredibly clean looking code like
#
require 'fattr'

class Config
fattrs %w( host port)
def initialize(&block) instance_eval &block end
end

conf = Config.new{
host 'codeforpeople.org'

port 80
}

p conf

~ > ruby samples/e.rb

#<Config:0x224d4 @port=80, @host="codeforpeople.org">


<========< samples/f.rb >========>

~ > cat samples/f.rb

#
# of course fattrs works as well at class/module level as at
instance
# level
#
require 'fattr'

module Logging
Level_names = {
0 => 'INFO',
# ...
42 => 'DEBUG',
}

class << self
fattr 'level' => 42
fattr('level_name'){ Level_names[level] }
end
end

p Logging.level
p Logging.level_name

~ > ruby samples/f.rb

42
"DEBUG"


<========< samples/g.rb >========>

~ > cat samples/g.rb

#
# you can add class/module fattrs the 'normal' way or using the
provided
# shortcut method
#
require 'fattr'

class C
class << self
fattr 'a' => 4
end

Fattr 'b' => 2
end

p [ C.a, C.b ].join

~ > ruby samples/g.rb

"42"


enjoy!

a @ http://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



5 Answers

Daniel Berger

3/8/2008 4:18:00 AM

0



On Mar 7, 9:08 pm, ara howard <ara.t.how...@gmail.com> wrote:
> NAME
> fattr.rb
>
> INSTALL
> gem install fattrs
>
> URIS
> http://codeforpeople.co...
> http://rubyforge.org/projects/code...
> http://codeforpeople.rubyforg...
>
> SYNOPSIS
> fattr.rb is a "fatter attr" for ruby.
>
> fattr.rb supercedes attributes.rb as that library, even though it
> added only
> one method to the global namespace, collided too frequently with
> user code -
> in particular rails' code.
>
> the implementation of fattr.rb borrows many of the best ideas from
> the
> metakoans.rb ruby quiz
>
> http://www.rubyquiz.com/q...
>
> in particular the solutions of Christian Neukirchen and Florian
> Gross along
> with concepts from the original traits.rb lib
>
> key features provided by fattrs are
>
> - ability to specify default values for attrs and definition
> time. values
> can be literal objects or blocks, which are evaluated in the
> context of
> self to initialize the variable
>
> - classes remember which fattrs they've defined and this
> information is
> available to client code
>
> - a whole suite of methods is defined by calls to #fattrs including
> getter, setter, query (var?) and banger (var! - which forces
> re-initialization from the default value/block)
>
> - ability to define multiple fattrs at once using key => value
> pairs
>
> - fast lookup of whether or not a class has defined a certain fattr
>
> - fattrs can be defined on objects on a per singleton basis
>
> - getters acts as setters if an argument is given to them
>
> - block caching, calling an fattr with a block sets the instance
> variable to that block
>
> - shortcuts for adding class/module level fattrs
>

Interesting. What about data types?

class C
fattr 'a' => Integer
end

c = C.new
c.a = 42 # ok
c.a = 'hello' # TypeError

You may have this, but I didn't see it in the samples or readme (but
very well may have missed it).

Regards,

Dan

ara.t.howard

3/8/2008 5:57:00 AM

0


On Mar 7, 2008, at 9:18 PM, Daniel Berger wrote:

> Interesting. What about data types?
>
> class C
> fattr 'a' => Integer
> end
>
> c = C.new
> c.a = 42 # ok
> c.a = 'hello' # TypeError
>
> You may have this, but I didn't see it in the samples or readme (but
> very well may have missed it).

it is *far* more heavyweight, but see

http://codeforp...lib/ruby/traits/traits-0....

it also supports aspects, validation, and a ton of other stuff

cheers.

a @ http://codeforp...
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




Peña, Botp

3/8/2008 6:13:00 AM

0

From: ara howard [mailto:ara.t.howard@gmail.com]=20
# it is *far* more heavyweight, but see

if it isn't slow, i'd prefer it over fattr any time. mixing the two =
seems to make my head shake a little :) in fact, i would have even =
alias fattr to traits :))

thanks for traits, btw. it is cool

kind regards -botp


ara.t.howard

3/8/2008 6:37:00 AM

0


On Mar 7, 2008, at 11:13 PM, Pe=F1a, Botp wrote:

> # it is *far* more heavyweight, but see
>
> if it isn't slow, i'd prefer it over fattr any time. mixing the two =20=

> seems to make my head shake a little :) in fact, i would have even =20=

> alias fattr to traits :))
>
> thanks for traits, btw. it is cool

heh - yeah traits is powerful and robust, but the code makes *my* head =20=

spin. i'd like to rewrite in the style of fattr - but supporting all =20=

the same features - not time attm tho...

cheers.

a @ http://draw...
--
sleep is the best meditation.
h.h. the 14th dalai lama




Sean O'Halpin

3/8/2008 11:18:00 PM

0

You might also be interested in doodle (http://doodle.rub...)
which I've just released.

$ xmpfilter examples/foo.rb
require 'rubygems'
require 'doodle'

class Foo < Doodle::Base
has :name, :kind => String
end

foo = Foo "Bar"
foo # => #<Foo:0x10a1378 @name="Bar">
baz = Foo 1
# ~> -:10: name must be String - got Fixnum(1) (Doodle::ValidationError)

Regards,
Sean

On Sat, Mar 8, 2008 at 4:18 AM, Daniel Berger <djberg96@gmail.com> wrote:
>
>
>
> On Mar 7, 9:08 pm, ara howard <ara.t.how...@gmail.com> wrote:
> > NAME
> > fattr.rb
> >
> > INSTALL
> > gem install fattrs
> >
> > URIS
> > http://codeforpeople.co...
> > http://rubyforge.org/projects/code...
> > http://codeforpeople.rubyforg...
> >
> > SYNOPSIS
> > fattr.rb is a "fatter attr" for ruby.
> >
> > fattr.rb supercedes attributes.rb as that library, even though it
> > added only
> > one method to the global namespace, collided too frequently with
> > user code -
> > in particular rails' code.
> >
> > the implementation of fattr.rb borrows many of the best ideas from
> > the
> > metakoans.rb ruby quiz
> >
> > http://www.rubyquiz.com/q...
> >
> > in particular the solutions of Christian Neukirchen and Florian
> > Gross along
> > with concepts from the original traits.rb lib
> >
> > key features provided by fattrs are
> >
> > - ability to specify default values for attrs and definition
> > time. values
> > can be literal objects or blocks, which are evaluated in the
> > context of
> > self to initialize the variable
> >
> > - classes remember which fattrs they've defined and this
> > information is
> > available to client code
> >
> > - a whole suite of methods is defined by calls to #fattrs including
> > getter, setter, query (var?) and banger (var! - which forces
> > re-initialization from the default value/block)
> >
> > - ability to define multiple fattrs at once using key => value
> > pairs
> >
> > - fast lookup of whether or not a class has defined a certain fattr
> >
> > - fattrs can be defined on objects on a per singleton basis
> >
> > - getters acts as setters if an argument is given to them
> >
> > - block caching, calling an fattr with a block sets the instance
> > variable to that block
> >
> > - shortcuts for adding class/module level fattrs
> >
>
> Interesting. What about data types?
>
> class C
> fattr 'a' => Integer
> end
>
> c = C.new
> c.a = 42 # ok
> c.a = 'hello' # TypeError
>
> You may have this, but I didn't see it in the samples or readme (but
> very well may have missed it).
>
> Regards,
>
> Dan
>
>