friendly_postgres 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. data/.document +2 -0
  2. data/.gitignore +26 -0
  3. data/APACHE-LICENSE +202 -0
  4. data/CHANGELOG.md +19 -0
  5. data/CONTRIBUTORS.md +7 -0
  6. data/LICENSE +20 -0
  7. data/README.md +265 -0
  8. data/Rakefile +68 -0
  9. data/TODO.md +5 -0
  10. data/VERSION +1 -0
  11. data/examples/friendly.yml +7 -0
  12. data/friendly.gemspec +232 -0
  13. data/lib/friendly.rb +54 -0
  14. data/lib/friendly/associations.rb +7 -0
  15. data/lib/friendly/associations/association.rb +34 -0
  16. data/lib/friendly/associations/set.rb +37 -0
  17. data/lib/friendly/attribute.rb +91 -0
  18. data/lib/friendly/boolean.rb +10 -0
  19. data/lib/friendly/cache.rb +24 -0
  20. data/lib/friendly/cache/by_id.rb +33 -0
  21. data/lib/friendly/config.rb +5 -0
  22. data/lib/friendly/data_store.rb +72 -0
  23. data/lib/friendly/document.rb +257 -0
  24. data/lib/friendly/document_table.rb +56 -0
  25. data/lib/friendly/index.rb +73 -0
  26. data/lib/friendly/memcached.rb +48 -0
  27. data/lib/friendly/named_scope.rb +17 -0
  28. data/lib/friendly/newrelic.rb +6 -0
  29. data/lib/friendly/query.rb +42 -0
  30. data/lib/friendly/scope.rb +100 -0
  31. data/lib/friendly/scope_proxy.rb +45 -0
  32. data/lib/friendly/sequel_monkey_patches.rb +34 -0
  33. data/lib/friendly/storage.rb +31 -0
  34. data/lib/friendly/storage_factory.rb +24 -0
  35. data/lib/friendly/storage_proxy.rb +103 -0
  36. data/lib/friendly/table.rb +15 -0
  37. data/lib/friendly/table_creator.rb +48 -0
  38. data/lib/friendly/time.rb +14 -0
  39. data/lib/friendly/translator.rb +32 -0
  40. data/lib/friendly/uuid.rb +148 -0
  41. data/rails/init.rb +3 -0
  42. data/spec/config.yml.example +7 -0
  43. data/spec/fakes/data_store_fake.rb +29 -0
  44. data/spec/fakes/database_fake.rb +12 -0
  45. data/spec/fakes/dataset_fake.rb +28 -0
  46. data/spec/fakes/document.rb +18 -0
  47. data/spec/fakes/serializer_fake.rb +12 -0
  48. data/spec/fakes/time_fake.rb +12 -0
  49. data/spec/integration/ad_hoc_scopes_spec.rb +42 -0
  50. data/spec/integration/basic_object_lifecycle_spec.rb +114 -0
  51. data/spec/integration/batch_insertion_spec.rb +29 -0
  52. data/spec/integration/convenience_api_spec.rb +25 -0
  53. data/spec/integration/count_spec.rb +12 -0
  54. data/spec/integration/default_value_spec.rb +15 -0
  55. data/spec/integration/find_via_cache_spec.rb +101 -0
  56. data/spec/integration/finder_spec.rb +64 -0
  57. data/spec/integration/has_many_spec.rb +18 -0
  58. data/spec/integration/index_spec.rb +57 -0
  59. data/spec/integration/named_scope_spec.rb +34 -0
  60. data/spec/integration/pagination_spec.rb +63 -0
  61. data/spec/integration/scope_chaining_spec.rb +22 -0
  62. data/spec/integration/table_creator_spec.rb +64 -0
  63. data/spec/integration/write_through_cache_spec.rb +53 -0
  64. data/spec/spec.opts +1 -0
  65. data/spec/spec_helper.rb +103 -0
  66. data/spec/unit/associations/association_spec.rb +57 -0
  67. data/spec/unit/associations/set_spec.rb +43 -0
  68. data/spec/unit/attribute_spec.rb +105 -0
  69. data/spec/unit/cache_by_id_spec.rb +102 -0
  70. data/spec/unit/cache_spec.rb +21 -0
  71. data/spec/unit/config_spec.rb +4 -0
  72. data/spec/unit/data_store_spec.rb +188 -0
  73. data/spec/unit/document_spec.rb +358 -0
  74. data/spec/unit/document_table_spec.rb +126 -0
  75. data/spec/unit/friendly_spec.rb +25 -0
  76. data/spec/unit/index_spec.rb +196 -0
  77. data/spec/unit/memcached_spec.rb +114 -0
  78. data/spec/unit/named_scope_spec.rb +16 -0
  79. data/spec/unit/query_spec.rb +104 -0
  80. data/spec/unit/scope_proxy_spec.rb +44 -0
  81. data/spec/unit/scope_spec.rb +113 -0
  82. data/spec/unit/storage_factory_spec.rb +59 -0
  83. data/spec/unit/storage_proxy_spec.rb +218 -0
  84. data/spec/unit/translator_spec.rb +96 -0
  85. data/website/index.html +210 -0
  86. data/website/scripts/clipboard.swf +0 -0
  87. data/website/scripts/shBrushAS3.js +61 -0
  88. data/website/scripts/shBrushBash.js +66 -0
  89. data/website/scripts/shBrushCSharp.js +67 -0
  90. data/website/scripts/shBrushColdFusion.js +102 -0
  91. data/website/scripts/shBrushCpp.js +99 -0
  92. data/website/scripts/shBrushCss.js +93 -0
  93. data/website/scripts/shBrushDelphi.js +57 -0
  94. data/website/scripts/shBrushDiff.js +43 -0
  95. data/website/scripts/shBrushErlang.js +54 -0
  96. data/website/scripts/shBrushGroovy.js +69 -0
  97. data/website/scripts/shBrushJScript.js +52 -0
  98. data/website/scripts/shBrushJava.js +59 -0
  99. data/website/scripts/shBrushJavaFX.js +60 -0
  100. data/website/scripts/shBrushPerl.js +74 -0
  101. data/website/scripts/shBrushPhp.js +91 -0
  102. data/website/scripts/shBrushPlain.js +35 -0
  103. data/website/scripts/shBrushPowerShell.js +76 -0
  104. data/website/scripts/shBrushPython.js +66 -0
  105. data/website/scripts/shBrushRuby.js +57 -0
  106. data/website/scripts/shBrushScala.js +53 -0
  107. data/website/scripts/shBrushSql.js +68 -0
  108. data/website/scripts/shBrushVb.js +58 -0
  109. data/website/scripts/shBrushXml.js +71 -0
  110. data/website/scripts/shCore.js +30 -0
  111. data/website/scripts/shLegacy.js +30 -0
  112. data/website/styles/friendly.css +103 -0
  113. data/website/styles/help.png +0 -0
  114. data/website/styles/ie.css +35 -0
  115. data/website/styles/magnifier.png +0 -0
  116. data/website/styles/page_white_code.png +0 -0
  117. data/website/styles/page_white_copy.png +0 -0
  118. data/website/styles/print.css +29 -0
  119. data/website/styles/printer.png +0 -0
  120. data/website/styles/screen.css +257 -0
  121. data/website/styles/shCore.css +330 -0
  122. data/website/styles/shThemeDefault.css +173 -0
  123. data/website/styles/shThemeDjango.css +176 -0
  124. data/website/styles/shThemeEclipse.css +190 -0
  125. data/website/styles/shThemeEmacs.css +175 -0
  126. data/website/styles/shThemeFadeToGrey.css +177 -0
  127. data/website/styles/shThemeMidnight.css +175 -0
  128. data/website/styles/shThemeRDark.css +175 -0
  129. metadata +302 -0
