attribution 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/attribution.gemspec +1 -1
- data/lib/attribution/version.rb +1 -1
- data/lib/attribution.rb +23 -0
- data/test/attribution_test.rb +32 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7fbd76f70d8e5491d55e03c1ac6f0b38b095b6b
|
4
|
+
data.tar.gz: afe56be4e9efe8c16595942dd2378917728c0abe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aad19801c643833ee388d1a389d9f74664a3b02262982e3af5566142c5e0cdcbcb2427ef29bc52579ddd6995d880f98e210a745ecc98074d1dae9e7e93bd364c
|
7
|
+
data.tar.gz: b4d49688cb6eeb36337a32a0c2d2b32301f478c1ee0abae579e2ce2280fdf2f17f479cb07b68ed345b19e61cfdffd8d457725f325799d019ba1b23645db6a86b
|
data/attribution.gemspec
CHANGED
data/lib/attribution/version.rb
CHANGED
data/lib/attribution.rb
CHANGED
@@ -197,6 +197,29 @@ module Attribution
|
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
|
+
# Defines an array attribute
|
201
|
+
#
|
202
|
+
# @param [Symbol] attr The name of the attribute
|
203
|
+
# @param [Hash{Symbol => Object}] metadata The metadata for the attribute
|
204
|
+
def array(attr, metadata={})
|
205
|
+
add_attribute(attr, :array, metadata)
|
206
|
+
define_method("#{attr}=") do |arg|
|
207
|
+
instance_variable_set("@#{attr}", Array(arg))
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Defines a hash attribute. This is named hash_attr instead of hash
|
212
|
+
# to avoid a conflict with Object#hash
|
213
|
+
#
|
214
|
+
# @params [Symbol] attr The name of the attribute
|
215
|
+
# @params [Hash{Symbol => Object}] metadata The metadata for the attribute
|
216
|
+
def hash_attr(attr, metadata={})
|
217
|
+
add_attribute(attr, :hash, metadata)
|
218
|
+
define_method("#{attr}=") do |arg|
|
219
|
+
instance_variable_set("@#{attr}", Hash(arg))
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
200
223
|
# Associations
|
201
224
|
|
202
225
|
# @return [Array<Hash>] The associations for this class
|
data/test/attribution_test.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
#TODO: support using a different class name than the association name
|
4
|
-
|
5
3
|
class Address
|
6
4
|
include Attribution
|
7
5
|
|
@@ -38,6 +36,8 @@ class Book
|
|
38
36
|
time :created_at
|
39
37
|
time :updated_at
|
40
38
|
time_zone :time_zone
|
39
|
+
array :numbers
|
40
|
+
hash_attr :location
|
41
41
|
|
42
42
|
has_many :chapters
|
43
43
|
has_many :readers
|
@@ -253,6 +253,21 @@ class AttributionTest < Test::Unit::TestCase
|
|
253
253
|
assert_equal Time.parse('2013-03-17 00:00:00'), book.created_at
|
254
254
|
end
|
255
255
|
|
256
|
+
def test_nothing
|
257
|
+
book = Book.new
|
258
|
+
assert_equal nil, book.id
|
259
|
+
assert_equal nil, book.title
|
260
|
+
assert_equal nil, book.price
|
261
|
+
assert_equal nil, book.published_on
|
262
|
+
assert_equal nil, book.ebook_available
|
263
|
+
assert_equal nil, book.used
|
264
|
+
assert_equal nil, book.shipping_weight
|
265
|
+
assert_equal nil, book.created_at
|
266
|
+
assert_equal nil, book.time_zone
|
267
|
+
assert_equal nil, book.numbers
|
268
|
+
assert_equal nil, book.location
|
269
|
+
end
|
270
|
+
|
256
271
|
def test_nil
|
257
272
|
book = Book.new(
|
258
273
|
:id => nil,
|
@@ -264,6 +279,8 @@ class AttributionTest < Test::Unit::TestCase
|
|
264
279
|
:shipping_weight => nil,
|
265
280
|
:created_at => nil,
|
266
281
|
:time_zone => nil,
|
282
|
+
:numbers => nil,
|
283
|
+
:location => nil
|
267
284
|
)
|
268
285
|
assert_equal nil, book.id
|
269
286
|
assert_equal nil, book.title
|
@@ -274,6 +291,19 @@ class AttributionTest < Test::Unit::TestCase
|
|
274
291
|
assert_equal nil, book.shipping_weight
|
275
292
|
assert_equal nil, book.created_at
|
276
293
|
assert_equal nil, book.time_zone
|
294
|
+
assert_equal [], book.numbers
|
295
|
+
assert_equal({}, book.location)
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_array_with_scalar
|
299
|
+
book = Book.new(:numbers => 42)
|
300
|
+
assert_equal [42], book.numbers
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_hash_attr
|
304
|
+
book = Book.new(:location => { :lat => 39.27983915, :lon => -76.60873889 })
|
305
|
+
assert_equal 39.27983915, book.location[:lat]
|
306
|
+
assert_equal -76.60873889, book.location[:lon]
|
277
307
|
end
|
278
308
|
|
279
309
|
def test_attributes_setter
|