0

This is a get started example of Ruby data mapper. However, how does this example illustrate the power of datamapper that separate the application logic and data persistence?

Any better example can give us that the separation can lead us do unit test more easily?

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

Thanks!

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • I think there is a general misunderstanding here. What you are talking about is the "datamapper approach" not the gem datamapper. you could use that gem to follow the datamapper pattern where you have an data object that holds the database stuff and an domain object to handle the business logic. – phoet Nov 11 '13 at 12:40
  • thanks, in the above example, is it an simple active record pattern? Can you give me a simple datamapper pattern using datamapper gem? Thanks a lot – TheOneTeam Nov 11 '13 at 13:39
  • here is an example: http://jgaskins.org/blog/2012/04/20/data-mapper-vs-active-record – phoet Nov 11 '13 at 13:44
  • thanks i have read that before, but it doesn't show a good example on how to separate the model obj and data persistence. The article example only show the model obj, how to save to db? – TheOneTeam Nov 11 '13 at 13:53

0 Answers0