Dm Intro

663 views

Published on

An introduction to DataMapper

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
663
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dm Intro

  1. 1. DataMapper An Introduction
  2. 2. Similarities • Associations • Validations • Migrations
  3. 3. Identity Map
  4. 4. @parent = Parent.first(:name => 'John') @parent.children.each do |child| child.parent.should == @parent end # => true
  5. 5. Properties
  6. 6. class User include DataMapper::Resource property :name, String, :index => true, :nullable => false, :length => 30 property :email, String, :nullable => false, :length => 255, :format => :email_address property :password, String, :nullable => false property :remarks, Text end
  7. 7. Custom Types
  8. 8. Custom Data-types • Csv • Enum • EpochTime • FilePath • Flag • IPAddress • URI • Yaml • Regex
  9. 9. Lazy Loading
  10. 10. class Post .. property :body, Text, :lazy => false property :summary, Text, :lazy => [:show] property :subtitle, String, :lazy => [:show] property :views, Integer, :lazy => [:count] .. end
  11. 11. Multiple Repositories
  12. 12. class Page include DataMapper::Resource property :id, Serial property :name, String repository(:legacy) do property :name, String, :field => 'title' end end Page.all(:repository => :legacy)
  13. 13. Auto Validations
  14. 14. property :email, String, :length => 255, :nullable => false # => email varchar(255) not null (create table SQL) # => validate_present :email # => validate_length :email, :max => 255
  15. 15. Migrations
  16. 16. • Auto Migrate • Auto Upgrade • Migration
  17. 17. Finding Records
  18. 18. exhibitions = Exhibition.all(:run_time.gt => 2, :run_time.lt => 5) # => SQL conditions: 'run_time > 1 AND run_time < 5'
  19. 19. class Zoo # all the keys and property setup here def self.open all(:open => true) end def self.big all(:animal_count.gte => 1000) end end big_open_zoos = Zoo.big.open
  20. 20. big_open_zoos = Zoo.all(:open => true).all(:animal_count.gte => 1000)
  21. 21. big_open_zoos = Zoo.all(:open => true).all(:animal_count.gte => 1000) # => This line of code generate zero sql big_open_zoos.each do |zoo| puts zoo.name end # => This generates one sql.
  22. 22. Kicker methods • #each • #first • #last • #count • #map • etc
  23. 23. Strategic Eager Loading
  24. 24. Query Path
  25. 25. Person.all(quot;addresses.street.likequot; => '%marina%') SELECT quot;peoplequot;.quot;idquot;, quot;peoplequot;.quot;namequot; FROM quot;peoplequot; INNER JOIN quot;addressesquot; ON (quot;peoplequot;.quot;idquot; = quot;addressesquot;.quot;person_idquot;) WHERE (quot;addressesquot;.quot;streetquot; LIKE '%marina%') ORDER BY quot;peoplequot;.quot;idquot;
  26. 26. Questions?

×