这是Ruby数据映射器的入门示例。但是,此示例如何说明将应用程序逻辑和数据持久性分开的datamapper的强大功能?
任何更好的例子都可以告诉我们,分离可以让我们更容易进行单元测试吗?
class Post
include DataMapper::Resource
property :id, Serial # An auto-increment integer key
property :title, String # A varchar type string, for short strings
property :body, Text # A text block, for longer string data.
property :created_at, DateTime # A DateTime, for any date you might like.
end
# create makes the resource immediately
@post = Post.create(
:title => "My first DataMapper post",
:body => "A lot of text ...",
:created_at => Time.now
)
# Or new gives you it back unsaved, for more operations
@post = Post.new(:title => ..., ...)
@post.save # persist the resource
谢谢!