model_attribute 2.1.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.travis.yml +3 -2
- data/CHANGELOG.md +6 -0
- data/README.md +8 -8
- data/lib/model_attribute/casts.rb +3 -3
- data/lib/model_attribute/version.rb +1 -1
- data/spec/model_attributes_spec.rb +15 -8
- metadata +23 -22
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
ZTc1NmViMDUxNDhiNmIzMGJiYmNiMzE5ZWM2ZDFjMGVmM2EwNjQxNg==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f7732b75fa197e06270fb128e6765d2f6907a11c
|
4
|
+
data.tar.gz: d30224be4128b258304a58b5560dbe63d854fb0e
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
ODRjZWVmNDI2NzIxODk4MmY0YmQ0MTY3NGM0OTk5OGExMTFjY2UwM2Q0MzQ2
|
11
|
-
MzMyY2FlM2M3ZDk5Zjc3YjUzYTY5NmIwNjQ3NjU1NjYxYjMyMWM=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MmE3NmY2MmMyYmU3MmRiNzY5ZGRhNjQ3MzYxMDhlMzdiZjhlZTFiMDVhOTY3
|
14
|
-
MDU0M2Q1OGJjZjRkZGM5YjA3ZGYyY2E0OTBkNDUwMDAxM2Y1NmQ2MTUzOWZi
|
15
|
-
N2VkMmM5MzViMGQ3OWYyODljNTg3Y2E4ZGExZTVhMmQzMzgzODc=
|
6
|
+
metadata.gz: 90199911d48ea11bcab09b6e993135cf60f5b584ebfbf05192f86c5a39d9d928baea6ceeda4703a03afbc32c8be1cc4e3e3b6cfbcba983ea437f6b51ec5504ff
|
7
|
+
data.tar.gz: 5a87bbf29f4d0c321c764f02ea8a8a77ba9ac255ac9fdcec2f96d0f907f5bf34efb6b63cd7c4950910eee6e0d1a21a05bde8fdc09ba7960f8a7be4def71af6d3
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## 3.0.0
|
6
|
+
|
7
|
+
- **Breaking change**: All casting errors raise `ArgumentError`. Previously some
|
8
|
+
errors during casting would raise `RuntimeError`.
|
9
|
+
Thanks to [@gotascii](https://github.com/gotascii) for the report.
|
10
|
+
|
5
11
|
## 2.1.0
|
6
12
|
|
7
13
|
- **New feature**: default values. Allows you to specify a default value like
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ Simple attributes for a non-ActiveRecord model.
|
|
12
12
|
- Supports efficient serialization of attributes to JSON.
|
13
13
|
- Mass assignment - handy for initializers.
|
14
14
|
|
15
|
-
Why not [Virtus][virtus-gem]? Virtus doesn't provide
|
15
|
+
Why not [Virtus][virtus-gem]? Virtus doesn't provide dirty tracking, and
|
16
16
|
doesn't integrate with [ActiveModel::Dirty][am-dirty]. So if you're not using
|
17
17
|
ActiveRecord, but you need attributes with dirty tracking, ModelAttribute may be
|
18
18
|
what you're after. For example, it works very well for a model that fronts an
|
@@ -24,7 +24,7 @@ Also in favor of ModelAttribute:
|
|
24
24
|
- It supports efficient serialization and deserialization to/from JSON.
|
25
25
|
|
26
26
|
[virtus-gem]:https://github.com/solnic/virtus
|
27
|
-
[am-dirty]:https://github.com/rails/rails/blob/
|
27
|
+
[am-dirty]:https://github.com/rails/rails/blob/master/activemodel/lib/active_model/dirty.rb
|
28
28
|
[source]:https://github.com/yammer/model_attribute/blob/master/lib/model_attribute.rb
|
29
29
|
|
30
30
|
## Usage
|
@@ -103,7 +103,7 @@ user.grades # => ["A", "A*", "C"]
|
|
103
103
|
user.grades = 'AAB'
|
104
104
|
user.grades # => "AAB"
|
105
105
|
user.grades = Time.now
|
106
|
-
# =>
|
106
|
+
# => ArgumentError: JSON only supports nil, numeric, string, boolean and arrays and hashes of those.
|
107
107
|
|
108
108
|
# read_attribute and write_attribute methods
|
109
109
|
user.read_attribute(:created_at)
|
@@ -126,8 +126,8 @@ Oj.dump(user.attributes_for_json, mode: :strict)
|
|
126
126
|
# => "{\"id\":5,\"paid\":true,\"name\":\"Fred\",\"created_at\":1421171317762}"
|
127
127
|
user2 = User.new(Oj.load(json, strict: true))
|
128
128
|
|
129
|
-
# Change tracking. A much smaller set of
|
130
|
-
# ActiveModel::
|
129
|
+
# Change tracking. A much smaller set of functions than that provided by
|
130
|
+
# ActiveModel::Dirty.
|
131
131
|
user.changes # => {:id=>[nil, 5], :paid=>[nil, true], :created_at=>[nil, 2015-01-08 15:57:05 +0000], :name=>[nil, "Fred"]}
|
132
132
|
user.name_changed? # => true
|
133
133
|
# If you need the new values to send as a PUT to a web service
|
@@ -137,7 +137,7 @@ user.changes_for_json # => {"id"=>5, "paid"=>true, "name"=>"Fred", "created_at"=
|
|
137
137
|
user.changes.clear
|
138
138
|
user.changes # => {}
|
139
139
|
|
140
|
-
# Equality
|
140
|
+
# Equality if all the attribute values match
|
141
141
|
another = User.new
|
142
142
|
another.id = 5
|
143
143
|
another.paid = true
|
@@ -182,7 +182,7 @@ user.read_attribute(:name) # => "Charlie"
|
|
182
182
|
user.attributes # => {:name=>"Charlie"}
|
183
183
|
# attributes_for_json omits defaults to keep the JSON compact
|
184
184
|
user.attributes_for_json # => {}
|
185
|
-
#
|
185
|
+
# You can add them back in if you need them
|
186
186
|
user.attributes_for_json.merge(user.class.attribute_defaults) # => {:name=>"Charlie"}
|
187
187
|
# A default isn't a change
|
188
188
|
user.changes # => {}
|
@@ -210,7 +210,7 @@ Or install it yourself as:
|
|
210
210
|
|
211
211
|
## Contributing
|
212
212
|
|
213
|
-
1. Fork it
|
213
|
+
1. [Fork it](https://github.com/yammer/model_attribute/fork)
|
214
214
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
215
215
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
216
216
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -8,7 +8,7 @@ module ModelAttribute
|
|
8
8
|
when :integer
|
9
9
|
int = Integer(value)
|
10
10
|
float = Float(value)
|
11
|
-
raise "Can't cast #{value.inspect} to an integer without loss of precision" unless int == float
|
11
|
+
raise ArgumentError, "Can't cast #{value.inspect} to an integer without loss of precision" unless int == float
|
12
12
|
int
|
13
13
|
when :boolean
|
14
14
|
if !!value == value
|
@@ -18,7 +18,7 @@ module ModelAttribute
|
|
18
18
|
elsif value == 'f'
|
19
19
|
false
|
20
20
|
else
|
21
|
-
raise "Can't cast #{value.inspect} to boolean"
|
21
|
+
raise ArgumentError, "Can't cast #{value.inspect} to boolean"
|
22
22
|
end
|
23
23
|
when :time
|
24
24
|
case value
|
@@ -41,7 +41,7 @@ module ModelAttribute
|
|
41
41
|
if valid_json?(value)
|
42
42
|
value
|
43
43
|
else
|
44
|
-
raise "JSON only supports nil, numeric, string, boolean and arrays and hashes of those."
|
44
|
+
raise ArgumentError, "JSON only supports nil, numeric, string, boolean and arrays and hashes of those."
|
45
45
|
end
|
46
46
|
else
|
47
47
|
raise UnsupportedTypeError.new(type)
|
@@ -69,7 +69,7 @@ RSpec.describe "a class using ModelAttribute" do
|
|
69
69
|
end
|
70
70
|
|
71
71
|
it "raises when passed a float with non-zero decimal part" do
|
72
|
-
expect { user.id = 3.3 }.to raise_error
|
72
|
+
expect { user.id = 3.3 }.to raise_error(ArgumentError)
|
73
73
|
end
|
74
74
|
|
75
75
|
it "parses an integer string" do
|
@@ -78,7 +78,8 @@ RSpec.describe "a class using ModelAttribute" do
|
|
78
78
|
end
|
79
79
|
|
80
80
|
it "raises if passed a string it can't parse" do
|
81
|
-
expect { user.id = '3a' }.to raise_error
|
81
|
+
expect { user.id = '3a' }.to raise_error(ArgumentError,
|
82
|
+
/invalid value for Integer.*: "3a"/)
|
82
83
|
end
|
83
84
|
|
84
85
|
it "stores nil" do
|
@@ -119,7 +120,8 @@ RSpec.describe "a class using ModelAttribute" do
|
|
119
120
|
end
|
120
121
|
|
121
122
|
it "raises if passed a string it can't parse" do
|
122
|
-
expect { user.paid = '3a' }.to raise_error
|
123
|
+
expect { user.paid = '3a' }.to raise_error(ArgumentError,
|
124
|
+
'Can\'t cast "3a" to boolean')
|
123
125
|
end
|
124
126
|
|
125
127
|
it "stores nil" do
|
@@ -204,7 +206,8 @@ RSpec.describe "a class using ModelAttribute" do
|
|
204
206
|
end
|
205
207
|
|
206
208
|
it "raises for unparseable strings" do
|
207
|
-
expect { user.created_at = "Today, innit?" }.to raise_error
|
209
|
+
expect { user.created_at = "Today, innit?" }.to raise_error(ArgumentError,
|
210
|
+
'no time information in "Today, innit?"')
|
208
211
|
end
|
209
212
|
|
210
213
|
it "converts Dates to Time" do
|
@@ -279,19 +282,23 @@ RSpec.describe "a class using ModelAttribute" do
|
|
279
282
|
end
|
280
283
|
|
281
284
|
it "raises when passed an object not supported by JSON" do
|
282
|
-
expect { user.profile = Object.new }.to raise_error
|
285
|
+
expect { user.profile = Object.new }.to raise_error(ArgumentError,
|
286
|
+
"JSON only supports nil, numeric, string, boolean and arrays and hashes of those.")
|
283
287
|
end
|
284
288
|
|
285
289
|
it "raises when passed a hash with a non-string key" do
|
286
|
-
expect { user.profile = {1 => 'first'} }.to raise_error
|
290
|
+
expect { user.profile = {1 => 'first'} }.to raise_error(ArgumentError,
|
291
|
+
"JSON only supports nil, numeric, string, boolean and arrays and hashes of those.")
|
287
292
|
end
|
288
293
|
|
289
294
|
it "raises when passed a hash with an unsupported value" do
|
290
|
-
expect { user.profile = {'first' => :symbol} }.to raise_error
|
295
|
+
expect { user.profile = {'first' => :symbol} }.to raise_error(ArgumentError,
|
296
|
+
"JSON only supports nil, numeric, string, boolean and arrays and hashes of those.")
|
291
297
|
end
|
292
298
|
|
293
299
|
it "raises when passed an array with an unsupported value" do
|
294
|
-
expect { user.profile = [1, 2, nil, :symbol] }.to raise_error
|
300
|
+
expect { user.profile = [1, 2, nil, :symbol] }.to raise_error(ArgumentError,
|
301
|
+
"JSON only supports nil, numeric, string, boolean and arrays and hashes of those.")
|
295
302
|
end
|
296
303
|
|
297
304
|
it "stores nil" do
|
metadata
CHANGED
@@ -1,110 +1,111 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Waller
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.7'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '3.1'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.1'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec-nc
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0.2'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: guard
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - ~>
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '2.8'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - ~>
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.8'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: guard-rspec
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - ~>
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '4.3'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - ~>
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '4.3'
|
97
|
-
description:
|
98
|
-
|
97
|
+
description: |2
|
98
|
+
Attributes for non-ActiveRecord models.
|
99
|
+
Smaller and simpler than Virtus, and adds dirty tracking.
|
99
100
|
email:
|
100
101
|
- dwaller@yammer-inc.com
|
101
102
|
executables: []
|
102
103
|
extensions: []
|
103
104
|
extra_rdoc_files: []
|
104
105
|
files:
|
105
|
-
- .gitignore
|
106
|
-
- .rspec
|
107
|
-
- .travis.yml
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- ".travis.yml"
|
108
109
|
- CHANGELOG.md
|
109
110
|
- Gemfile
|
110
111
|
- Guardfile
|
@@ -129,17 +130,17 @@ require_paths:
|
|
129
130
|
- lib
|
130
131
|
required_ruby_version: !ruby/object:Gem::Requirement
|
131
132
|
requirements:
|
132
|
-
- -
|
133
|
+
- - ">="
|
133
134
|
- !ruby/object:Gem::Version
|
134
135
|
version: '0'
|
135
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
137
|
requirements:
|
137
|
-
- -
|
138
|
+
- - ">="
|
138
139
|
- !ruby/object:Gem::Version
|
139
140
|
version: '0'
|
140
141
|
requirements: []
|
141
142
|
rubyforge_project:
|
142
|
-
rubygems_version: 2.4.5
|
143
|
+
rubygems_version: 2.4.5.1
|
143
144
|
signing_key:
|
144
145
|
specification_version: 4
|
145
146
|
summary: Attributes for non-ActiveRecord models
|