ksiomelo
ksiomelo

Reputation: 1908

Can't access class instance variable set in another module

I can't access class instance variable set in another module using the

instance_variable_set("@#{key_name}", key_value)

Like in the example below:

module Neo4jMapper
  module Node
    def self.included base
      base.extend NodeClassMethods
      base.send :prepend, NodeInstanceMethods
    end
  end

 module NodeInstanceMethods

   def initialize(prototype)
     # Set neo4jId
     @_id     = prototype["self"].split('/').last.to_i

     # Set properties
     for key in self.class.keys
       key_name = key[0]
       if prototype["data"].has_key?(key_name)
         key_value = prototype["data"][key_name] 
         instance_variable_set("@#{key_name}", key_value)
       end
     end
   end
 end 
end


class Person
  include Neo4jMapper::Node
  #....
end

Instantiation:

person = Person.create({:name => "neo4j_mapper", :email => "[email protected]"})

This sets the person object with @name = "[email protected]" (viewed from the debugger). But the following line does not work:

email = person.email # ERROR: undefined method `email' for #<Person:0x007fd2038b8508>

Any ideas?

Upvotes: 0

Views: 369

Answers (1)

Dan Grahn
Dan Grahn

Reputation: 9424

You need to use attr_accessor to generate a getter and setter.

attr_accessor :email

References

Upvotes: 2

Related Questions