maiha-habto 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/README +76 -0
  2. metadata +53 -0
data/README ADDED
@@ -0,0 +1,76 @@
1
+ Habto
2
+ =====
3
+
4
+ Has and belongs to one
5
+
6
+ This basically acts as habtm, but has two new features.
7
+
8
+ 1. Singular named accessor
9
+ 2. Association counter cache
10
+
11
+
12
+ Singular named accessor
13
+ =======================
14
+
15
+ We can use not only plural named accessor but also singular one.
16
+
17
+ class User
18
+ habto :email
19
+ end
20
+
21
+ User.new.emails => [] # normal habtm accessor
22
+ User.new.email => nil # useful for handling singular data
23
+
24
+
25
+ Association counter cache
26
+ =========================
27
+
28
+ :count option allows use to prefetch count of target in one find method,
29
+ where the join table is used as counter cache.
30
+
31
+ class Keyword
32
+ habto :image
33
+ habto :board
34
+ end
35
+
36
+ class Image
37
+ habto :keyword
38
+ end
39
+
40
+ class Board
41
+ habto :keyword
42
+ end
43
+
44
+
45
+ ### Normal operation
46
+
47
+ Keyword.find(1)
48
+ => #<Keyword:0xf715348c @attributes={"name"=>"cute", "id"=>"1"}>
49
+
50
+ ### Get the count information by :count option with association names
51
+
52
+ Keyword.find(1, :count=>:images)
53
+ => #<Keyword:0xf707dae4 @attributes={"name"=>"cute", "image_count"=>"4", "id"=>"1", "keyword_id"=>"1"}>
54
+ Keyword.find(1, :count=>:boards)
55
+ => #<Keyword:0xf707a560 @attributes={"board_count"=>"2", "name"=>"cute", "id"=>"1", "keyword_id"=>"1"}>
56
+
57
+ ### Fetch all count information by :count option with true (same as :count=>[:images, :boards])
58
+
59
+ Keyword.find(1, :count=>true)
60
+ => #<Keyword:0xf7084880 @attributes={"board_count"=>"2", "name"=>"cute", "image_count"=>"4", "id"=>"1", "keyword_id"=>"1"}>
61
+
62
+
63
+ Test
64
+ ====
65
+
66
+ 1. create a new rails project for test
67
+ 2. prepare files
68
+ ln -s vendor/plugins/habto/spec
69
+ rm -rf app/models
70
+ ln -s ../spec/app/models app/models
71
+ ln -s ../spec/migrate db/migrate
72
+ 3. run migrate
73
+ 4. rake spec
74
+
75
+
76
+ Copyright (c) 2008 maiha@wota.jp, released under the MIT license
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maiha-habto
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - maiha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-10 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email: maiha@wota.jp
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ has_rdoc: true
27
+ homepage: http://github.com/maiha/habto
28
+ post_install_message:
29
+ rdoc_options: []
30
+
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ version:
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 1.2.0
49
+ signing_key:
50
+ specification_version: 2
51
+ summary: ""
52
+ test_files: []
53
+