Question

I'm using DataMapper 1.2.0 and trying to define a custom property type to save in the database. I'm trying to understand how I'm supposed to do this, but I keep getting a NoMethodError.

The object is a Dog, but this type comes from a gem I don't control so I can't make it inherit from DataMapper::Property::Object, so I think I need to create a new type which will be the custom property(DatabaseDog). My understanding from the documentation is that it needs two methods:

  • #load(value) takes a string from the database and returns the Dog it represents
  • #dump(value) takes a Dog and returns a string that will be persisted in the database

The fact that I keep getting an error makes me think I'm misunderstanding this.

How should I create this custom type properly?

Edit: I changed the dump method a bit and found it keeps receiving a string. Why is #dump receiving a string as an argument? Shouldn't it be receiving the Dog object that I'm assigning?

test.rb

# The setup

require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'

class Dog
  attr_accessor :name
end

class DatabaseDog < DataMapper::Property::Text
  def load(value)
    d = Dog.new
    d.name = value
    d
  end

  def dump(value)
    puts "got here: #{value.inspect}"
    if value.nil?
      value
    else
      value.name
    end
  end
end

class User
  include DataMapper::Resource
  property :id, Serial, key: true
  property :pet, DatabaseDog
end

DataMapper.setup(:default, "sqlite3://#{File.join(Dir.pwd, "users.db")}")
DataMapper.finalize
DataMapper.auto_upgrade!


# Finally, the test

ein = Dog.new
ein.name = "Ein"

spike = User.create
spike.pet = ein

spike.save #=> NoMethodError

Error

got here: nil
got here: "#<Dog:0x007fca399b7d40>"
./test.rb:19:in `dump': undefined method `name' for "#<Dog:0x007fba19960c00>":String (NoMethodError)
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/property.rb:700:in `valid?'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/dirty.rb:88:in `block in valid_attributes?'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/dirty.rb:87:in `each_key'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/dirty.rb:87:in `valid_attributes?'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/dirty.rb:21:in `commit'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:956:in `_persist'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:987:in `block in update_with_hooks'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:984:in `catch'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:984:in `update_with_hooks'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:1021:in `save_self'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:1006:in `block in _save'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:1222:in `run_once'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:1005:in `_save'
        from (...)/test/.bundle/gems/dm-core-1.2.0/lib/dm-core/resource.rb:405:in `save'
        from ./test.rb:42:in `<main>'
Was it helpful?

Solution

According to the documentation DatabaseDog should inherit from DataMapper::Property::Object instead of DataMapper::Property::Text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top