[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ANN] traits-0.3.0

Ara.T.Howard

6/22/2005 12:18:00 PM

11 Answers

Ralph \"PJPizza\" Siegler

6/22/2005 2:07:00 PM

0

On Wed, Jun 22, 2005 at 09:18:13PM +0900, Ara.T.Howard wrote:
>
> URLS
>
> http://raa.ruby-lang.org/search.rhtml?sea...
> http://codeforpeople.com/lib/r...
>
> ABOUT
>
> traits.rb aims to be a better set of attr_* methods and encourages better
> living through meta-programming and uniform access priciples. traits.rb
> supercedes attributes.rb. why? the name is shorter ;-)

Hello Ara,

would it be possible to have a kind of observer on traits, kind of like Apple/obc/cocoa's KVO?



Ralph "PJPizza" Siegler




Ara.T.Howard

6/22/2005 2:36:00 PM

0

Ralph \"PJPizza\" Siegler

6/22/2005 3:16:00 PM

0

On Wed, Jun 22, 2005 at 11:36:10PM +0900, Ara.T.Howard wrote:
> imagining something like
>
> class C
> has 'a', 'default' => 42,
> 'pre' => proc{|this| puts "pre a : #{ this.a }"},
> 'post' => proc{|this| puts "post a : #{ this.a }"},
> end
>
> obj = C::new
> obj.a('forty-two')
>

Wow, this aspect-oriented flavor of idea is even *better* than what I was thinking of!


> or were you thinking more like
>
> class C
> some_observer = SomeObserver::new
>
> has 'a', 'default' => 42,
> 'observer' => some_observer
> end
>
> and observer.notify(self, value) would be called or something?
>

yes, that's what I was thinking, but that AO type idea is even kewler



Ralph "PJPizza" Siegler



Joel VanderWerf

6/22/2005 8:05:00 PM

0

Ara.T.Howard wrote:
...
> <========< sample/c.rb >========>
...
> class C
> class << self
> has('classname'){|this| this.name.upcase }
> end
>
> has('classname'){|this| this.class.name.downcase }
> end

Hi, Ara,
Just curious, why wouldn't #instance_eval give you the desided behavior?
In that case, you could just use 'self' instead of 'this'.


Ara.T.Howard

6/22/2005 8:13:00 PM

0

Joel VanderWerf

6/22/2005 8:54:00 PM

0

Ralph "PJPizza" Siegler wrote:
> On Wed, Jun 22, 2005 at 09:18:13PM +0900, Ara.T.Howard wrote:
>
>>URLS
>>
>> http://raa.ruby-lang.org/search.rhtml?sea...
>> http://codeforpeople.com/lib/r...
>>
>>ABOUT
>>
>> traits.rb aims to be a better set of attr_* methods and encourages better
>> living through meta-programming and uniform access priciples. traits.rb
>> supercedes attributes.rb. why? the name is shorter ;-)
>
>
> Hello Ara,
>
> would it be possible to have a kind of observer on traits, kind of like Apple/obc/cocoa's KVO?
>
>
>
> Ralph "PJPizza" Siegler

I'm not sure what those observers do, but the observable module on RAA
can be used orthogonally with any pair of methods #attr and #attr=, as
in the following example:

[~] cat foo.rb
require 'observable'
class A
def foo; 3; end
def foo=(x); p x; end
extend Observable
observable :foo
end

a = A.new

a.when_foo(Object) {|val| p "changing foo to #{val.inspect}"}

a.foo = "zap"

[~] ruby foo.rb
"changing foo to 3"
"zap"
"changing foo to \"zap\""


So presumably this could be used with traits without modifying either
library.


Ara.T.Howard

6/22/2005 9:01:00 PM

0

Ralph \"PJPizza\" Siegler

6/22/2005 9:39:00 PM

0

On Thu, Jun 23, 2005 at 05:54:16AM +0900, Joel VanderWerf wrote:
> I'm not sure what those observers do, but the observable module on RAA
> can be used orthogonally with any pair of methods #attr and #attr=, as
> in the following example:


Joel,

that's awesome, I only had known about Ruby's included Observer mixin,
not this thing.


thanks very much,


Ralph "PJPizza" Siegler




Joel VanderWerf

6/22/2005 9:45:00 PM

0

Ralph "PJPizza" Siegler wrote:
> On Thu, Jun 23, 2005 at 05:54:16AM +0900, Joel VanderWerf wrote:
>
>>I'm not sure what those observers do, but the observable module on RAA
>>can be used orthogonally with any pair of methods #attr and #attr=, as
>>in the following example:
>
>
>
> Joel,
>
> that's awesome, I only had known about Ruby's included Observer mixin,
> not this thing.

Thanks! It's especially useful in GUI code, as in the foxtails lib,
which uses it to wire up various Fox controls and views with your apps
"model" variables.


Devin Mullins

6/23/2005 1:46:00 AM

0

I'm not sure I'd ever use it, but if you're looking for ideas, I think a
protocol by which to register observers would be pretty neat.

*reads email about 'observable' library*

I mean, uh, an improvement over the observable library. Something like
this, perchance:

class C
has :a, :observers => true
has :b, :validators => true
end

c = C.new
c.observe(:a) { |old,new|
blah
}
c.observe(:a) { |old,new|
additional_blah
}
c.validate(:b) { |old,new|
blah?
}

If not blah?, then b is not changed.

I'm not sure what it should do, in the observe case, should the block
throw an exception.

Also, if you could think of a good way to allow the object to internally
add observers/validators, even if observers isn't true. Ideally, modify
the protocol to make use of Ruby access control. Maybe give them
individual private methods, such as observe-a and validate-b. Maybe,
instead, just make add_observer and add_validator private methods that
do the same thing.

Or, yeah, I could just direct these suggestions to the author of
'observable'. ;)

Devin