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.
@@ -1,54 +1,62 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoneyAttribute
4
- # Type
5
- class Type < ActiveRecord::Type::Value
6
- def initialize(currency:, column_type: ActiveRecord::Type::Decimal.new)
7
- @currency = currency
8
- @column_type = column_type
9
- super()
10
- end
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
- return if value.currency == @currency
21
+ currency = MoneyAttribute.default_currency
22
+ return if value.currency == currency
25
23
 
26
- message = "'#{value.inspect}' has different currency. Only #{@currency.code} allowed."
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
- def deserialize(value)
34
- return nil unless value
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
- if @column_type.is_a?(ActiveRecord::Type::Integer)
37
- Mint::Money.from_subunits(value, @currency)
38
- else
39
- Mint::Money.from(value, @currency)
40
- end
41
- end
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
- def serialize(value)
44
- return nil unless value
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
- if @column_type.is_a?(ActiveRecord::Type::Integer)
47
- value.subunits
48
- else
49
- value.to_d
50
- end
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoneyAttribute
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -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.1.0
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.10
102
+ rubygems_version: 4.0.17
92
103
  specification_version: 4
93
104
  summary: Money attributes for ActiveRecord
94
105
  test_files: []