Datamapper
-
Upload
contagion -
Category
Technology
-
view
323 -
download
1
Transcript of Datamapper
Mapping class Post include DataMapper::Resource property :id, Serial property :title, String property :body, Text property :created_at, DateTime
has n, :comments end
3
Loaded Set>> @p = Post.all=> [#<Post id=1>, #<Post id=2>]
>> @p.class=> DataMapper::Collection
>> @p[0].colleaction=> [#<Post id=1>, #<Post id=2>]
7
Strategy Eager Loading
@posts = [email protected] do |p| puts p.user.nameend
2 Queriesno more n+1 or :include
8
Lazy Loading@posts = Post.all#select id,title,created_at from posts;
@posts.each do |p| puts p.body #select body from posts where ....;end
Only load what you need( :fetch in 1.0? )
9
Lazyp = Post.all
# no query happened!
p[0]#select * from posts limit 1
Wait for the kicker methods: each, map, count, etc.
10
Scopedreview = Post.all(:title.like =>‘review’ )
my_review = yes_p.all(:user_id =>my_id)
conditions are joined together when the kicker kicks in
11
Query Path
Post.all('comments.email.like' => 'contagion%' )
# SELECT "posts"."id", "posts"."created_at" FROM "posts" INNER JOIN "comments" ON ("posts"."id" = "comments"."post_id") WHERE ("comments"."email" LIKE 'contagion%') ORDER BY "posts"."id"
12
Go off the Golden Pathrepository(:default).adapter.resource_naming_convention = DataMapper::NamingConventions::Resource::Underscored
class Stock property :target_symbol, String, :key => trueproperty :target_type_id, String, :key => true
repository(:legacy) do property :target_name, Stringend
end
Custom Naming ConventionComposite Keys
Multiple databases
13
• 50 lines of code (read only)
• 1 hour (because I am stupid..)
• unified interface
• works with association !!
My Stupid MemCache Adaptor
17
Custom Type
• Serial : Integer with auto increment
• CSV : Serialize array into string
• YAML、JSON
• Custom :
• implements load / dump / typecast
19