Custom class contructors?

5 views
Skip to first unread message

Peter Neubauer

unread,
Dec 2, 2009, 9:32:54 AM12/2/09
to neo4jrb
Hi there,
sorry for sounding stupid, but I am trying to do something like
class Event
include Neo4j::NodeMixin
property :name, :time
end

And then providing a custom constructor that initializes these
properties. It seems

class Event
include Neo4j::NodeMixin
property :name, :time
def initialize(_name)
self[:name] = _name
end
end
Event.new('hej')

is not working:

undefined method `set_property' for nil:NilClass (NoMethodError)
./features/step_definitions/add_steps.rb:21:in `initialize'
./features/step_definitions/add_steps.rb:26:in `new'


Any hint on the right way?

/peter

GTalk: neubauer.peter
Skype peter.neubauer
Phone +46 704 106975
LinkedIn http://www.linkedin.com/in/neubauer
Twitter http://twitter.com/peterneubauer

http://www.neo4j.org - Relationships count.
http://www.linkedprocess.org - Distributed computing on LinkedData scale

Craig Taverner

unread,
Dec 2, 2009, 9:35:23 AM12/2/09
to neo...@googlegroups.com
I don't know neo4j.rb all that well, but following Ruby conventions the following should work:
  self.name = _name
The 'property :name' field almost certainly creates a setter method for storing this property.


--

You received this message because you are subscribed to the Google Groups "neo4jrb" group.
To post to this group, send email to neo...@googlegroups.com.
To unsubscribe from this group, send email to neo4jrb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/neo4jrb?hl=en.



Peter Ferne

unread,
Dec 2, 2009, 10:16:43 AM12/2/09
to neo...@googlegroups.com
> Any hint on the right way?


I don't know if this is the "right" way, and it doesn't answer your
question directly, but I have been defining my own mixin:

module Scrump
module NodeMixin
def initialize *args
super
shortname = self.class.name.split('::').last.downcase

self.class.subref_node.relationships.outgoing("every_#{shortname}") <<
self
self["created_at"] = Time.now.to_f
end
end

class Feedback
include Neo4j::NodeMixin
include NodeMixin
...
end
end

--
Peter Ferne, http://jivatechnology.com, http://twitter.com/petef



Andreas Ronge

unread,
Dec 2, 2009, 11:40:18 AM12/2/09
to neo...@googlegroups.com
Hi

There is an example for this here:
http://github.com/andreasronge/neo4j/blob/master/test/neo4j/node_mixin_spec.rb
You need to call super first to initialize the node (I think).

I think the following should work:

class Event
include Neo4j::NodeMixin
property :name, :time
def initialize(name)
super
self.name = name
end
end

The initialize method is called both when loading the node from
filesystem and when creating a new node.
There is a method that is only called when creating the object: init_node

So maybe this is better:

class Event
include Neo4j::NodeMixin
property :name, :time

def init_node(*args)
self.name = args[0]
end
end

/Andreas

On Wed, Dec 2, 2009 at 3:32 PM, Peter Neubauer
<peter.n...@neotechnology.com> wrote:

Andreas Ronge

unread,
Dec 2, 2009, 11:52:07 AM12/2/09
to neo...@googlegroups.com
I've added a lighthouse ticket on making the init_node take one or
more argument instead of a list (*args)
It should support this:

class Event
include Neo4j::NodeMixin
property :name, :time

def init_node(name)
self[:name] = name
end
end

http://neo4j.lighthouseapp.com/projects/15548-neo4j/tickets/98-neo4jinit-method-should-take-one-or-more-args
Who wants to implement this :-)

/Andreas

Peter Neubauer

unread,
Dec 2, 2009, 12:53:37 PM12/2/09
to neo4jrb
Yes,
that is exactly what I was initially trying and suspecting :)

I can try but I am not fluent in Ruby, so I guess someone else can jump in?

/peter

GTalk: neubauer.peter
Skype peter.neubauer
Phone +46 704 106975
LinkedIn http://www.linkedin.com/in/neubauer
Twitter http://twitter.com/peterneubauer

http://www.neo4j.org - Relationships count.
http://www.linkedprocess.org - Distributed computing on LinkedData scale



Peter Neubauer

unread,
Dec 2, 2009, 1:10:20 PM12/2/09
to neo4jrb
Yes,
thanks for all the advise!

class Event
include Neo4j::NodeMixin
property :name, :time
def initialize _name
super
self.name = _name
end
end

Event.new 'Stuff added'

does the trick. Nice!

/peter

GTalk: neubauer.peter
Skype peter.neubauer
Phone +46 704 106975
LinkedIn http://www.linkedin.com/in/neubauer
Twitter http://twitter.com/peterneubauer

http://www.neo4j.org - Relationships count.
http://www.linkedprocess.org - Distributed computing on LinkedData scale



On Wed, Dec 2, 2009 at 5:40 PM, Andreas Ronge <andrea...@jayway.se> wrote:
Reply all
Reply to author
Forward
0 new messages