On Mar 21, 1:58 pm, Nels Nelson <[email protected]> wrote:
> On Mar 21, 12:32 pm, funny_falcon <[email protected]> wrote:> > because
> I cannot define classes inside of a method
> > why?
>
> <snip>
>
> That would work! :-) But I would have to subvert a Ruby idiom,
> instead of just subverting an ORM framework preference.
>
> I suppose I should have been more specific:
>
> I cannot define classes that are available to namespaces outside of
> the method, unless I refer to those classes using a variable name
> throughout the code, instead of a constant as normal.
>
> dynamic constant assignment Test = Class.new(Sequel::Model(:test))
> (SyntaxError)
>
> I would have to do:
>
> module Test
> class << self
> attr_accessor :model
> end
> end
>
> class TestSetup
> def up
> Test.model = Class.new(Sequel::Model(:test))
> end
> end
module Test
DEFINE_MODELS = proc do
class Model1 < Sequel::Model
end
end
def self.define_models
class_eval(&DEFINE_MODELS)
end
end
class TestSetup
def up
Test.define_models
end
end
> And then reference the class otherwise known as "Test" as "Test.model"
> throughout the codebase. This works, but I feel that it fails the
> "least surprise" qualifier.
I agree. For testing, I often assign classes to instance variables,
as it keeps the code clean. Plus, if you use instance_variables
instead of constants, you don't need to remove the constants on tear-
down.
> I also realize that constants can indeed be re-assigned in Ruby, using
> some meta-programming techniques and some warning-stifling, but I'm
> really not willing to go there for some simple Sequel Model demos.
That's a shame, because it isn't all that complicated, as evidenced by
the above code.
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.