model_attribute 3.1.1 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/CHANGELOG.md +5 -0
- data/README.md +6 -0
- data/lib/model_attribute.rb +1 -1
- data/lib/model_attribute/casts.rb +2 -0
- data/lib/model_attribute/version.rb +1 -1
- data/spec/model_attributes_spec.rb +24 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46443eebfd6c0b53bb687d38803ffdccfb39562e
|
4
|
+
data.tar.gz: 41f63196c4d17ae538d4e14acd8d2b2aa8bb45be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ead02d4451ea6044fdb1e613af619d3dd8b842ec06bd1d7a22785d5c6710e970836bef13c715322b9824efc446d27cc07b326d70adee6be81aef575eacc1196c
|
7
|
+
data.tar.gz: aa4e8541337cf0341799366122be1d772663ebc2f988eae15f14bf821176c389c4538554e8b4322ee6bcbf7c1257b27a3c1e30ecd21b6785a5d22907a0b1a118
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
data/lib/model_attribute.rb
CHANGED
@@ -4,7 +4,7 @@ require "model_attribute/errors"
|
|
4
4
|
require "time"
|
5
5
|
|
6
6
|
module ModelAttribute
|
7
|
-
SUPPORTED_TYPES = [:integer, :boolean, :string, :time, :json]
|
7
|
+
SUPPORTED_TYPES = [:integer, :float, :boolean, :string, :time, :json]
|
8
8
|
|
9
9
|
def self.extended(base)
|
10
10
|
base.send(:include, InstanceMethods)
|
@@ -6,6 +6,7 @@ class User
|
|
6
6
|
attribute :created_at, :time
|
7
7
|
attribute :profile, :json
|
8
8
|
attribute :reward_points, :integer, default: 0
|
9
|
+
attribute :win_rate, :float
|
9
10
|
|
10
11
|
def initialize(attributes = {})
|
11
12
|
set_attributes(attributes)
|
@@ -32,14 +33,14 @@ RSpec.describe "a class using ModelAttribute" do
|
|
32
33
|
User.attribute :address, :custom_type
|
33
34
|
end.to raise_error(ModelAttribute::UnsupportedTypeError,
|
34
35
|
"Unsupported type :custom_type. " +
|
35
|
-
"Must be one of :integer, :boolean, :string, :time, :json.")
|
36
|
+
"Must be one of :integer, :float, :boolean, :string, :time, :json.")
|
36
37
|
end
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
40
41
|
describe ".attributes" do
|
41
42
|
it "returns an array of attribute names as symbols" do
|
42
|
-
expect(User.attributes).to eq([:id, :paid, :name, :created_at, :profile, :reward_points])
|
43
|
+
expect(User.attributes).to eq([:id, :paid, :name, :created_at, :profile, :reward_points, :win_rate])
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
@@ -94,6 +95,18 @@ RSpec.describe "a class using ModelAttribute" do
|
|
94
95
|
end
|
95
96
|
end
|
96
97
|
|
98
|
+
describe "a float attribute (win_rate)" do
|
99
|
+
it "stores a float" do
|
100
|
+
user.win_rate = 35.62
|
101
|
+
expect(user.win_rate).to eq(35.62)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "parses a float string" do
|
105
|
+
user.win_rate = 35.62
|
106
|
+
expect(user.win_rate).to eq(35.62)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
97
110
|
describe "a boolean attribute (paid)" do
|
98
111
|
it "is nil when unset" do
|
99
112
|
expect(user.paid).to be_nil
|
@@ -212,7 +225,7 @@ RSpec.describe "a class using ModelAttribute" do
|
|
212
225
|
|
213
226
|
it "parses strings to date/times" do
|
214
227
|
user.created_at = "2014-12-25 14:00:00 +0100"
|
215
|
-
expect(user.created_at).to eq(Time.new(2014, 12, 25, 13, 00, 00))
|
228
|
+
expect(user.created_at).to eq(Time.new(2014, 12, 25, 13, 00, 00, 0))
|
216
229
|
end
|
217
230
|
|
218
231
|
it "raises for unparseable strings" do
|
@@ -226,8 +239,8 @@ RSpec.describe "a class using ModelAttribute" do
|
|
226
239
|
end
|
227
240
|
|
228
241
|
it "converts DateTime to Time" do
|
229
|
-
user.created_at = DateTime.parse("2014-12-25 13:00:45")
|
230
|
-
expect(user.created_at).to eq(Time.new(2014, 12, 25, 13, 00, 45))
|
242
|
+
user.created_at = DateTime.parse("2014-12-25 13:00:45 +0000")
|
243
|
+
expect(user.created_at.utc).to eq(Time.new(2014, 12, 25, 13, 00, 45, 0))
|
231
244
|
end
|
232
245
|
|
233
246
|
it "stores nil" do
|
@@ -444,7 +457,7 @@ RSpec.describe "a class using ModelAttribute" do
|
|
444
457
|
|
445
458
|
it "serializes time attributes as JSON integer" do
|
446
459
|
user.created_at = Time.now
|
447
|
-
expect(changes_for_json).to include("created_at" =>
|
460
|
+
expect(changes_for_json).to include("created_at" => kind_of(Integer))
|
448
461
|
end
|
449
462
|
end
|
450
463
|
|
@@ -496,7 +509,7 @@ RSpec.describe "a class using ModelAttribute" do
|
|
496
509
|
end
|
497
510
|
|
498
511
|
it "serializes time attributes as JSON integer" do
|
499
|
-
expect(user.attributes_for_json).to include("created_at" =>
|
512
|
+
expect(user.attributes_for_json).to include("created_at" => kind_of(Integer))
|
500
513
|
end
|
501
514
|
|
502
515
|
it "serializes string attributes as JSON string" do
|
@@ -556,9 +569,10 @@ RSpec.describe "a class using ModelAttribute" do
|
|
556
569
|
let(:user) do
|
557
570
|
User.new(id: 1,
|
558
571
|
name: "Fred",
|
559
|
-
created_at: "2014-12-25 08:00",
|
572
|
+
created_at: "2014-12-25 08:00 +0000",
|
560
573
|
paid: true,
|
561
|
-
profile: {'interests' => ['coding', 'social networks'], 'rank' => 15}
|
574
|
+
profile: {'interests' => ['coding', 'social networks'], 'rank' => 15},
|
575
|
+
win_rate: 35.62)
|
562
576
|
end
|
563
577
|
|
564
578
|
it "includes integer attributes as 'name: value'" do
|
@@ -590,7 +604,7 @@ RSpec.describe "a class using ModelAttribute" do
|
|
590
604
|
end
|
591
605
|
|
592
606
|
it "looks like '#<User id: 1, paid: true, name: ..., created_at: ...>'" do
|
593
|
-
expect(user.inspect).to eq("#<User id: 1, paid: true, name: \"Fred\", created_at: 2014-12-25 08:00:00 +0000, profile: {\"interests\"=>[\"coding\", \"social networks\"], \"rank\"=>15}, reward_points: 0>")
|
607
|
+
expect(user.inspect).to eq("#<User id: 1, paid: true, name: \"Fred\", created_at: 2014-12-25 08:00:00 +0000, profile: {\"interests\"=>[\"coding\", \"social networks\"], \"rank\"=>15}, reward_points: 0, win_rate: 35.62>")
|
594
608
|
end
|
595
609
|
end
|
596
610
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.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: 2017-
|
11
|
+
date: 2017-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|