这是 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
谢谢!