money_attribute 1.1.0 → 1.2.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.
- checksums.yaml +4 -4
- data/README.md +117 -18
- data/Rakefile +31 -3
- data/lib/generators/templates/money_attribute.rb +20 -12
- data/lib/money_attribute/attribute_spec.rb +78 -0
- data/lib/money_attribute/attribute_spec_registry.rb +50 -0
- data/lib/money_attribute/configuration.rb +43 -15
- data/lib/money_attribute/converter.rb +14 -4
- data/lib/money_attribute/{core_ext.rb → core_ext/numeric.rb} +1 -1
- data/lib/money_attribute/current.rb +13 -0
- data/lib/money_attribute/form_builder_extension.rb +2 -0
- data/lib/money_attribute/macro.rb +45 -68
- data/lib/money_attribute/migration_extensions/helper.rb +1 -2
- data/lib/money_attribute/money_amount.rb +20 -24
- data/lib/money_attribute/query/amount_condition.rb +48 -0
- data/lib/money_attribute/query/amount_order.rb +22 -0
- data/lib/money_attribute/query/currency_condition.rb +23 -0
- data/lib/money_attribute/query/helpers.rb +30 -0
- data/lib/money_attribute/query/pick.rb +38 -0
- data/lib/money_attribute/query/pluck.rb +43 -0
- data/lib/money_attribute/query/sum.rb +40 -0
- data/lib/money_attribute/query.rb +120 -0
- data/lib/money_attribute/railtie.rb +7 -1
- data/lib/money_attribute/type.rb +41 -33
- data/lib/money_attribute/version.rb +1 -1
- data/lib/money_attribute.rb +15 -1
- metadata +14 -3
data/lib/money_attribute/type.rb
CHANGED
|
@@ -1,54 +1,62 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module MoneyAttribute
|
|
4
|
-
#
|
|
5
|
-
class
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def cast(value)
|
|
13
|
-
if value.is_a?(String)
|
|
14
|
-
Mint::Money.parse(value, @currency)
|
|
15
|
-
else
|
|
16
|
-
super
|
|
17
|
-
end
|
|
18
|
-
end
|
|
4
|
+
# Base type for money amount attributes. Handles casting and validation.
|
|
5
|
+
class AmountType < ActiveRecord::Type::Value
|
|
6
|
+
# Casts string input into a +Mint::Money+ value.
|
|
7
|
+
#
|
|
8
|
+
# @param value [String, Numeric, Mint::Money, nil] the input value
|
|
9
|
+
# @return [Mint::Money, Numeric, nil] a Money value for strings, otherwise delegates to super
|
|
10
|
+
def cast(value) = value.is_a?(String) ? Money.parse(value, MoneyAttribute.default_currency) : super
|
|
19
11
|
|
|
12
|
+
# Validates that the value is compatible with the fixed currency type.
|
|
13
|
+
#
|
|
14
|
+
# @param value [Object] the value to validate
|
|
15
|
+
# @return [void]
|
|
16
|
+
# @raise [ArgumentError] when the value has a mismatched currency or is an unsupported type
|
|
20
17
|
def assert_valid_value(value)
|
|
21
18
|
case value
|
|
22
19
|
when NilClass, Numeric, String then return
|
|
23
20
|
when Mint::Money
|
|
24
|
-
|
|
21
|
+
currency = MoneyAttribute.default_currency
|
|
22
|
+
return if value.currency == currency
|
|
25
23
|
|
|
26
|
-
message = "'#{value.inspect}' has different currency. Only #{
|
|
24
|
+
message = "'#{value.inspect}' has different currency. Only #{currency.code} allowed."
|
|
27
25
|
else
|
|
28
26
|
message = "'#{value.inspect}' is not a valid type for the attribute."
|
|
29
27
|
end
|
|
30
28
|
raise ArgumentError, message
|
|
31
29
|
end
|
|
30
|
+
end
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
# Type for integer columns storing subunits (e.g. cents).
|
|
33
|
+
class IntegerAmountType < AmountType
|
|
34
|
+
# Deserializes a subunit integer into a +Mint::Money+ value.
|
|
35
|
+
#
|
|
36
|
+
# @param value [Integer, nil] the raw database value
|
|
37
|
+
# @return [Mint::Money, nil]
|
|
38
|
+
def deserialize(value) = value && Money.from_subunits(value.to_i, MoneyAttribute.default_currency)
|
|
35
39
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
# Serializes a +Mint::Money+ value into subunits.
|
|
41
|
+
#
|
|
42
|
+
# @param value [Mint::Money, nil]
|
|
43
|
+
# @return [Integer, nil]
|
|
44
|
+
def serialize(value) = value&.subunits
|
|
45
|
+
end
|
|
42
46
|
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
# Type for decimal columns storing unit values (e.g. 12.34).
|
|
48
|
+
class DecimalAmountType < AmountType
|
|
49
|
+
# Deserializes a decimal value into a +Mint::Money+ value.
|
|
50
|
+
#
|
|
51
|
+
# @param value [BigDecimal, nil] the raw database value
|
|
52
|
+
# @return [Mint::Money, nil]
|
|
53
|
+
def deserialize(value) = value && Money.from(value, MoneyAttribute.default_currency)
|
|
45
54
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
end
|
|
55
|
+
# Serializes a +Mint::Money+ value into a decimal.
|
|
56
|
+
#
|
|
57
|
+
# @param value [Mint::Money, nil]
|
|
58
|
+
# @return [BigDecimal, nil]
|
|
59
|
+
def serialize(value) = value&.to_d
|
|
52
60
|
end
|
|
53
61
|
end
|
|
54
62
|
|
data/lib/money_attribute.rb
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# MoneyAttribute provides ActiveRecord integrations for the Minting money gem.
|
|
4
|
+
#
|
|
5
|
+
# Two storage modes:
|
|
6
|
+
#
|
|
7
|
+
# - +money_attribute :price+ — composite (amount + currency columns)
|
|
8
|
+
# - +money_amount :price+ — single column (fixed currency)
|
|
9
|
+
#
|
|
10
|
+
# @see MoneyAttribute::Macro
|
|
11
|
+
# @see MoneyAttribute::MoneyAmount
|
|
12
|
+
|
|
3
13
|
require 'minting'
|
|
4
|
-
require 'money_attribute/core_ext'
|
|
14
|
+
require 'money_attribute/core_ext/numeric'
|
|
5
15
|
require 'money_attribute/core_ext/string'
|
|
6
16
|
require 'money_attribute/configuration'
|
|
17
|
+
require 'money_attribute/current'
|
|
18
|
+
require 'money_attribute/attribute_spec'
|
|
19
|
+
require 'money_attribute/attribute_spec_registry'
|
|
7
20
|
require 'money_attribute/macro'
|
|
8
21
|
require 'money_attribute/money_amount'
|
|
9
22
|
require 'money_attribute/converter'
|
|
10
23
|
require 'money_attribute/type'
|
|
11
24
|
require 'money_attribute/form_builder_extension'
|
|
25
|
+
require 'money_attribute/query'
|
|
12
26
|
require 'money_attribute/railtie'
|
|
13
27
|
require 'money_attribute/version'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: money_attribute
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gilson Ferraz
|
|
@@ -50,16 +50,27 @@ files:
|
|
|
50
50
|
- lib/generators/money_attribute/initializer_generator.rb
|
|
51
51
|
- lib/generators/templates/money_attribute.rb
|
|
52
52
|
- lib/money_attribute.rb
|
|
53
|
+
- lib/money_attribute/attribute_spec.rb
|
|
54
|
+
- lib/money_attribute/attribute_spec_registry.rb
|
|
53
55
|
- lib/money_attribute/configuration.rb
|
|
54
56
|
- lib/money_attribute/converter.rb
|
|
55
|
-
- lib/money_attribute/core_ext.rb
|
|
57
|
+
- lib/money_attribute/core_ext/numeric.rb
|
|
56
58
|
- lib/money_attribute/core_ext/string.rb
|
|
59
|
+
- lib/money_attribute/current.rb
|
|
57
60
|
- lib/money_attribute/form_builder_extension.rb
|
|
58
61
|
- lib/money_attribute/macro.rb
|
|
59
62
|
- lib/money_attribute/migration_extensions/helper.rb
|
|
60
63
|
- lib/money_attribute/migration_extensions/schema_statements.rb
|
|
61
64
|
- lib/money_attribute/migration_extensions/table_definition.rb
|
|
62
65
|
- lib/money_attribute/money_amount.rb
|
|
66
|
+
- lib/money_attribute/query.rb
|
|
67
|
+
- lib/money_attribute/query/amount_condition.rb
|
|
68
|
+
- lib/money_attribute/query/amount_order.rb
|
|
69
|
+
- lib/money_attribute/query/currency_condition.rb
|
|
70
|
+
- lib/money_attribute/query/helpers.rb
|
|
71
|
+
- lib/money_attribute/query/pick.rb
|
|
72
|
+
- lib/money_attribute/query/pluck.rb
|
|
73
|
+
- lib/money_attribute/query/sum.rb
|
|
63
74
|
- lib/money_attribute/railtie.rb
|
|
64
75
|
- lib/money_attribute/type.rb
|
|
65
76
|
- lib/money_attribute/version.rb
|
|
@@ -88,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
88
99
|
- !ruby/object:Gem::Version
|
|
89
100
|
version: '0'
|
|
90
101
|
requirements: []
|
|
91
|
-
rubygems_version: 4.0.
|
|
102
|
+
rubygems_version: 4.0.17
|
|
92
103
|
specification_version: 4
|
|
93
104
|
summary: Money attributes for ActiveRecord
|
|
94
105
|
test_files: []
|