attribution 0.3.2 → 0.4.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.
- data/attribution.gemspec +1 -1
- data/lib/attribution/version.rb +1 -1
- data/lib/attribution.rb +33 -22
- data/test/attribution_test.rb +24 -0
- metadata +2 -2
data/attribution.gemspec
CHANGED
data/lib/attribution/version.rb
CHANGED
data/lib/attribution.rb
CHANGED
@@ -9,16 +9,8 @@ module Attribution
|
|
9
9
|
cls.extend(ClassMethods)
|
10
10
|
end
|
11
11
|
|
12
|
-
def initialize(
|
13
|
-
|
14
|
-
attrs.each do |k,v|
|
15
|
-
setter = "#{k}="
|
16
|
-
if respond_to?(setter)
|
17
|
-
send(setter, v)
|
18
|
-
else
|
19
|
-
instance_variable_set("@#{k}", v)
|
20
|
-
end
|
21
|
-
end
|
12
|
+
def initialize(attributes={})
|
13
|
+
self.attributes = attributes
|
22
14
|
end
|
23
15
|
|
24
16
|
# TODO: Use associations argument as a way to specify which associations should be included
|
@@ -29,6 +21,20 @@ module Attribution
|
|
29
21
|
end
|
30
22
|
end
|
31
23
|
|
24
|
+
def attributes=(attributes)
|
25
|
+
if attributes
|
26
|
+
attributes = JSON.parse(attributes) if attributes.is_a?(String)
|
27
|
+
attributes.each do |k,v|
|
28
|
+
setter = "#{k}="
|
29
|
+
if respond_to?(setter)
|
30
|
+
send(setter, v)
|
31
|
+
else
|
32
|
+
instance_variable_set("@#{k}", v)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
32
38
|
module ClassMethods
|
33
39
|
def cast(obj)
|
34
40
|
case obj
|
@@ -195,21 +201,26 @@ module Attribution
|
|
195
201
|
|
196
202
|
def has_many(association_name)
|
197
203
|
# foos
|
198
|
-
define_method(association_name) do
|
199
|
-
ivar = "@#{association_name}"
|
200
|
-
if instance_variable_defined?(ivar)
|
201
|
-
instance_variable_get(ivar)
|
202
|
-
else
|
203
|
-
# TODO: Support a more generic version of lazy-loading
|
204
|
-
begin
|
205
|
-
association_class = association_name.to_s.singularize.classify.constantize
|
206
|
-
rescue NameError => ex
|
207
|
-
raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_name.to_s.classify} does not exist")
|
208
|
-
end
|
204
|
+
define_method(association_name) do |*query|
|
209
205
|
|
210
|
-
|
206
|
+
# TODO: Support a more generic version of lazy-loading
|
207
|
+
begin
|
208
|
+
association_class = association_name.to_s.singularize.classify.constantize
|
209
|
+
rescue NameError => ex
|
210
|
+
raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_name.to_s.classify} does not exist")
|
211
|
+
end
|
212
|
+
|
213
|
+
if query.empty? # Ex: Books.all, so we want to cache it.
|
214
|
+
ivar = "@#{association_name}"
|
215
|
+
if instance_variable_defined?(ivar)
|
216
|
+
instance_variable_get(ivar)
|
217
|
+
elsif association_class.respond_to?(:all)
|
211
218
|
instance_variable_set(ivar, Array(association_class.all("#{self.class.name.underscore}_id" => id)))
|
212
219
|
end
|
220
|
+
else # Ex: Book.all(:name => "The..."), so we do not want to cache it
|
221
|
+
if association_class.respond_to?(:all)
|
222
|
+
Array(association_class.all({"#{self.class.name.underscore}_id" => id}.merge(query.first)))
|
223
|
+
end
|
213
224
|
end
|
214
225
|
end
|
215
226
|
|
data/test/attribution_test.rb
CHANGED
@@ -28,6 +28,18 @@ class Book
|
|
28
28
|
|
29
29
|
belongs_to :book
|
30
30
|
has_many :chapters
|
31
|
+
has_many :readers
|
32
|
+
end
|
33
|
+
|
34
|
+
class Reader
|
35
|
+
include Attribution
|
36
|
+
|
37
|
+
integer :id
|
38
|
+
|
39
|
+
def self.all(query = {})
|
40
|
+
query
|
41
|
+
end
|
42
|
+
|
31
43
|
end
|
32
44
|
|
33
45
|
class Chapter
|
@@ -100,6 +112,10 @@ class AttributionTest < Test::Unit::TestCase
|
|
100
112
|
assert_equal ActiveSupport::TimeZone["Eastern Time (US & Canada)"], book.time_zone
|
101
113
|
assert_equal 1, book.chapters.first.number
|
102
114
|
assert_equal 3, book.chapters.size
|
115
|
+
assert_equal ({}), Reader.all
|
116
|
+
assert_equal ([['book_id', 1]]), book.readers
|
117
|
+
assert_equal ([['book_id', 1], [:name,"julio"]]), book.readers(:name => 'julio')
|
118
|
+
assert_equal ([['book_id', 1]]), book.readers # Instance variable caching
|
103
119
|
assert_equal book, book.chapters.first.book
|
104
120
|
end
|
105
121
|
|
@@ -182,4 +198,12 @@ class AttributionTest < Test::Unit::TestCase
|
|
182
198
|
assert_equal nil, book.created_at
|
183
199
|
assert_equal nil, book.time_zone
|
184
200
|
end
|
201
|
+
|
202
|
+
def test_attributes_setter
|
203
|
+
book = Book.new
|
204
|
+
book.attributes = { :title => "Whatever", :price => 50 }
|
205
|
+
assert_equal "Whatever", book.title
|
206
|
+
assert_equal BigDecimal.new("50"), book.price
|
207
|
+
assert_equal nil, book.id
|
208
|
+
end
|
185
209
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribution
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|