def reference(name, model)
      reader = :"#{name}_id"
      writer = :"#{name}_id="

      attribute reader, accessors: :reader
      index reader

      define_method(writer) do |value|
        @_memo.delete(name)
        @attributes[reader] = value
      end

      define_method(:"#{name}=") do |object|
        assert_valid_class object, model

        send(writer, object ? object.id : nil)
        @_memo[name] = object
      end

      define_method(name) do
        @_memo[name] ||= begin
                           model = Ork::Utils.const(self.class, model)
                           model[send(reader)]
                         end
      end
    end
def reference(name, model)
      reader = :"#{name}_id"
      writer = :"#{name}_id="

      attribute reader, accessors: :reader
      index reader

      define_method(writer) do |value|
        @_memo.delete(name)
        @attributes[reader] = value
      end

      define_method(:"#{name}=") do |object|
        assert_valid_class object, model

        send(writer, object ? object.id : nil)
        @_memo[name] = object
      end

      define_method(name) do
        @_memo[name] ||= begin
                           model = Ork::Utils.const(self.class, model)
                           model[send(reader)]
                         end
      end
    end
A macro for defining an attribute, an index, and an accessor
 for a given model.

 Example:

   class Post
     include Ork::Document

     reference :user, :User
   end

   # It's the same as:

   class Post
     include Ork::Document

     attribute :user_id
     index :user_id

     def user
       @_memo[:user] ||= User[user_id]
     end

     def user=(user)
       self.user_id = user.id
       @_memo[:user] = user
     end

     def user_id=(user_id)
       @_memo.delete(:user_id)
       self.user_id = user_id
     end
   end