attribution 0.8.0 → 0.9.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: 76c80b1626e3995ef953a1ecf966eeb2e0cf0935
4
- data.tar.gz: 60f0bb25b5470c74f7ba0ba38991c447a8cc63b5
3
+ metadata.gz: a7fbd76f70d8e5491d55e03c1ac6f0b38b095b6b
4
+ data.tar.gz: afe56be4e9efe8c16595942dd2378917728c0abe
5
5
  SHA512:
6
- metadata.gz: e29e6caae51db60d0e910bb568f92d93a125881968a2e97018990d87655f6e189d4ca20d8dcf5ae43e18fae2c0b7ac40228e6a9cddb3568094a3fd23bf064bdf
7
- data.tar.gz: 75f106172c77d966576e169e565fed38976cff5274ad1e3189f56dc892e4077b56800c365e0e017926083767d375f6083d1811c374b12f9b73dd22e18aa1f022
6
+ metadata.gz: aad19801c643833ee388d1a389d9f74664a3b02262982e3af5566142c5e0cdcbcb2427ef29bc52579ddd6995d880f98e210a745ecc98074d1dae9e7e93bd364c
7
+ data.tar.gz: b4d49688cb6eeb36337a32a0c2d2b32301f478c1ee0abae579e2ce2280fdf2f17f479cb07b68ed345b19e61cfdffd8d457725f325799d019ba1b23645db6a86b
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.8.0"
5
+ gem.version = "0.9.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.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
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
@@ -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
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.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Barry