C
Corey Konrad
wow this attr_accessor thing is really great i can see this saving ALOT
of typing.
of typing.
Indeed!
This is the kind of stuff that really brings out the "automagic".
Mr. Hansson really did us all a good thing with knowing Ruby and
knowing ORM and applying these concepts well with a language that
really does lend itself to it. Things are nice now. Sometimes things
are slow, but everyone can be glad things will only get better!
Oh, I don't mean to give him credit for that, but for recognizing
that type of construct and how it is like the ORM stuff in
ActiveRecord! (unless I'm mistaken) It is a good example of the kinds
of things Rails developers should probably get familiar with
conceptually.
(I'm working through your book and the Agile book at
the same time, the different approaches are interesting. Each book
seems to fill in gaps for the other, so I can't say anyone should
start with one or the other, but both.)
Corey said:wow this attr_accessor thing is really great i can see this saving ALOT
of typing.
If you want to check that assigned values are valid at the
time of assignment (instead of later during validation),
please consider using "typed_attr" from my "chattr" gem.
gem install chattr
class Foo
typed_attr String :bar do |v| v.size <= 20 end
end
Now you can't assign a string that's nil or more than 20 characters
without an exception being thrown.
David said:Wouldn't it be better to call that checked_attr? I'm just thinking
that it's mostly checking the class, rather than the type
You have the choice of using class checking or block-based checking,
which can perform whatever type checks are relevant, including respond_to?
and related checks. The parameter list consists of a list of any of four
kinds of things:
* a Class (which is used for subsequent class checks),
* nil (indicating that nil values are allowed),
* a Symbol, which creates an attribute having the defined checks,
* any other value, which is used as a default value.
The block, if any, is applied to all created attributes.
Class checking is just a shorthand limited form of type checking. I'm
fully aware if the distinction, but this method encourages type checking,
not just class checking, so I think it's correctly named.
The gem also contains "array_attr", which creates a subclass of Array that
overrides every mutating method of Array to provide the same checking for
array attributes as well (with the exception of nil and default values).
David said:I think there's a problem with array_attr; I'm getting:
/opt/local/lib/ruby/gems/1.8/gems/chattr-0.9.0/lib/chattr.rb:378:in
`throw': wrong number of arguments (0 for 1) (ArgumentError)
when I try to violate the constraint on an array. It looks like
there's a throw just kind of sitting there on its own.
John said:Indeed!
This is the kind of stuff that really brings out the "automagic".
Mr. Hansson really did us all a good thing with knowing Ruby and
knowing ORM and applying these concepts well with a language that
really does lend itself to it. Things are nice now. Sometimes things
are slow, but everyone can be glad things will only get better!
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.