encapsulate_as_money 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 871fc3992a7bc06f07246f53da916ac9488782a5
4
+ data.tar.gz: cfb3f79877ec1b350bd984235f57126051840ef7
5
+ SHA512:
6
+ metadata.gz: 11fd7c0c1ea70da59ea3ec0a19e6771cbcac143b63ea1e6c35fe81f2aaa4f3e0f058ecf64bc9a277d4a481c8f6a9be145a1482b99597395d6f59e42b0eaafc10
7
+ data.tar.gz: 9e2dd5dd3a400dea78ac0bf13c0c44d5b2ce1775fe824942e81d340d2580588f528597415764d996495122892fbb57f2e3a9c52be1c55fd1114f28a5e0b4c048
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ bin/
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 1.9.3
5
+ notifications:
6
+ email:
7
+ recipients:
8
+ - orien@envato.com
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in encapsulate_as_money.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Envato
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,71 @@
1
+ # EncapsulateAsMoney
2
+ [![Build Status](https://travis-ci.org/envato/encapsulate_as_money.svg?branch=master)](https://travis-ci.org/envato/encapsulate_as_money)
3
+
4
+ Want your model attribute to be a [Money](https://github.com/RubyMoney/money)
5
+ instance? EncapsulateAsMoney provides a simple way to get this done!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'encapsulate_as_money'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ### Rails
20
+
21
+ Add the `encapsulate_as_money` method to the Active Record base class.
22
+
23
+ ```ruby
24
+ ActiveRecord::Base.extend(EncapsulateAsMoney)
25
+ ```
26
+
27
+ Now say you have the model:
28
+
29
+ ```ruby
30
+ class MyModel < ActiveRecord::Base
31
+ encapsulate_as_money :amount
32
+ end
33
+ ```
34
+
35
+ Which is based on the database table:
36
+
37
+ ```ruby
38
+ create_table "my_models" do |table|
39
+ table.integer "amount"
40
+ end
41
+ ```
42
+
43
+ Now we can create and save an instance like:
44
+
45
+ ```ruby
46
+ MyModel.create!(amount: 5.dollars)
47
+ ```
48
+
49
+ This will create a row as such:
50
+
51
+ | id | amount |
52
+ | --:| ------:|
53
+ | 1 | 500 |
54
+
55
+ Note the value is represented as cents.
56
+
57
+ Once persisted we can find the value like:
58
+
59
+ ```ruby
60
+ MyModel.find_by_id(1).amount #=> 5.dollars
61
+ ```
62
+
63
+ Note that it uses the default Money currency.
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it ( https://github.com/envato/encapsulate_as_money/fork )
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :spec
5
+
6
+ Rake::TestTask.new(:spec) do |t|
7
+ t.libs << "spec"
8
+ t.test_files = Dir.glob("spec/**/*_spec.rb")
9
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "encapsulate_as_money/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "encapsulate_as_money"
8
+ spec.version = EncapsulateAsMoney::VERSION
9
+ spec.authors = ["Anthony Sellitti", "Orien Madgwick", "Keith Pitt", "Martin Jagusch", "Mark Turnley", "Pete Yandall"]
10
+ spec.email = ["anthony.sellitti@envato.com", "_@orien.io", "me@keithpitt.com", "_@mj.io", "mark@envato.com", "pete@envato.com"]
11
+ spec.summary = "Represent model attributes as Money instances"
12
+ spec.homepage = "https://github.com/envato/encapsulate_as_money"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "money", ">= 5.1.0"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "minitest-given"
25
+ spec.add_development_dependency "yard"
26
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require "encapsulate_as_money/version"
3
+ require "money"
4
+
5
+ module EncapsulateAsMoney
6
+ def encapsulate_as_money(*attributes)
7
+ options = extract_options(attributes)
8
+ attributes.each do |attribute|
9
+ encapsulate_attribute_as_money(attribute, options[:preserve_nil])
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def encapsulate_attribute_as_money(attribute, preserve_nil = true)
16
+ if preserve_nil
17
+ define_method attribute do
18
+ Money.new(super()) if super()
19
+ end
20
+ else
21
+ define_method attribute do
22
+ Money.new(super() || 0)
23
+ end
24
+ end
25
+
26
+ define_method "#{attribute}=" do |money|
27
+ super(money && money.fractional)
28
+ end
29
+ end
30
+
31
+ def extract_options(args)
32
+ args.last.is_a?(Hash) ? args.pop : {}
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module EncapsulateAsMoney
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe EncapsulateAsMoney do
5
+
6
+ Given(:model_base_class_with_attr) {
7
+ Class.new {
8
+ extend EncapsulateAsMoney
9
+ attr_accessor :attribute
10
+ }
11
+ }
12
+ Given(:model_instance) { model_class.new }
13
+ Given!(:init_attr_value) { model_instance.instance_variable_set :@attribute, initial_attr_value }
14
+
15
+ describe "encapsulating the attribute as money" do
16
+ When(:model_class) {
17
+ Class.new(model_base_class_with_attr) {
18
+ encapsulate_as_money :attribute
19
+ }
20
+ }
21
+
22
+ context "initial value is nil" do
23
+ Given(:initial_attr_value) { nil }
24
+ Then { model_instance.attribute == Money.new(0) }
25
+ end
26
+
27
+ context "initial value is 0" do
28
+ Given(:initial_attr_value) { 0 }
29
+ Then { model_instance.attribute == Money.new(0) }
30
+ end
31
+
32
+ context "initial value is 1" do
33
+ Given(:initial_attr_value) { 1 }
34
+ Then { model_instance.attribute == Money.new(1) }
35
+ end
36
+ end
37
+
38
+ describe "encapsulating the attribute as money, preserving nil" do
39
+ When(:model_class) {
40
+ Class.new(model_base_class_with_attr) {
41
+ encapsulate_as_money :attribute, :preserve_nil => true
42
+ }
43
+ }
44
+
45
+ context "initial value is nil" do
46
+ Given(:initial_attr_value) { nil }
47
+ Then { model_instance.attribute == nil }
48
+ end
49
+
50
+ context "initial value is 0" do
51
+ Given(:initial_attr_value) { 0 }
52
+ Then { model_instance.attribute == Money.new(0) }
53
+ end
54
+
55
+ context "initial value is 1" do
56
+ Given(:initial_attr_value) { 1 }
57
+ Then { model_instance.attribute == Money.new(1) }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "encapsulate_as_money"
3
+ require "minitest/autorun"
4
+ require "minitest/given"
5
+ require "minitest/pride"
6
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encapsulate_as_money
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Sellitti
8
+ - Orien Madgwick
9
+ - Keith Pitt
10
+ - Martin Jagusch
11
+ - Mark Turnley
12
+ - Pete Yandall
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+ date: 2014-05-01 00:00:00.000000000 Z
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: money
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 5.1.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 5.1.0
32
+ - !ruby/object:Gem::Dependency
33
+ name: bundler
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '1.6'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '1.6'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ - !ruby/object:Gem::Dependency
61
+ name: minitest-given
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ - !ruby/object:Gem::Dependency
75
+ name: yard
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ description:
89
+ email:
90
+ - anthony.sellitti@envato.com
91
+ - _@orien.io
92
+ - me@keithpitt.com
93
+ - _@mj.io
94
+ - mark@envato.com
95
+ - pete@envato.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files: []
99
+ files:
100
+ - ".gitignore"
101
+ - ".travis.yml"
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - encapsulate_as_money.gemspec
107
+ - lib/encapsulate_as_money.rb
108
+ - lib/encapsulate_as_money/version.rb
109
+ - spec/encapsulate_as_money_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: https://github.com/envato/encapsulate_as_money
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Represent model attributes as Money instances
135
+ test_files:
136
+ - spec/encapsulate_as_money_spec.rb
137
+ - spec/spec_helper.rb
138
+ has_rdoc: