model_attribute 3.1.1 → 3.2.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: c1442469dac9a5931595c49246ee3da3a22e6d9b
4
- data.tar.gz: 6b91c22381b05461883e96803bd633dccb543dd7
3
+ metadata.gz: 46443eebfd6c0b53bb687d38803ffdccfb39562e
4
+ data.tar.gz: 41f63196c4d17ae538d4e14acd8d2b2aa8bb45be
5
5
  SHA512:
6
- metadata.gz: bef1314438464ce917392b5c9d7fdaaec78d1a034b2540441ca31b8f1671f5e9d38d684747b43b51ce612f6184532724811d8f93efa6daccbcf77739d5807880
7
- data.tar.gz: eb63d59f1982222964bc54db8af7c0c11b40b7718920312418c5b4278b6e7a825f17612eb0dd9f5b71ba1e4d127ddacf417e39174f0ea39c9128c2557f39eb7f
6
+ metadata.gz: ead02d4451ea6044fdb1e613af619d3dd8b842ec06bd1d7a22785d5c6710e970836bef13c715322b9824efc446d27cc07b326d70adee6be81aef575eacc1196c
7
+ data.tar.gz: aa4e8541337cf0341799366122be1d772663ebc2f988eae15f14bf821176c389c4538554e8b4322ee6bcbf7c1257b27a3c1e30ecd21b6785a5d22907a0b1a118
@@ -4,6 +4,7 @@ install:
4
4
  rvm:
5
5
  - "1.9.3"
6
6
  - "2.1.10"
7
- - "2.2.5"
8
- - "2.3.1"
9
- - "jruby-9.1.5.0"
7
+ - "2.2.7"
8
+ - "2.3.4"
9
+ - "2.4.1"
10
+ - "jruby-9.1.10.0"
@@ -2,6 +2,11 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## 3.2.0
6
+
7
+ - Support `Float` type with `:float`.
8
+ - Tested with Ruby 2.4.
9
+
5
10
  ## 3.1.0
6
11
 
7
12
  - Allow strings 'true' and 'false' to be assigned to boolean attributes and be
data/README.md CHANGED
@@ -212,6 +212,12 @@ Or install it yourself as:
212
212
 
213
213
  $ gem install model_attribute
214
214
 
215
+ ## Testing
216
+
217
+ Running specs:
218
+
219
+ $ rspec
220
+
215
221
  ## Contributing
216
222
 
217
223
  1. [Fork it](https://github.com/yammer/model_attribute/fork)
@@ -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)
@@ -10,6 +10,8 @@ module ModelAttribute
10
10
  float = Float(value)
11
11
  raise ArgumentError, "Can't cast #{value.inspect} to an integer without loss of precision" unless int == float
12
12
  int
13
+ when :float
14
+ Float(value)
13
15
  when :boolean
14
16
  if !!value == value
15
17
  value
@@ -1,3 +1,3 @@
1
1
  module ModelAttribute
2
- VERSION = "3.1.1"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -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" => instance_of(Fixnum))
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" => instance_of(Fixnum))
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.1.1
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-02-12 00:00:00.000000000 Z
11
+ date: 2017-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler