attribution 0.6.0 → 0.6.1

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "attribution"
5
- gem.version = "0.6.0"
5
+ gem.version = "0.6.1"
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.0"
2
+ VERSION = "0.6.1"
3
3
  end
data/lib/attribution.rb CHANGED
@@ -10,6 +10,9 @@ module Attribution
10
10
  end
11
11
 
12
12
  def initialize(attributes={})
13
+ self.class.attribute_names.each do |attr|
14
+ instance_variable_set("@#{attr}", nil)
15
+ end
13
16
  self.attributes = attributes
14
17
  end
15
18
 
@@ -21,18 +24,17 @@ module Attribution
21
24
  end
22
25
  alias_method :to_h, :attributes
23
26
 
24
- def to_json
25
- to_h.to_json
26
- end
27
-
28
27
  def attributes=(attributes)
29
- if attributes
30
- attributes = JSON.parse(attributes) if attributes.is_a?(String)
31
- attributes.each do |k,v|
32
- setter = "#{k}="
33
- if respond_to?(setter)
34
- send(setter, v)
35
- end
28
+ attributes = case attributes
29
+ when String then JSON.parse(attributes)
30
+ when Hash then attributes
31
+ else {}
32
+ end.with_indifferent_access
33
+
34
+ attributes.each do |k,v|
35
+ setter = "#{k}="
36
+ if respond_to?(setter)
37
+ send(setter, v)
36
38
  end
37
39
  end
38
40
  end
@@ -218,5 +218,6 @@ class AttributionTest < Test::Unit::TestCase
218
218
  def test_to_json_should_only_include_attibutes
219
219
  book = Book.new(id: 1, foo: 'bar')
220
220
  assert_equal nil, JSON.parse(book.to_json)['foo']
221
+ assert JSON.parse(book.to_json).key?('title'), 'to_json sohuld include attributes that have no value assigned to them'
221
222
  end
222
223
  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.6.0
4
+ version: 0.6.1
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-04-29 00:00:00.000000000 Z
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport