encapsulate_as_money 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +7 -2
- data/.yardopts +1 -0
- data/Appraisals +7 -0
- data/README.md +2 -0
- data/Rakefile +5 -5
- data/encapsulate_as_money.gemspec +5 -2
- data/gemfiles/rails_3_2.gemfile +7 -0
- data/gemfiles/rails_4_0.gemfile +7 -0
- data/lib/encapsulate_as_money.rb +9 -3
- data/lib/encapsulate_as_money/activemodel_integration.rb +24 -0
- data/lib/encapsulate_as_money/version.rb +1 -1
- data/script/setup.sh +8 -0
- data/spec/encapsulate_as_money_spec.rb +64 -21
- data/spec/integration/activemodel_spec.rb +19 -0
- data/spec/integration_helper.rb +38 -0
- data/spec/spec_helper.rb +7 -5
- metadata +60 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c6b4e070f12e143adb39e5f507981a3962f35bc
|
4
|
+
data.tar.gz: 362cf15ef24f6f0424a02a309b43deb1a3391fbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bf283d26e20491e4be2a81500133f0cb2ebd909301fe6fb9ecd0e1deb30b2b7f622965e0d2f59f47db639452263bbbd8ff634f5627deec4fdf103d71898ffdb
|
7
|
+
data.tar.gz: 318dac26671a0b62d2651d399b575724c871b7e93a750e9d854e799c28d43335800588d10f0275996dc056cce5d147cacc4ba7539308473b86c2924728cf57fb
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.1.
|
3
|
+
- 2.1.2
|
4
4
|
- 1.9.3
|
5
|
+
before_install:
|
6
|
+
- gem update bundler
|
7
|
+
gemfile:
|
8
|
+
- gemfiles/rails_3_2.gemfile
|
9
|
+
- gemfiles/rails_4_0.gemfile
|
5
10
|
notifications:
|
6
11
|
email:
|
7
12
|
recipients:
|
8
|
-
-
|
13
|
+
- financedev@envato.com
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
- LICENSE.txt
|
data/Appraisals
ADDED
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# EncapsulateAsMoney
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/encapsulate_as_money.svg)](http://badge.fury.io/rb/encapsulate_as_money)
|
2
4
|
[![Build Status](https://travis-ci.org/envato/encapsulate_as_money.svg?branch=master)](https://travis-ci.org/envato/encapsulate_as_money)
|
3
5
|
|
4
6
|
Want your model attribute to be a [Money](https://github.com/RubyMoney/money)
|
data/Rakefile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
-
require
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require "yard"
|
3
4
|
|
4
5
|
task :default => :spec
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
YARD::Rake::YardocTask.new
|
@@ -19,8 +19,11 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.add_dependency "money", ">= 5.1.0"
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "bundler"
|
23
23
|
spec.add_development_dependency "rake"
|
24
|
-
spec.add_development_dependency "
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.0"
|
25
|
+
spec.add_development_dependency "rspec-given"
|
25
26
|
spec.add_development_dependency "yard"
|
27
|
+
spec.add_development_dependency "appraisal"
|
28
|
+
spec.add_development_dependency "pry"
|
26
29
|
end
|
data/lib/encapsulate_as_money.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require "encapsulate_as_money/version"
|
3
|
+
require "encapsulate_as_money/activemodel_integration"
|
3
4
|
require "money"
|
4
5
|
|
5
6
|
module EncapsulateAsMoney
|
@@ -17,14 +18,19 @@ private
|
|
17
18
|
define_method attribute do
|
18
19
|
Money.new(super()) if super()
|
19
20
|
end
|
21
|
+
|
22
|
+
define_method "#{attribute}=" do |money|
|
23
|
+
super(money && money.fractional)
|
24
|
+
end
|
20
25
|
else
|
21
26
|
define_method attribute do
|
22
27
|
Money.new(super() || 0)
|
23
28
|
end
|
24
|
-
end
|
25
29
|
|
26
|
-
|
27
|
-
|
30
|
+
define_method "#{attribute}=" do |money|
|
31
|
+
num = (money && money.fractional) || 0
|
32
|
+
super(num)
|
33
|
+
end
|
28
34
|
end
|
29
35
|
end
|
30
36
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
begin
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
if ActiveModel::VERSION::MAJOR == 4
|
5
|
+
module ActiveModel::Validations::Clusivity
|
6
|
+
private
|
7
|
+
|
8
|
+
def inclusion_method(enumerable)
|
9
|
+
if enumerable.first.is_a?(Money)
|
10
|
+
:cover?
|
11
|
+
else
|
12
|
+
return :include? unless enumerable.is_a?(Range)
|
13
|
+
case enumerable.first
|
14
|
+
when Numeric, Time, DateTime
|
15
|
+
:cover?
|
16
|
+
else
|
17
|
+
:include?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
rescue LoadError
|
24
|
+
end
|
data/script/setup.sh
ADDED
@@ -10,7 +10,6 @@ describe EncapsulateAsMoney do
|
|
10
10
|
}
|
11
11
|
}
|
12
12
|
Given(:model_instance) { model_class.new }
|
13
|
-
Given!(:init_attr_value) { model_instance.instance_variable_set :@attribute, initial_attr_value }
|
14
13
|
|
15
14
|
describe "encapsulating the attribute as money" do
|
16
15
|
When(:model_class) {
|
@@ -19,19 +18,41 @@ describe EncapsulateAsMoney do
|
|
19
18
|
}
|
20
19
|
}
|
21
20
|
|
22
|
-
|
23
|
-
Given(:
|
24
|
-
|
25
|
-
|
21
|
+
describe "reading" do
|
22
|
+
Given!(:init_attr_value) { model_instance.instance_variable_set :@attribute, initial_attr_value }
|
23
|
+
|
24
|
+
context "initial value is nil" do
|
25
|
+
Given(:initial_attr_value) { nil }
|
26
|
+
Then { model_instance.attribute == Money.zero }
|
27
|
+
end
|
28
|
+
|
29
|
+
context "initial value is 0" do
|
30
|
+
Given(:initial_attr_value) { 0 }
|
31
|
+
Then { model_instance.attribute == Money.zero }
|
32
|
+
end
|
26
33
|
|
27
|
-
|
28
|
-
|
29
|
-
|
34
|
+
context "initial value is 1" do
|
35
|
+
Given(:initial_attr_value) { 1 }
|
36
|
+
Then { model_instance.attribute == Money.new(1) }
|
37
|
+
end
|
30
38
|
end
|
31
39
|
|
32
|
-
|
33
|
-
|
34
|
-
|
40
|
+
describe "writing" do
|
41
|
+
|
42
|
+
context "a value of $1" do
|
43
|
+
When { model_instance.attribute = Money.new(1_00) }
|
44
|
+
Then { model_instance.instance_variable_get(:@attribute) == 1_00 }
|
45
|
+
end
|
46
|
+
|
47
|
+
context "a value of $0" do
|
48
|
+
When { model_instance.attribute = Money.zero }
|
49
|
+
Then { model_instance.instance_variable_get(:@attribute) == 0 }
|
50
|
+
end
|
51
|
+
|
52
|
+
context "a value of nil" do
|
53
|
+
When { model_instance.attribute = nil }
|
54
|
+
Then { model_instance.instance_variable_get(:@attribute) == 0 }
|
55
|
+
end
|
35
56
|
end
|
36
57
|
end
|
37
58
|
|
@@ -42,19 +63,41 @@ describe EncapsulateAsMoney do
|
|
42
63
|
}
|
43
64
|
}
|
44
65
|
|
45
|
-
|
46
|
-
Given(:
|
47
|
-
|
48
|
-
|
66
|
+
describe "reading" do
|
67
|
+
Given!(:init_attr_value) { model_instance.instance_variable_set :@attribute, initial_attr_value }
|
68
|
+
|
69
|
+
context "initial value is nil" do
|
70
|
+
Given(:initial_attr_value) { nil }
|
71
|
+
Then { model_instance.attribute == nil }
|
72
|
+
end
|
73
|
+
|
74
|
+
context "initial value is 0" do
|
75
|
+
Given(:initial_attr_value) { 0 }
|
76
|
+
Then { model_instance.attribute == Money.zero }
|
77
|
+
end
|
49
78
|
|
50
|
-
|
51
|
-
|
52
|
-
|
79
|
+
context "initial value is 1" do
|
80
|
+
Given(:initial_attr_value) { 1 }
|
81
|
+
Then { model_instance.attribute == Money.new(1) }
|
82
|
+
end
|
53
83
|
end
|
54
84
|
|
55
|
-
|
56
|
-
|
57
|
-
|
85
|
+
describe "writing" do
|
86
|
+
|
87
|
+
context "a value of $1" do
|
88
|
+
When { model_instance.attribute = Money.new(1_00) }
|
89
|
+
Then { model_instance.instance_variable_get(:@attribute) == 1_00 }
|
90
|
+
end
|
91
|
+
|
92
|
+
context "a value of $0" do
|
93
|
+
When { model_instance.attribute = Money.zero }
|
94
|
+
Then { model_instance.instance_variable_get(:@attribute) == 0 }
|
95
|
+
end
|
96
|
+
|
97
|
+
context "a value of nil" do
|
98
|
+
When { model_instance.attribute = nil }
|
99
|
+
Then { model_instance.instance_variable_get(:@attribute).nil? }
|
100
|
+
end
|
58
101
|
end
|
59
102
|
end
|
60
103
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "ActiveModel integration", skip: !ENV['APPRAISAL_INITIALIZED'] && !ENV['TRAVIS'] do
|
4
|
+
describe '.validates_inclusion_of' do
|
5
|
+
it "validates money correctly" do
|
6
|
+
expect(Price.new(amount: Money.new(0))).not_to be_valid
|
7
|
+
expect(Price.new(amount: Money.new(1))).to be_valid
|
8
|
+
expect(Price.new(amount: Money.new(2))).to be_valid
|
9
|
+
expect(Price.new(amount: Money.new(3))).not_to be_valid
|
10
|
+
end
|
11
|
+
|
12
|
+
it "validates numbers correctly" do
|
13
|
+
expect(Price.new(qty: 0)).not_to be_valid
|
14
|
+
expect(Price.new(qty: 1)).to be_valid
|
15
|
+
expect(Price.new(qty: 2)).to be_valid
|
16
|
+
expect(Price.new(qty: 3)).not_to be_valid
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class Price
|
4
|
+
extend EncapsulateAsMoney
|
5
|
+
|
6
|
+
case ActiveModel::VERSION::MAJOR
|
7
|
+
when 3 then include ActiveModel::AttributeMethods
|
8
|
+
when 4 then include ActiveModel::Model
|
9
|
+
else raise NotImplementedError, ActiveModel::VERSION::MAJOR
|
10
|
+
end
|
11
|
+
|
12
|
+
include ActiveModel::Validations
|
13
|
+
|
14
|
+
encapsulate_as_money :amount
|
15
|
+
validates_inclusion_of :amount, in: Money.new(1)..Money.new(2), allow_nil: true
|
16
|
+
validates_inclusion_of :qty, in: 1..2, allow_nil: true
|
17
|
+
|
18
|
+
def initialize(attributes = {})
|
19
|
+
@amount = attributes[:amount]
|
20
|
+
@qty = attributes[:qty]
|
21
|
+
end
|
22
|
+
|
23
|
+
def amount
|
24
|
+
@amount
|
25
|
+
end
|
26
|
+
|
27
|
+
def amount=(amount)
|
28
|
+
@amount = amount
|
29
|
+
end
|
30
|
+
|
31
|
+
def qty
|
32
|
+
@qty
|
33
|
+
end
|
34
|
+
|
35
|
+
def qty=(qty)
|
36
|
+
@qty = qty
|
37
|
+
end
|
38
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
|
6
|
-
|
2
|
+
require 'encapsulate_as_money'
|
3
|
+
require 'rspec/given'
|
4
|
+
require 'integration_helper' if ENV['APPRAISAL_INITIALIZED'] || ENV['TRAVIS']
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.color = true
|
8
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encapsulate_as_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Sellitti
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date:
|
16
|
+
date: 2016-04-26 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: money
|
@@ -33,16 +33,16 @@ dependencies:
|
|
33
33
|
name: bundler
|
34
34
|
requirement: !ruby/object:Gem::Requirement
|
35
35
|
requirements:
|
36
|
-
- - "
|
36
|
+
- - ">="
|
37
37
|
- !ruby/object:Gem::Version
|
38
|
-
version: '
|
38
|
+
version: '0'
|
39
39
|
type: :development
|
40
40
|
prerelease: false
|
41
41
|
version_requirements: !ruby/object:Gem::Requirement
|
42
42
|
requirements:
|
43
|
-
- - "
|
43
|
+
- - ">="
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version: '
|
45
|
+
version: '0'
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
name: rake
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,7 +58,21 @@ dependencies:
|
|
58
58
|
- !ruby/object:Gem::Version
|
59
59
|
version: '0'
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
|
-
name:
|
61
|
+
name: rspec
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2.0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '2.0'
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: rspec-given
|
62
76
|
requirement: !ruby/object:Gem::Requirement
|
63
77
|
requirements:
|
64
78
|
- - ">="
|
@@ -85,6 +99,34 @@ dependencies:
|
|
85
99
|
- - ">="
|
86
100
|
- !ruby/object:Gem::Version
|
87
101
|
version: '0'
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: appraisal
|
104
|
+
requirement: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
type: :development
|
110
|
+
prerelease: false
|
111
|
+
version_requirements: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
- !ruby/object:Gem::Dependency
|
117
|
+
name: pry
|
118
|
+
requirement: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
type: :development
|
124
|
+
prerelease: false
|
125
|
+
version_requirements: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
88
130
|
description:
|
89
131
|
email:
|
90
132
|
- anthony.sellitti@envato.com
|
@@ -99,14 +141,22 @@ extra_rdoc_files: []
|
|
99
141
|
files:
|
100
142
|
- ".gitignore"
|
101
143
|
- ".travis.yml"
|
144
|
+
- ".yardopts"
|
145
|
+
- Appraisals
|
102
146
|
- Gemfile
|
103
147
|
- LICENSE.txt
|
104
148
|
- README.md
|
105
149
|
- Rakefile
|
106
150
|
- encapsulate_as_money.gemspec
|
151
|
+
- gemfiles/rails_3_2.gemfile
|
152
|
+
- gemfiles/rails_4_0.gemfile
|
107
153
|
- lib/encapsulate_as_money.rb
|
154
|
+
- lib/encapsulate_as_money/activemodel_integration.rb
|
108
155
|
- lib/encapsulate_as_money/version.rb
|
156
|
+
- script/setup.sh
|
109
157
|
- spec/encapsulate_as_money_spec.rb
|
158
|
+
- spec/integration/activemodel_spec.rb
|
159
|
+
- spec/integration_helper.rb
|
110
160
|
- spec/spec_helper.rb
|
111
161
|
homepage: https://github.com/envato/encapsulate_as_money
|
112
162
|
licenses:
|
@@ -128,11 +178,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
178
|
version: '0'
|
129
179
|
requirements: []
|
130
180
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
181
|
+
rubygems_version: 2.5.1
|
132
182
|
signing_key:
|
133
183
|
specification_version: 4
|
134
184
|
summary: Represent model attributes as Money instances
|
135
185
|
test_files:
|
136
186
|
- spec/encapsulate_as_money_spec.rb
|
187
|
+
- spec/integration/activemodel_spec.rb
|
188
|
+
- spec/integration_helper.rb
|
137
189
|
- spec/spec_helper.rb
|
138
190
|
has_rdoc:
|