attribution 0.6.5 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: abcb3ede33930d68c99d76201000eea2a878ba72
4
- data.tar.gz: 78d1eb1a1e21148853afbd7edc6b7c4e67bdc0a7
3
+ metadata.gz: 12280ee9715f6fab2bb30351d1bb4db0fb7ff3b8
4
+ data.tar.gz: d90c899d05ae2475a49b4acd971041c2517b066f
5
5
  SHA512:
6
- metadata.gz: f3c00d1d2eaa859e537daf1fc2b73f3a17a327b3d19efcd686308584ed090792fe0efa4f47483d04805b5bc31f0919ca3f8934fd985e5f407543e3da1f7a2f33
7
- data.tar.gz: 6da0d3f42ae5c6d1f1483c75157509d16ac0947d1f0cffab831d80fdd23cd2a0acca833f12f5d79c463a49025d4f27a491560e4420f17efee419cdbec1de6897
6
+ metadata.gz: d896f36c33b15cf12912f07453dfadf4894e80044ed1e975f94ef8965a3d72a3f6fc8e1b55fa4208a0eb2eae8b67c4eb6b8ca6e6fc000363dbb5d601e32b5081
7
+ data.tar.gz: 8ec63499d6ed0238ae23990fb5e3f5d89f1d3ee4ec48ee06839e4a72ab882c7b26aec3ccff48ec026bb860555b583d7c372a325625227469da020b41d5fe8fe3
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.6.5"
5
+ gem.version = "0.7.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.6.5"
2
+ VERSION = "0.7.0"
3
3
  end
data/lib/attribution.rb CHANGED
@@ -197,6 +197,10 @@ module Attribution
197
197
  end
198
198
  end
199
199
 
200
+ def autoload_associations?
201
+ true
202
+ end
203
+
200
204
  # Association macros
201
205
 
202
206
  # Defines an attribute that is a reference to another Attribution class.
@@ -236,14 +240,13 @@ module Attribution
236
240
  instance_variable_get("@#{association_name}")
237
241
  elsif id = instance_variable_get("@#{association_name}_id")
238
242
 
239
- # TODO: Support a more generic version of lazy-loading
240
243
  begin
241
244
  association_class = Object.const_get(association_class_name)
242
245
  rescue NameError => ex
243
246
  raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_class_name} does not exist")
244
247
  end
245
248
 
246
- if association_class.respond_to?(:find)
249
+ if self.class.autoload_associations? && association_class.respond_to?(:find)
247
250
  instance_variable_set("@#{association_name}", association_class.find(id))
248
251
  end
249
252
  else
@@ -290,11 +293,11 @@ module Attribution
290
293
  ivar = "@#{association_name}"
291
294
  if instance_variable_defined?(ivar)
292
295
  instance_variable_get(ivar)
293
- elsif association_class.respond_to?(:all)
296
+ elsif self.class.autoload_associations? && association_class.respond_to?(:all)
294
297
  instance_variable_set(ivar, Array(association_class.all("#{self.class.name.underscore}_id" => id)))
295
298
  end
296
299
  else # Ex: Book.all(:name => "The..."), so we do not want to cache it
297
- if association_class.respond_to?(:all)
300
+ if self.class.autoload_associations? && association_class.respond_to?(:all)
298
301
  Array(association_class.all({"#{self.class.name.demodulize.underscore}_id" => id}.merge(query.first)))
299
302
  end
300
303
  end
@@ -4,12 +4,12 @@ require 'attribution/model'
4
4
  class Article
5
5
  include Attribution::Model
6
6
 
7
- string :title, required: true, format: { with: /\A\w/, message: "must start with a letter" }, length: 4..20
7
+ string :title, :required => true, :format => { :with => /\A\w/, :message => "must start with a letter" }, :length => 4..20
8
8
  end
9
9
 
10
10
  class AttributionModelTest < Test::Unit::TestCase
11
11
  def test_model
12
- article = Article.new(id: 1, created_at: Time.now, updated_at: Time.now)
12
+ article = Article.new(:id => 1, :created_at => Time.now, :updated_at => Time.now)
13
13
  assert !article.valid?
14
14
  assert_equal ["can't be blank", "must start with a letter", "is too short (minimum is 4 characters)"], article.errors[:title]
15
15
  end
@@ -42,6 +42,9 @@ class Book
42
42
  belongs_to :book
43
43
  has_many :chapters
44
44
  has_many :readers
45
+
46
+ def self.find(*args)
47
+ end
45
48
  end
46
49
 
47
50
  class Reader
@@ -65,6 +68,10 @@ class Chapter
65
68
 
66
69
  belongs_to :book
67
70
  has_many :pages
71
+
72
+ def self.all(*args)
73
+ []
74
+ end
68
75
  end
69
76
 
70
77
  class Page
@@ -120,6 +127,22 @@ module Music
120
127
  end
121
128
  end
122
129
 
130
+ class StoreModel
131
+ include Attribution
132
+
133
+ def self.autoload_associations?
134
+ false
135
+ end
136
+ end
137
+
138
+ class Product < StoreModel
139
+ has_many :orders
140
+ end
141
+
142
+ class Order < StoreModel
143
+ belongs_to :product
144
+ end
145
+
123
146
  class AttributionTest < Test::Unit::TestCase
124
147
 
125
148
  def test_create
@@ -274,7 +297,7 @@ class AttributionTest < Test::Unit::TestCase
274
297
  end
275
298
 
276
299
  def test_to_json_should_only_include_attibutes
277
- book = Book.new(id: 1, foo: 'bar')
300
+ book = Book.new(:id => 1, :foo => 'bar')
278
301
  assert_equal nil, JSON.parse(book.to_json)['foo']
279
302
  assert JSON.parse(book.to_json).key?('title'), 'to_json sohuld include attributes that have no value assigned to them'
280
303
  end
@@ -289,12 +312,37 @@ class AttributionTest < Test::Unit::TestCase
289
312
  end
290
313
 
291
314
  def test_namespaced_belongs_to
292
- track = Music::Track.new(number: 1, title: "Elevator Music", album: { artist: "Beck", title: "The Information" })
315
+ track = Music::Track.new(:number => 1, :title => "Elevator Music", :album => { :artist => "Beck", :title => "The Information" })
293
316
  assert_equal "Beck", track.album.artist
294
317
  end
295
318
 
296
319
  def test_namespaced_has_many
297
- album = Music::Album.new(artist: "Beck", title: "The Information", tracks: [ { number: 1, title: "Elevator Music" } ])
320
+ album = Music::Album.new(:artist => "Beck", :title => "The Information", :tracks => [ { :number => 1, :title => "Elevator Music" } ])
298
321
  assert_equal "Elevator Music", album.tracks.first.title
299
322
  end
323
+
324
+ def test_autoload_belongs_to_associations
325
+ chapter = Chapter.new(:book_id => 42)
326
+ Book.expects(:find).with(42)
327
+ chapter.book
328
+ end
329
+
330
+ def test_autoload_has_many_associations
331
+ book = Book.new(:id => 42)
332
+ Chapter.expects(:all).with("book_id" => 42)
333
+ book.chapters
334
+ end
335
+
336
+ def test_disabled_autoload_belongs_to_associations
337
+ order = Order.new
338
+ Product.expects(:find).never
339
+ order.product
340
+ end
341
+
342
+ def test_disabled_autoload_has_many_associations
343
+ product = Product.new
344
+ Order.expects(:all).never
345
+ product.orders
346
+ end
347
+
300
348
  end
data/test/test_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'test/unit'
2
+ require 'mocha/setup'
2
3
  require 'attribution'
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.6.5
4
+ version: 0.7.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-22 00:00:00.000000000 Z
11
+ date: 2013-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport