dm-money 0.1.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.
- data/.gitignore +7 -0
- data/Gemfile +12 -0
- data/LICENSE +20 -0
- data/README.rdoc +61 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/dm-money.rb +77 -0
- data/test/helper.rb +19 -0
- data/test/test_money.rb +31 -0
- metadata +85 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Stateless Systems
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= dm-money
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
BigMoney backed DataMapper money properties.
|
6
|
+
|
7
|
+
== Todo
|
8
|
+
|
9
|
+
* Validations.
|
10
|
+
|
11
|
+
== Synopsis
|
12
|
+
|
13
|
+
=== Basic
|
14
|
+
|
15
|
+
require 'dm-core'
|
16
|
+
require 'dm-money'
|
17
|
+
|
18
|
+
class Cake
|
19
|
+
include DataMapper::Resource
|
20
|
+
# ... other properties.
|
21
|
+
|
22
|
+
money :gst
|
23
|
+
# public
|
24
|
+
# gst #=> BigMoney
|
25
|
+
# gst=(value)
|
26
|
+
#
|
27
|
+
# property :gst_amount, BigDecimal, accessor: private
|
28
|
+
# property :gst_currency, String, accessor: private, length: 3
|
29
|
+
end
|
30
|
+
|
31
|
+
=== Property Options
|
32
|
+
|
33
|
+
require 'dm-core'
|
34
|
+
require 'dm-money'
|
35
|
+
|
36
|
+
class Cake
|
37
|
+
include DataMapper::Resource
|
38
|
+
# ... other properties.
|
39
|
+
|
40
|
+
money :price, required: true, accessor: protected
|
41
|
+
# protected
|
42
|
+
# price #=> BigMoney
|
43
|
+
# price=(value)
|
44
|
+
#
|
45
|
+
# property :price_amount, BigDecimal, accessor: private, required: true
|
46
|
+
# property :price_currency, String, accessor: private, required: true, length: 3
|
47
|
+
end
|
48
|
+
|
49
|
+
== Install
|
50
|
+
|
51
|
+
* Via git:
|
52
|
+
|
53
|
+
git clone git://github.com/shanna/dm-money.git
|
54
|
+
|
55
|
+
* Via gem:
|
56
|
+
|
57
|
+
gem install dm-money
|
58
|
+
|
59
|
+
== License
|
60
|
+
|
61
|
+
See LICENSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# vim: syntax=ruby
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
require 'rake/testtask'
|
6
|
+
require 'rake/rdoctask'
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'jeweler'
|
10
|
+
Jeweler::Tasks.new do |gem|
|
11
|
+
gem.name = 'dm-money'
|
12
|
+
gem.summary = %q{BigMoney backed DataMapper money plugin.}
|
13
|
+
gem.email = ['shane.hanna@gmail.com']
|
14
|
+
gem.homepage = 'http://github.com/shanna/dm-money'
|
15
|
+
gem.authors = ['Shane Hanna']
|
16
|
+
gem.executables = [] # Only ever bundled development executables in bin/*
|
17
|
+
gem.add_dependency 'big_money', '~> 1.1.0'
|
18
|
+
gem.add_dependency 'dm-core', '>= 0.10'
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake::TestTask.new(:test) do |test|
|
26
|
+
test.libs << 'lib' << 'test'
|
27
|
+
test.pattern = 'test/**/test_*.rb'
|
28
|
+
test.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
Rake::RDocTask.new do |rdoc|
|
32
|
+
if File.exist?('VERSION.yml')
|
33
|
+
config = YAML.load(File.read('VERSION.yml'))
|
34
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
35
|
+
else
|
36
|
+
version = ""
|
37
|
+
end
|
38
|
+
|
39
|
+
rdoc.rdoc_dir = 'rdoc'
|
40
|
+
rdoc.title = "dm-money #{version}"
|
41
|
+
rdoc.rdoc_files.include('README*')
|
42
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
43
|
+
end
|
44
|
+
|
45
|
+
task :default => :test
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/dm-money.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'big_money'
|
2
|
+
require 'dm-core'
|
3
|
+
|
4
|
+
module DataMapper
|
5
|
+
module Money
|
6
|
+
def self.included(model)
|
7
|
+
model.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
# Add a BigMoney property.
|
13
|
+
#
|
14
|
+
# ==== Parameters
|
15
|
+
# name<Symbol>:: Name of the composite money property.
|
16
|
+
# options<Hash(Symbol => String)>:: DataMapper property options.
|
17
|
+
#
|
18
|
+
# ==== Examples
|
19
|
+
# class Bike
|
20
|
+
# include DataMapper::Resource
|
21
|
+
# # ... other properties.
|
22
|
+
#
|
23
|
+
# money :gst
|
24
|
+
# # public
|
25
|
+
# # gst #=> BigMoney
|
26
|
+
# # gst=(value)
|
27
|
+
# #
|
28
|
+
# # property :gst_amount, BigDecimal, accessor: private
|
29
|
+
# # property :gst_currency, String, accessor: private, length: 3
|
30
|
+
#
|
31
|
+
# money :price, required: true, accessor: protected
|
32
|
+
# # protected
|
33
|
+
# # price #=> BigMoney
|
34
|
+
# # price=(value)
|
35
|
+
# #
|
36
|
+
# # property :price_amount, BigDecimal, accessor: private, required: true
|
37
|
+
# # property :price_currency, String, accessor: private, required: true, length: 3
|
38
|
+
# end
|
39
|
+
#--
|
40
|
+
# TODO: Validations.
|
41
|
+
def money(name, options = {})
|
42
|
+
raise ArgumentError.new('You need to pass at least one argument') if name.empty?
|
43
|
+
|
44
|
+
property = Property.new(self, name, BigDecimal, options)
|
45
|
+
name = property.name.to_s
|
46
|
+
instance_variable_name = property.instance_variable_name
|
47
|
+
name_amount = "#{name}_amount"
|
48
|
+
name_currency = "#{name}_currency"
|
49
|
+
|
50
|
+
self.property name_amount.to_sym, BigDecimal, property.options.except(:accessor, :reader, :writer).merge(:accessor => :private)
|
51
|
+
self.property name_currency.to_sym, String, :accessor => :private, :required => property.required, :length => 3
|
52
|
+
|
53
|
+
# TODO: Access amount, currency via readers or properties?
|
54
|
+
# TODO: Validations or error message attempting to set with something other than BigMoney?
|
55
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
56
|
+
#{property.reader_visibility}
|
57
|
+
def #{name}
|
58
|
+
return #{instance_variable_name} if defined?(#{instance_variable_name})
|
59
|
+
return unless #{name_amount} && #{name_currency}
|
60
|
+
#{instance_variable_name} = BigMoney.new(#{name_amount}, #{name_currency})
|
61
|
+
end
|
62
|
+
|
63
|
+
#{property.writer_visibility}
|
64
|
+
def #{name}=(value)
|
65
|
+
raise TypeError.new('Expected BigMoney +value+ but got \#{value.class}') unless value.kind_of?(BigMoney)
|
66
|
+
self.#{name_amount} = value.amount
|
67
|
+
self.#{name_currency} = value.currency
|
68
|
+
#{instance_variable_name} = value
|
69
|
+
end
|
70
|
+
RUBY
|
71
|
+
end
|
72
|
+
|
73
|
+
end # ClassMethods
|
74
|
+
|
75
|
+
Model.append_inclusions self
|
76
|
+
end # Money
|
77
|
+
end # DataMapper
|
data/test/helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
2
|
+
|
3
|
+
require File.join(root, 'gems', 'environment')
|
4
|
+
Bundler.require_env(:development)
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'shoulda'
|
9
|
+
rescue LoadError
|
10
|
+
warn 'Shoulda is required for testing. Use gem bundle to install development gems.'
|
11
|
+
exit 1
|
12
|
+
end
|
13
|
+
|
14
|
+
$:.unshift File.join(root, 'lib'), File.dirname(__FILE__)
|
15
|
+
require 'dm-money'
|
16
|
+
|
17
|
+
DataMapper.setup(:default, :adapter => 'in_memory', :database => 'dm_money_test')
|
18
|
+
class Test::Unit::TestCase
|
19
|
+
end
|
data/test/test_money.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
class TestMoney < Test::Unit::TestCase
|
4
|
+
context 'BigMoney' do
|
5
|
+
setup do
|
6
|
+
Object.send(:remove_const, :Cake) if defined?(Cake)
|
7
|
+
class ::Cake
|
8
|
+
include DataMapper::Resource
|
9
|
+
property :id, Serial
|
10
|
+
property :type, String
|
11
|
+
money :price
|
12
|
+
end # Cake
|
13
|
+
end
|
14
|
+
|
15
|
+
should 'behave like accessor' do
|
16
|
+
price = BigMoney.new('2.50', :aud)
|
17
|
+
cake = Cake.create(:type => 'carrot')
|
18
|
+
|
19
|
+
assert cake.price = price
|
20
|
+
assert_equal price, cake.price
|
21
|
+
end
|
22
|
+
|
23
|
+
should 'be accepted by constructor' do
|
24
|
+
price = BigMoney.new('2.80', :aud)
|
25
|
+
assert_nothing_raised do
|
26
|
+
cake = Cake.create(:type => 'sponge', :price => price)
|
27
|
+
assert_equal price, cake.price
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end # TestMoney
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shane Hanna
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-25 00:00:00 +11:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: big_money
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: dm-core
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0.10"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email:
|
37
|
+
- shane.hanna@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- LICENSE
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- lib/dm-money.rb
|
53
|
+
- test/helper.rb
|
54
|
+
- test/test_money.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/shanna/dm-money
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: BigMoney backed DataMapper money plugin.
|
83
|
+
test_files:
|
84
|
+
- test/test_money.rb
|
85
|
+
- test/helper.rb
|