activerecord-has_money 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +27 -0
- data/Rakefile +2 -0
- data/activerecord-has_money.gemspec +23 -0
- data/lib/activerecord/has_money.rb +24 -0
- data/lib/activerecord/has_money_spec.rb +14 -0
- data/lib/activerecord-has_money/railtie.rb +9 -0
- data/lib/activerecord-has_money/version.rb +5 -0
- data/lib/activerecord-has_money.rb +14 -0
- data/spec/activerecord/has_money_spec.rb +63 -0
- data/spec/spec_helper.rb +8 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 81a27ed9aadcc6b35eff80ab5352ed187f664483
|
4
|
+
data.tar.gz: 1556aa32f6c91c6aa03f80bfffe3653e9dde0d80
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3833b5832975b05a99be2ed23a38a217e87f93b255e1e4b9ae8d12c5151387575cc6957c1c58b962cda7fdef9fdd19c6a1a51d4c64ed7208dd11b1a72f4d6204
|
7
|
+
data.tar.gz: 97c7610c71fff0f49717ce8c1a87e4aa5a21b93d78202b896ef4fef2956805e42782e27e56c215497ccc3cc80085a0be7e42cb423dd303f35a63eb47fbf2236d
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.2-p290@activerecord-has_money --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Philip Champon
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Description
|
2
|
+
|
3
|
+
The has_money gem is intended to allow callers to conveniently convert an attribute stored in cents to dollars.
|
4
|
+
|
5
|
+
# Configuration
|
6
|
+
|
7
|
+
1. Add it to your `Gemfile`
|
8
|
+
* `gem 'activerecord-has_money'`
|
9
|
+
1. Add it to your model
|
10
|
+
* `has_money :total_budget`
|
11
|
+
1. Add it to your tests
|
12
|
+
* `it { should have_money_for(:total_budget) }`
|
13
|
+
|
14
|
+
# Usage
|
15
|
+
|
16
|
+
1. The original attribute is left unmolested
|
17
|
+
1. The virtual attribute `total_budget_dollars` is a Money object
|
18
|
+
* +, -, /, * integers or floats to a Money object will yield a casting error
|
19
|
+
* `total_budget_dollars.to_f + 100` will work
|
20
|
+
1. Assuming the above example, you can save models with dollar values
|
21
|
+
* `Model.new(total_budget_dollars: 100)`
|
22
|
+
* The resulting attribute will be `total_budget 10000`
|
23
|
+
|
24
|
+
# Copyright
|
25
|
+
|
26
|
+
Copyright (c) 2012 Adaptly. See LICENSE.txt for further details.
|
27
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/activerecord-has_money/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Philip Champon"]
|
6
|
+
gem.email = ["philip@adaptly.com"]
|
7
|
+
gem.description = %q{Allow attributes stored in cents to be converted to dollars}
|
8
|
+
gem.summary = %q{Provide virtual methods (ATTR_dollars) in models and provide matchers for testing}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "activerecord-has_money"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActiveRecord::HasMoney::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rake"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_runtime_dependency "money"
|
21
|
+
gem.add_runtime_dependency "activerecord", "~>3"
|
22
|
+
gem.add_runtime_dependency "railties", "~>3"
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module HasMoney
|
3
|
+
def has_money(*attributes)
|
4
|
+
attributes.each do |attribute|
|
5
|
+
define_method "#{attribute}_dollars" do
|
6
|
+
value = send(attribute)
|
7
|
+
if value.respond_to?(:to_money)
|
8
|
+
Money.new(send(attribute))
|
9
|
+
else
|
10
|
+
Money.new(0)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
define_method "#{attribute}_dollars=" do |value|
|
15
|
+
if value.respond_to?(:to_money)
|
16
|
+
send("#{attribute}=", value.to_money.cents)
|
17
|
+
else
|
18
|
+
raise ArgumentError, "Can't convert #{value.class} to Money"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
RSpec::Matchers.define(:have_money_for) do |attribute|
|
2
|
+
description do
|
3
|
+
"#{attribute}_dollars should return #{attribute} as a dollar value"
|
4
|
+
end
|
5
|
+
|
6
|
+
match do |model|
|
7
|
+
expected = (model.send(attribute) / 100.0).to_money
|
8
|
+
model.send("#{attribute}_dollars") == expected
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_for_should do
|
12
|
+
"#{attribute}_dollars should return #{attribute} as a dollar value"
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rails"
|
2
|
+
require "money"
|
3
|
+
require "activerecord/has_money"
|
4
|
+
require "activerecord-has_money/railtie"
|
5
|
+
require "activerecord-has_money/version"
|
6
|
+
|
7
|
+
if defined?(RSpec)
|
8
|
+
require "activerecord/has_money_spec.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
module ActiveRecord
|
12
|
+
module HasMoney
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'activerecord/has_money'
|
3
|
+
require 'money'
|
4
|
+
|
5
|
+
describe ActiveRecord::HasMoney do
|
6
|
+
let (:host) { Class.new.extend(ActiveRecord::HasMoney) }
|
7
|
+
let (:host_instance) { host.new }
|
8
|
+
|
9
|
+
describe "extending a class" do
|
10
|
+
it "adds a has_money class method" do
|
11
|
+
host.should respond_to(:has_money)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe ".has_money with :spent" do
|
16
|
+
before :each do
|
17
|
+
host.class_exec do
|
18
|
+
attr_accessor :spent
|
19
|
+
has_money :spent
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "adds #spent_dollars method to the host class" do
|
24
|
+
host_instance.should respond_to(:spent_dollars)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "adds #spent_dollars= method to the host class" do
|
28
|
+
host_instance.should respond_to(:spent_dollars=)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#spent_dollars" do
|
32
|
+
context "with a spent value that responds to #to_money" do
|
33
|
+
it "returns a Money object initialized with the spent attribute" do
|
34
|
+
host_instance.spent = 100
|
35
|
+
host_instance.spent_dollars.should == Money.new(100)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with a spent value that does not respond to #to_money" do
|
40
|
+
it "returns a Money object with a default of 0 cents" do
|
41
|
+
host_instance.spent = nil
|
42
|
+
host_instance.spent_dollars.should == Money.new(0)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#spent_dollars=" do
|
48
|
+
context "with a value that responds to #to_money" do
|
49
|
+
it "assigns cents value to spent" do
|
50
|
+
host_instance.spent_dollars = 100
|
51
|
+
host_instance.spent.should == 10000
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "with a value that does not respond to #to_money" do
|
56
|
+
it "raises and error" do
|
57
|
+
expect { host_instance.spent_dollars = nil }.to raise_error
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-has_money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Philip Champon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: money
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: railties
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3'
|
83
|
+
description: Allow attributes stored in cents to be converted to dollars
|
84
|
+
email:
|
85
|
+
- philip@adaptly.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rvmrc"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- activerecord-has_money.gemspec
|
98
|
+
- lib/activerecord-has_money.rb
|
99
|
+
- lib/activerecord-has_money/railtie.rb
|
100
|
+
- lib/activerecord-has_money/version.rb
|
101
|
+
- lib/activerecord/has_money.rb
|
102
|
+
- lib/activerecord/has_money_spec.rb
|
103
|
+
- spec/activerecord/has_money_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: ''
|
106
|
+
licenses: []
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.5.1
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Provide virtual methods (ATTR_dollars) in models and provide matchers for
|
128
|
+
testing
|
129
|
+
test_files:
|
130
|
+
- spec/activerecord/has_money_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
has_rdoc:
|