Bean setter and &block problem

Hi all,

I am writing a DSL for a Java object model using Jruby (and having a
good time doing it!), and I’m making heavy use of the “Eval’d block”
pattern. My problem is setters within those blocks. Here’s some
simplified code:

Java bean:

public class TestBean {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

JRuby:

class TestBean
def self.generate (&block)
bean = TestBean.new
bean.instance_eval &block
bean
end
end

direct_bean = TestBean.new
direct_bean.name = “bar”
puts direct_bean.name

bean = TestBean.generate do
set_name “foo”
end
puts bean.name

bean2 = TestBean.generate do
name = “baz”
end
puts bean2.name

Expected output:
bar
foo
baz

Actual output:
bar
foo
nil

I can see if i use “puts methods” within the last block that name= is
defined for TestBean within the block but it does not do what I expect
it to (call setName()). What gives? Is this the expected behavior, and
in that case why? The behavior is the same if I extend the TestBean
class instead of reopening it. Sorry for the clunky code, I’m still
working on my Ruby-fu…

Thanks,
/Mns

since I ran into the same issue during the weekend

name = “baz”

just set a local variable’name’ which is never used again. you need to
call the setter (if it exists) by
self.name = “baz”

regards, Kristian

2011/3/9 Måns af Klercker [email protected]:

private String _name_;
bean

puts bean.name

working on my Ruby-fu…


Kristian Meier + Sanuka Meier
Vadakkethu House,
Edayanmula West PO - 689532,
Pathanamthitta District, Kerala, INDIA

tel: +91 468 2319577

protect your privacy while searching the net: www.ixquick.com

         _=_
       q(-_-)p
        '_) (_`
        /__/  \
     _(<_   / )_
  (__\_\_|_/__)

Thank you Kristian for your quick answer! So if I understand you
correctly:

When called in the scope within bean.instance_eval, creating the local
variable ‘name’ has higher precedence than calling the ‘self.name=’
method? That makes ok sense I guess. It 's a little unfortunate for my
DSL, since it (sort-of) adds noise. I have used ‘set_name “baz”’
sucessfully, which is similar.

The idea of my project is of course to be able to construct a DSL for
creating Java bean objects which is clear and low on noise. ‘name =
“baz”’ seemed nice and clear :frowning: Anyone have any suggestions on how to
avoid calling set_XXX or self.XXX directly in these blocks that does
not add much boilerplate code to the DSL definition code? Might there
be Ruby mind-tricks to acheive it?

cheers,
/Mns

Pretty neat, but I have, say 20 classes, some of them with more than
50 bean properties. That’s a lot of extra code :slight_smile: I’m wondering if
there’s some dynamic way of doing this and still keep decent
performance – somtimes there are more than 1M objects in these
graphs.

cheers,
/Mns

2011/3/9 kristian [email protected]:

Your performance question aside…

You could write something which uses introspection to ask for all bean
properties and then write some code to generate a method like Kristian
described and then call that code on each property. Magic generation!

pure pseudo babble

bean.properties.each do |prop|
generate_getter_setter_for(prop)
end

Make your own java_import wrapper method and you can essentially make
this close to zero written lines (well a modest amount for the meta
programming code).

There are other considerations in this scheme like name collisions,
but it may be helpful in keeping your DSL code DRY.

-Tom

2011/3/9 Mns af Klercker [email protected]:

what I do is to define
and reduces the “noise”

sucessfully, which is similar.

public class TestBean {
bean.instance_eval &block
end
baz
class instead of reopening it. Sorry for the clunky code, I’m still

tel: +91 468 2319577

http://xircles.codehaus.org/manage_email
Pathanamthitta District, Kerala, INDIA
(__|_/)


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]

what I do is to define

def name(val =nil)
@name = val if val
@name
end

which allows me to use

name “baz”

and reduces the “noise”

regards, Kristian

2011/3/9 Måns af Klercker [email protected]:

“baz”’ seemed nice and clear :frowning: Anyone have any suggestions on how to

name = “baz”

I am writing a DSL for a Java object model using Jruby (and having a
}

name = “baz”
foo
/Måns

         _=_

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Kristian Meier + Sanuka Meier
Vadakkethu House,
Edayanmula West PO - 689532,
Pathanamthitta District, Kerala, INDIA

tel: +91 468 2319577

protect your privacy while searching the net: www.ixquick.com

         _=_
       q(-_-)p
        '_) (_`
        /__/  \
     _(<_   / )_
  (__\_\_|_/__)

Thanks a lot Kristian, I couldn’t ask for more!

cheers,
/Mns

2011/3/9 kristian [email protected]:

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