datamapper-money 0.0.2 → 0.0.3
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 +7 -0
- data/.github/workflows/test.yml +21 -0
- data/Gemfile +4 -0
- data/README.md +21 -2
- data/Rakefile +7 -0
- data/lib/datamapper-money/version.rb +1 -1
- data/lib/datamapper-money.rb +3 -3
- data/test/money_test.rb +40 -0
- metadata +20 -23
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: eb54d024ff40fe3261e1a1cbd037e165cc95ee8567c28094458aae140e97b06b
|
|
4
|
+
data.tar.gz: 3183d2c8015fd8d393e28996431f930f7b572dd5c983a676bcd3600fd7f809d5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a4d8c5bf8dfd32b14e4de8e34cc4adfd5c3d1a99651497fb69f85efd9a1a8f0693f7b999fdf8fed194883d35c0e661e95649ad077572a404e80cc597370ad326
|
|
7
|
+
data.tar.gz: b7171fec4f9aba053d2ce7d87803218f7b25469c615044c00c96b5cf98fe27494d5bffba65be61dd5ecd6842810175944e39c85666c77cc6ce2272bd8ea906cc
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
test:
|
|
9
|
+
runs-on: ubuntu-22.04
|
|
10
|
+
strategy:
|
|
11
|
+
fail-fast: false
|
|
12
|
+
matrix:
|
|
13
|
+
ruby: ['2.4', '2.5']
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: ruby/setup-ruby@v1
|
|
17
|
+
with:
|
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
|
19
|
+
bundler-cache: true
|
|
20
|
+
- run: bundle exec rake test
|
|
21
|
+
- run: gem build datamapper-money.gemspec
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
# datamapper-money
|
|
1
|
+
# datamapper-money [](https://github.com/smudge/datamapper-money/actions/workflows/test.yml) [](https://rubygems.org/gems/datamapper-money)
|
|
2
2
|
|
|
3
3
|
Adds a 'money' instance method to DataMapper-based models, for creating properties powered by the 'money' rubygem.
|
|
4
4
|
|
|
5
|
+
This gem uses the legacy DataMapper 1.2 and Money 5 libraries. Their SQLite
|
|
6
|
+
adapter is not compatible with current Ruby releases; CI tests older Rubies.
|
|
7
|
+
|
|
5
8
|
## Installation
|
|
6
9
|
|
|
7
10
|
Add this line to your application's Gemfile:
|
|
@@ -18,7 +21,23 @@ Or install it yourself as:
|
|
|
18
21
|
|
|
19
22
|
## Usage
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
```ruby
|
|
25
|
+
require 'datamapper-money'
|
|
26
|
+
|
|
27
|
+
class Product
|
|
28
|
+
include DataMapper::Resource
|
|
29
|
+
|
|
30
|
+
property :id, Serial
|
|
31
|
+
money :price
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
product = Product.new
|
|
35
|
+
product.price = Money.new(1234, 'USD')
|
|
36
|
+
product.price.cents # => 1234
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The `money` property stores its value in `price_cents` and `price_currency`
|
|
40
|
+
fields. Configure a DataMapper repository before creating or loading models.
|
|
22
41
|
|
|
23
42
|
## Contributing
|
|
24
43
|
|
data/Rakefile
CHANGED
data/lib/datamapper-money.rb
CHANGED
|
@@ -20,11 +20,11 @@ module DataMapper
|
|
|
20
20
|
name_cents = "#{name}_cents"
|
|
21
21
|
name_currency = "#{name}_currency"
|
|
22
22
|
|
|
23
|
-
options_cents = options.
|
|
23
|
+
options_cents = options.select { |key, _| [:required, :precision, :scale].include?(key) }.merge(:accessor => :private)
|
|
24
24
|
options_cents.merge!(:default => default.cents) if default
|
|
25
25
|
self.property name_cents.to_sym, DataMapper::Property::Decimal, options_cents
|
|
26
26
|
|
|
27
|
-
options_currency = options.
|
|
27
|
+
options_currency = options.select { |key, _| key == :required }.merge(:accessor => :private, :length => 3)
|
|
28
28
|
options_currency.merge!(:default => default.currency.to_s) if default
|
|
29
29
|
self.property name_currency.to_sym, DataMapper::Property::String, options_currency
|
|
30
30
|
|
|
@@ -33,7 +33,7 @@ module DataMapper
|
|
|
33
33
|
def #{name}
|
|
34
34
|
return #{instance_variable_name} if defined?(#{instance_variable_name})
|
|
35
35
|
return unless #{name_cents} && #{name_currency}
|
|
36
|
-
#{instance_variable_name} = Money.new(#{name_cents}, #{name_currency})
|
|
36
|
+
#{instance_variable_name} = ::Money.new(#{name_cents}, #{name_currency})
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
#{property.writer_visibility}
|
data/test/money_test.rb
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require 'dm-sqlite-adapter'
|
|
3
|
+
require 'datamapper-money'
|
|
4
|
+
|
|
5
|
+
DataMapper.setup(:default, 'sqlite::memory:')
|
|
6
|
+
|
|
7
|
+
class Product
|
|
8
|
+
include DataMapper::Resource
|
|
9
|
+
|
|
10
|
+
property :id, Serial
|
|
11
|
+
money :price
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
DataMapper.finalize
|
|
15
|
+
DataMapper.repository.adapter.execute(
|
|
16
|
+
'CREATE TABLE products (id INTEGER PRIMARY KEY, price_cents DECIMAL, price_currency VARCHAR(3))'
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
class MoneyTest < Minitest::Test
|
|
20
|
+
def test_assigns_a_money_value
|
|
21
|
+
product = Product.new
|
|
22
|
+
product.price = Money.new(1234, 'USD')
|
|
23
|
+
|
|
24
|
+
assert_equal 1234, product.price.cents
|
|
25
|
+
assert_equal 'USD', product.price.currency.to_s
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_persists_and_loads_a_money_value
|
|
29
|
+
product = Product.create!(price: Money.new(1234, 'USD'))
|
|
30
|
+
loaded = Product.get(product.id)
|
|
31
|
+
loaded.reload
|
|
32
|
+
|
|
33
|
+
assert_equal 1234, loaded.price.cents
|
|
34
|
+
assert_equal 'USD', loaded.price.currency.to_s
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_unset_value_is_nil
|
|
38
|
+
assert_nil Product.new.price
|
|
39
|
+
end
|
|
40
|
+
end
|
metadata
CHANGED
|
@@ -1,46 +1,41 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: datamapper-money
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
5
|
-
prerelease:
|
|
4
|
+
version: 0.0.3
|
|
6
5
|
platform: ruby
|
|
7
6
|
authors:
|
|
8
7
|
- Smudge
|
|
9
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 2026-11-26 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: dm-core
|
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
|
17
|
-
none: false
|
|
18
16
|
requirements:
|
|
19
|
-
- - ~>
|
|
17
|
+
- - "~>"
|
|
20
18
|
- !ruby/object:Gem::Version
|
|
21
19
|
version: 1.2.0
|
|
22
20
|
type: :runtime
|
|
23
21
|
prerelease: false
|
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
-
none: false
|
|
26
23
|
requirements:
|
|
27
|
-
- - ~>
|
|
24
|
+
- - "~>"
|
|
28
25
|
- !ruby/object:Gem::Version
|
|
29
26
|
version: 1.2.0
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
|
31
28
|
name: money
|
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
|
33
|
-
none: false
|
|
34
30
|
requirements:
|
|
35
|
-
- - ~>
|
|
31
|
+
- - "~>"
|
|
36
32
|
- !ruby/object:Gem::Version
|
|
37
33
|
version: 5.0.0
|
|
38
34
|
type: :runtime
|
|
39
35
|
prerelease: false
|
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
-
none: false
|
|
42
37
|
requirements:
|
|
43
|
-
- - ~>
|
|
38
|
+
- - "~>"
|
|
44
39
|
- !ruby/object:Gem::Version
|
|
45
40
|
version: 5.0.0
|
|
46
41
|
description: Adds a 'money' instance method to DataMapper-based models, for creating
|
|
@@ -51,7 +46,8 @@ executables: []
|
|
|
51
46
|
extensions: []
|
|
52
47
|
extra_rdoc_files: []
|
|
53
48
|
files:
|
|
54
|
-
- .
|
|
49
|
+
- ".github/workflows/test.yml"
|
|
50
|
+
- ".gitignore"
|
|
55
51
|
- Gemfile
|
|
56
52
|
- LICENSE.txt
|
|
57
53
|
- README.md
|
|
@@ -59,28 +55,29 @@ files:
|
|
|
59
55
|
- datamapper-money.gemspec
|
|
60
56
|
- lib/datamapper-money.rb
|
|
61
57
|
- lib/datamapper-money/version.rb
|
|
58
|
+
- test/money_test.rb
|
|
62
59
|
homepage: http://github.com/Smudge/datamapper-money
|
|
63
60
|
licenses: []
|
|
64
|
-
|
|
61
|
+
metadata: {}
|
|
62
|
+
post_install_message:
|
|
65
63
|
rdoc_options: []
|
|
66
64
|
require_paths:
|
|
67
65
|
- lib
|
|
68
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
-
none: false
|
|
70
67
|
requirements:
|
|
71
|
-
- -
|
|
68
|
+
- - ">="
|
|
72
69
|
- !ruby/object:Gem::Version
|
|
73
70
|
version: '0'
|
|
74
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
|
-
none: false
|
|
76
72
|
requirements:
|
|
77
|
-
- -
|
|
73
|
+
- - ">="
|
|
78
74
|
- !ruby/object:Gem::Version
|
|
79
75
|
version: '0'
|
|
80
76
|
requirements: []
|
|
81
|
-
rubyforge_project:
|
|
82
|
-
rubygems_version:
|
|
83
|
-
signing_key:
|
|
84
|
-
specification_version:
|
|
77
|
+
rubyforge_project:
|
|
78
|
+
rubygems_version: 2.7.6.3
|
|
79
|
+
signing_key:
|
|
80
|
+
specification_version: 4
|
|
85
81
|
summary: DataMapper-enabled use of 'money' rubygem.
|
|
86
|
-
test_files:
|
|
82
|
+
test_files:
|
|
83
|
+
- test/money_test.rb
|