@@ -0,0 +1,68 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "friendly_postgres"
8
+ gem.summary = %Q{NoSQL with MySQL in Ruby}
9
+ gem.description = %Q{}
10
+ gem.email = "jamesgolick@gmail.com"
11
+ gem.homepage = "http://friendlyorm.com"
12
+ gem.authors = ["James Golick"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "cucumber", ">= 0"
15
+ gem.add_development_dependency "jferris-mocha"
16
+ gem.add_development_dependency "memcached"
17
+ gem.add_dependency "sequel", ">= 3.7.0"
18
+ gem.add_dependency "json_pure"
19
+ gem.add_dependency "activesupport"
20
+ gem.add_dependency "will_paginate"
21
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
+ end
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
25
+ end
26
+
27
+ require 'spec/rake/spectask'
28
+ Spec::Rake::SpecTask.new(:spec) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.spec_files = FileList['spec/**/*_spec.rb']
31
+ end
32
+
33
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
34
+ spec.libs << 'lib' << 'spec'
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :spec => :check_dependencies
40
+
41
+ begin
42
+ require 'cucumber/rake/task'
43
+ Cucumber::Rake::Task.new(:features)
44
+
45
+ task :features => :check_dependencies
46
+ rescue LoadError
47
+ task :features do
48
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
49
+ end
50
+ end
51
+
52
+ task :default => :spec
53
+
54
+ require 'rake/rdoctask'
55
+ Rake::RDocTask.new do |rdoc|
56
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
57
+
58
+ rdoc.rdoc_dir = 'rdoc'
59
+ rdoc.title = "friendly #{version}"
60
+ rdoc.rdoc_files.include('README*')
61
+ rdoc.rdoc_files.include('lib/**/*.rb')
62
+ end
63
+
64
+ desc "Sync website."
65
+ task :website do
66
+ `rsync -avz website/* friendlyorm.com:/var/www/friendlyorm.com`
67
+ end
68
+
data/TODO.md ADDED
@@ -0,0 +1,5 @@
1
+ Ideas
2
+ =====
3
+
4
+ * Setup observers
5
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.3
@@ -0,0 +1,7 @@
1
+ development:
2
+ :adapter: "mysql"
3
+ :host: "localhost"
4
+ :user: "root"
5
+ :password: "swordfish"
6
+ :database: "friendly_development"
7
+
@@ -0,0 +1,232 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{friendly}
8
+ s.version = "0.4.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Golick"]
12
+ s.date = %q{2009-12-24}
13
+ s.description = %q{}
14
+ s.email = %q{jamesgolick@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "APACHE-LICENSE",
23
+ "CHANGELOG.md",
24
+ "CONTRIBUTORS.md",
25
+ "LICENSE",
26
+ "README.md",
27
+ "Rakefile",
28
+ "TODO.md",
29
+ "VERSION",
30
+ "examples/friendly.yml",
31
+ "friendly.gemspec",
32
+ "lib/friendly.rb",
33
+ "lib/friendly/associations.rb",
34
+ "lib/friendly/associations/association.rb",
35
+ "lib/friendly/associations/set.rb",
36
+ "lib/friendly/attribute.rb",
37
+ "lib/friendly/boolean.rb",
38
+ "lib/friendly/cache.rb",
39
+ "lib/friendly/cache/by_id.rb",
40
+ "lib/friendly/config.rb",
41
+ "lib/friendly/data_store.rb",
42
+ "lib/friendly/document.rb",
43
+ "lib/friendly/document_table.rb",
44
+ "lib/friendly/index.rb",
45
+ "lib/friendly/memcached.rb",
46
+ "lib/friendly/named_scope.rb",
47
+ "lib/friendly/newrelic.rb",
48
+ "lib/friendly/query.rb",
49
+ "lib/friendly/scope.rb",
50
+ "lib/friendly/scope_proxy.rb",
51
+ "lib/friendly/sequel_monkey_patches.rb",
52
+ "lib/friendly/storage.rb",
53
+ "lib/friendly/storage_factory.rb",
54
+ "lib/friendly/storage_proxy.rb",
55
+ "lib/friendly/table.rb",
56
+ "lib/friendly/table_creator.rb",
57
+ "lib/friendly/time.rb",
58
+ "lib/friendly/translator.rb",
59
+ "lib/friendly/uuid.rb",
60
+ "rails/init.rb",
61
+ "spec/config.yml.example",
62
+ "spec/fakes/data_store_fake.rb",
63
+ "spec/fakes/database_fake.rb",
64
+ "spec/fakes/dataset_fake.rb",
65
+ "spec/fakes/document.rb",
66
+ "spec/fakes/serializer_fake.rb",
67
+ "spec/fakes/time_fake.rb",
68
+ "spec/integration/ad_hoc_scopes_spec.rb",
69
+ "spec/integration/basic_object_lifecycle_spec.rb",
70
+ "spec/integration/batch_insertion_spec.rb",
71
+ "spec/integration/convenience_api_spec.rb",
72
+ "spec/integration/count_spec.rb",
73
+ "spec/integration/default_value_spec.rb",
74
+ "spec/integration/find_via_cache_spec.rb",
75
+ "spec/integration/finder_spec.rb",
76
+ "spec/integration/has_many_spec.rb",
77
+ "spec/integration/index_spec.rb",
78
+ "spec/integration/named_scope_spec.rb",
79
+ "spec/integration/pagination_spec.rb",
80
+ "spec/integration/scope_chaining_spec.rb",
81
+ "spec/integration/table_creator_spec.rb",
82
+ "spec/integration/write_through_cache_spec.rb",
83
+ "spec/spec.opts",
84
+ "spec/spec_helper.rb",
85
+ "spec/unit/associations/association_spec.rb",
86
+ "spec/unit/associations/set_spec.rb",
87
+ "spec/unit/attribute_spec.rb",
88
+ "spec/unit/cache_by_id_spec.rb",
89
+ "spec/unit/cache_spec.rb",
90
+ "spec/unit/config_spec.rb",
91
+ "spec/unit/data_store_spec.rb",
92
+ "spec/unit/document_spec.rb",
93
+ "spec/unit/document_table_spec.rb",
94
+ "spec/unit/friendly_spec.rb",
95
+ "spec/unit/index_spec.rb",
96
+ "spec/unit/memcached_spec.rb",
97
+ "spec/unit/named_scope_spec.rb",
98
+ "spec/unit/query_spec.rb",
99
+ "spec/unit/scope_proxy_spec.rb",
100
+ "spec/unit/scope_spec.rb",
101
+ "spec/unit/storage_factory_spec.rb",
102
+ "spec/unit/storage_proxy_spec.rb",
103
+ "spec/unit/translator_spec.rb",
104
+ "website/index.html",
105
+ "website/scripts/clipboard.swf",
106
+ "website/scripts/shBrushAS3.js",
107
+ "website/scripts/shBrushBash.js",
108
+ "website/scripts/shBrushCSharp.js",
109
+ "website/scripts/shBrushColdFusion.js",
110
+ "website/scripts/shBrushCpp.js",
111
+ "website/scripts/shBrushCss.js",
112
+ "website/scripts/shBrushDelphi.js",
113
+ "website/scripts/shBrushDiff.js",
114
+ "website/scripts/shBrushErlang.js",
115
+ "website/scripts/shBrushGroovy.js",
116
+ "website/scripts/shBrushJScript.js",
117
+ "website/scripts/shBrushJava.js",
118
+ "website/scripts/shBrushJavaFX.js",
119
+ "website/scripts/shBrushPerl.js",
120
+ "website/scripts/shBrushPhp.js",
121
+ "website/scripts/shBrushPlain.js",
122
+ "website/scripts/shBrushPowerShell.js",
123
+ "website/scripts/shBrushPython.js",
124
+ "website/scripts/shBrushRuby.js",
125
+ "website/scripts/shBrushScala.js",
126
+ "website/scripts/shBrushSql.js",
127
+ "website/scripts/shBrushVb.js",
128
+ "website/scripts/shBrushXml.js",
129
+ "website/scripts/shCore.js",
130
+ "website/scripts/shLegacy.js",
131
+ "website/styles/friendly.css",
132
+ "website/styles/help.png",
133
+ "website/styles/ie.css",
134
+ "website/styles/magnifier.png",
135
+ "website/styles/page_white_code.png",
136
+ "website/styles/page_white_copy.png",
137
+ "website/styles/print.css",
138
+ "website/styles/printer.png",
139
+ "website/styles/screen.css",
140
+ "website/styles/shCore.css",
141
+ "website/styles/shThemeDefault.css",
142
+ "website/styles/shThemeDjango.css",
143
+ "website/styles/shThemeEclipse.css",
144
+ "website/styles/shThemeEmacs.css",
145
+ "website/styles/shThemeFadeToGrey.css",
146
+ "website/styles/shThemeMidnight.css",
147
+ "website/styles/shThemeRDark.css"
148
+ ]
149
+ s.homepage = %q{http://friendlyorm.com}
150
+ s.rdoc_options = ["--charset=UTF-8"]
151
+ s.require_paths = ["lib"]
152
+ s.rubygems_version = %q{1.3.5}
153
+ s.summary = %q{NoSQL with MySQL in Ruby}
154
+ s.test_files = [
155
+ "spec/fakes/data_store_fake.rb",
156
+ "spec/fakes/database_fake.rb",
157
+ "spec/fakes/dataset_fake.rb",
158
+ "spec/fakes/document.rb",
159
+ "spec/fakes/serializer_fake.rb",
160
+ "spec/fakes/time_fake.rb",
161
+ "spec/integration/ad_hoc_scopes_spec.rb",
162
+ "spec/integration/basic_object_lifecycle_spec.rb",
163
+ "spec/integration/batch_insertion_spec.rb",
164
+ "spec/integration/convenience_api_spec.rb",
165
+ "spec/integration/count_spec.rb",
166
+ "spec/integration/default_value_spec.rb",
167
+ "spec/integration/find_via_cache_spec.rb",
168
+ "spec/integration/finder_spec.rb",
169
+ "spec/integration/has_many_spec.rb",
170
+ "spec/integration/index_spec.rb",
171
+ "spec/integration/named_scope_spec.rb",
172
+ "spec/integration/pagination_spec.rb",
173
+ "spec/integration/scope_chaining_spec.rb",
174
+ "spec/integration/table_creator_spec.rb",
175
+ "spec/integration/write_through_cache_spec.rb",
176
+ "spec/spec_helper.rb",
177
+ "spec/unit/associations/association_spec.rb",
178
+ "spec/unit/associations/set_spec.rb",
179
+ "spec/unit/attribute_spec.rb",
180
+ "spec/unit/cache_by_id_spec.rb",
181
+ "spec/unit/cache_spec.rb",
182
+ "spec/unit/config_spec.rb",
183
+ "spec/unit/data_store_spec.rb",
184
+ "spec/unit/document_spec.rb",
185
+ "spec/unit/document_table_spec.rb",
186
+ "spec/unit/friendly_spec.rb",
187
+ "spec/unit/index_spec.rb",
188
+ "spec/unit/memcached_spec.rb",
189
+ "spec/unit/named_scope_spec.rb",
190
+ "spec/unit/query_spec.rb",
191
+ "spec/unit/scope_proxy_spec.rb",
192
+ "spec/unit/scope_spec.rb",
193
+ "spec/unit/storage_factory_spec.rb",
194
+ "spec/unit/storage_proxy_spec.rb",
195
+ "spec/unit/translator_spec.rb"
196
+ ]
197
+
198
+ if s.respond_to? :specification_version then
199
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
200
+ s.specification_version = 3
201
+
202
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
203
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
204
+ s.add_development_dependency(%q<cucumber>, [">= 0"])
205
+ s.add_development_dependency(%q<jferris-mocha>, [">= 0"])
206
+ s.add_development_dependency(%q<memcached>, [">= 0"])
207
+ s.add_runtime_dependency(%q<sequel>, [">= 3.7.0"])
208
+ s.add_runtime_dependency(%q<json_pure>, [">= 0"])
209
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
210
+ s.add_runtime_dependency(%q<will_paginate>, [">= 0"])
211
+ else
212
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
213
+ s.add_dependency(%q<cucumber>, [">= 0"])
214
+ s.add_dependency(%q<jferris-mocha>, [">= 0"])
215
+ s.add_dependency(%q<memcached>, [">= 0"])
216
+ s.add_dependency(%q<sequel>, [">= 3.7.0"])
217
+ s.add_dependency(%q<json_pure>, [">= 0"])
218
+ s.add_dependency(%q<activesupport>, [">= 0"])
219
+ s.add_dependency(%q<will_paginate>, [">= 0"])
220
+ end
221
+ else
222
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
223
+ s.add_dependency(%q<cucumber>, [">= 0"])
224
+ s.add_dependency(%q<jferris-mocha>, [">= 0"])
225
+ s.add_dependency(%q<memcached>, [">= 0"])
226
+ s.add_dependency(%q<sequel>, [">= 3.7.0"])
227
+ s.add_dependency(%q<json_pure>, [">= 0"])
228
+ s.add_dependency(%q<activesupport>, [">= 0"])
229
+ s.add_dependency(%q<will_paginate>, [">= 0"])
230
+ end
231
+ end
232
+
@@ -0,0 +1,54 @@
1
+ require 'friendly/associations'
2
+ require 'friendly/attribute'
3
+ require 'friendly/boolean'
4
+ require 'friendly/cache'
5
+ require 'friendly/cache/by_id'
6
+ require 'friendly/config'
7
+ require 'friendly/data_store'
8
+ require 'friendly/document'
9
+ require 'friendly/document_table'
10
+ require 'friendly/index'
11
+ require 'friendly/memcached'
12
+ require 'friendly/named_scope'
13
+ require 'friendly/query'
14
+ require 'friendly/sequel_monkey_patches'
15
+ require 'friendly/scope'
16
+ require 'friendly/scope_proxy'
17
+ require 'friendly/storage_factory'
18
+ require 'friendly/storage_proxy'
19
+ require 'friendly/translator'
20
+ require 'friendly/uuid'
21
+
22
+ require 'json/pure'
23
+ require 'will_paginate/collection'
24
+
25
+ module Friendly
26
+ class << self
27
+ attr_accessor :datastore, :db, :cache
28
+
29
+ def configure(config)
30
+ self.db = Sequel.connect(config)
31
+ self.datastore = DataStore.new(db)
32
+ end
33
+
34
+ def batch
35
+ begin
36
+ datastore.start_batch
37
+ yield
38
+ datastore.flush_batch
39
+ ensure
40
+ datastore.reset_batch
41
+ end
42
+ end
43
+
44
+ def create_tables!
45
+ Document.create_tables!
46
+ end
47
+ end
48
+
49
+ class Error < RuntimeError; end
50
+ class RecordNotFound < Error; end
51
+ class MissingIndex < Error; end
52
+ class NoConverterExists < Friendly::Error; end
53
+ class NotSupported < Friendly::Error; end
54
+ end
@@ -0,0 +1,7 @@
1
+ require 'friendly/associations/association'
2
+ require 'friendly/associations/set'
3
+
4
+ module Friendly
5
+ module Associations
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ module Friendly
2
+ module Associations
3
+ class Association
4
+ attr_reader :owner_klass, :name
5
+
6
+ def initialize(owner_klass, name, options = {})
7
+ @owner_klass = owner_klass
8
+ @name = name
9
+ @class_name = options[:class_name]
10
+ @foreign_key = options[:foreign_key]
11
+ end
12
+
13
+ def klass
14
+ @klass ||= class_name.constantize
15
+ end
16
+
17
+ def foreign_key
18
+ @foreign_key ||= [owner_klass_name, :id].join("_").to_sym
19
+ end
20
+
21
+ def class_name
22
+ @class_name ||= name.to_s.classify
23
+ end
24
+
25
+ def owner_klass_name
26
+ owner_klass.name.to_s.underscore.singularize
27
+ end
28
+
29
+ def scope(document)
30
+ klass.scope(foreign_key => document.id)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ module Friendly
2
+ module Associations
3
+ class Set
4
+ attr_reader :klass, :association_klass, :associations
5
+
6
+ def initialize(klass, association_klass = Association)
7
+ @klass = klass
8
+ @association_klass = association_klass
9
+ @associations = {}
10
+ end
11
+
12
+ def add(name, options = {})
13
+ associations[name] = association_klass.new(klass, name, options)
14
+ add_association_accessor(name)
15
+ end
16
+
17
+ def get_scope(name, document)
18
+ get(name).scope(document)
19
+ end
20
+
21
+ def get(name)
22
+ associations[name]
23
+ end
24
+
25
+ protected
26
+ def add_association_accessor(name)
27
+ klass.class_eval do
28
+ eval <<-__END__
29
+ def #{name}
30
+ self.class.association_set.get_scope(:#{name}, self)
31
+ end
32
+ __END__
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end