attribution 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12444834b2a05df15ec10e3841f3fd0ef9ae2f83
4
- data.tar.gz: 3f49033c574560af94cb1052ca833661c2801bf8
3
+ metadata.gz: 76c80b1626e3995ef953a1ecf966eeb2e0cf0935
4
+ data.tar.gz: 60f0bb25b5470c74f7ba0ba38991c447a8cc63b5
5
5
  SHA512:
6
- metadata.gz: c463c0d115207d3fae0c8b3ea00c6faaeea0e15c33f4c8a8700bd4f13fefa1f05e9adfcf883b840063e93db5eea2c573ad2d45c2dd5599fc70a0446622788701
7
- data.tar.gz: 3153c9287a83a0b64b75982b1ab520dec38da9d46a251fda1a7bcb50618146f8b788c6338082d76ac3aedab4a1ac50cefd49cb464ad29eb6161631fa4e998634
6
+ metadata.gz: e29e6caae51db60d0e910bb568f92d93a125881968a2e97018990d87655f6e189d4ca20d8dcf5ae43e18fae2c0b7ac40228e6a9cddb3568094a3fd23bf064bdf
7
+ data.tar.gz: 75f106172c77d966576e169e565fed38976cff5274ad1e3189f56dc892e4077b56800c365e0e017926083767d375f6083d1811c374b12f9b73dd22e18aa1f022
data/attribution.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "attribution"
5
- gem.version = "0.7.1"
5
+ gem.version = "0.8.0"
6
6
  gem.authors = ["Paul Barry"]
7
7
  gem.email = ["mail@paulbarry.com"]
8
8
  gem.description = %q{Add attributes to Ruby objects}
@@ -1,3 +1,3 @@
1
1
  module Attribution
2
- VERSION = "0.7.1"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/attribution.rb CHANGED
@@ -197,14 +197,38 @@ module Attribution
197
197
  end
198
198
  end
199
199
 
200
+ # Associations
201
+
202
+ # @return [Array<Hash>] The associations for this class
203
+ def associations
204
+ @associations ||= if superclass && superclass.respond_to?(:associations)
205
+ superclass.associations.dup
206
+ else
207
+ []
208
+ end
209
+ end
210
+
211
+ # Defines an association
212
+ #
213
+ # @param [String] name The name of the association
214
+ # @param [Symbol] type The type of the association
215
+ # @param [Hash{Symbol => Object}] metadata The metadata for the association
216
+ def add_association(name, type, metadata={})
217
+ associations << (metadata || {}).merge(:name => name.to_sym, :type => type.to_sym)
218
+ end
219
+
220
+ # @param [Boolean] autoload_associations Enable/Disable autoloading of
221
+ # associations for this class and all subclasses.
200
222
  def autoload_associations(autoload_associations)
201
223
  @autoload_associations = autoload_associations
202
224
  end
203
225
 
226
+ # @return [Boolean] autoload_associations Whether or not this will
227
+ # autoload associations.
204
228
  def autoload_associations?
205
229
  if defined? @autoload_associations
206
230
  @autoload_associations
207
- elsif superclass && superclass.respond_to?(:autoload_associations?)
231
+ elsif superclass.respond_to?(:autoload_associations?)
208
232
  superclass.autoload_associations?
209
233
  else
210
234
  true
@@ -212,8 +236,8 @@ module Attribution
212
236
  end
213
237
 
214
238
  # Association macros
215
-
216
- # Defines an attribute that is a reference to another Attribution class.
239
+ #
240
+ # Defines an association that is a reference to another Attribution class.
217
241
  #
218
242
  # @param [Symbol] association_name The name of the association
219
243
  # @param [Hash] metadata Extra information about the association.
@@ -223,7 +247,7 @@ module Attribution
223
247
  # foo_id
224
248
  id_getter = "#{association_name}_id".to_sym
225
249
  add_attribute(id_getter, :integer, metadata)
226
-
250
+ add_association association_name, :belongs_to, metadata
227
251
  association_class_name = metadata.try(:fetch, :class_name, [name.split('::')[0..-2].join('::'), association_name.to_s.classify].reject(&:blank?).join('::'))
228
252
 
229
253
  define_method(id_getter) do
@@ -279,7 +303,7 @@ module Attribution
279
303
  end
280
304
  end
281
305
 
282
- # Defines an attribute that is a reference to an Array of another Attribution class.
306
+ # Defines an association that is a reference to an Array of another Attribution class.
283
307
  #
284
308
  # @param [Symbol] association_name The name of the association
285
309
  # @param [Hash] metadata Extra information about the association.
@@ -287,11 +311,12 @@ module Attribution
287
311
  # defaults to a class name based on the association name
288
312
  def has_many(association_name, metadata={})
289
313
 
314
+ add_association association_name, :has_many, metadata
315
+
290
316
  association_class_name = metadata.try(:fetch, :class_name, [name.split('::')[0..-2].join('::'), association_name.to_s.singularize.classify].reject(&:blank?).join('::'))
291
317
 
292
318
  # foos
293
319
  define_method(association_name) do |*query|
294
-
295
320
  # TODO: Support a more generic version of lazy-loading
296
321
  begin
297
322
  association_class = Object.const_get(association_class_name)
@@ -39,7 +39,6 @@ class Book
39
39
  time :updated_at
40
40
  time_zone :time_zone
41
41
 
42
- belongs_to :book
43
42
  has_many :chapters
44
43
  has_many :readers
45
44
 
@@ -343,4 +342,11 @@ class AttributionTest < Test::Unit::TestCase
343
342
  product.orders
344
343
  end
345
344
 
345
+ def test_associations
346
+ assert_equal [
347
+ { :name => :book, :type => :belongs_to },
348
+ { :name => :pages, :type => :has_many }
349
+ ], Chapter.associations
350
+ end
351
+
346
352
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribution
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Barry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-24 00:00:00.000000000 Z
11
+ date: 2013-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport