minting-rails 0.2.0

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
+ SHA256:
3
+ metadata.gz: 281304a9f89250f78bee993b5b4eaec8e675a4f5f8487a91a16a71c52df1ef04
4
+ data.tar.gz: 861df162c0953c9ba0893778b3805da34fe42ec98485498531eaa48b5cf16be2
5
+ SHA512:
6
+ metadata.gz: 1fd2c46e7fa9e8d0a8ea579fe2880af82676518d624b04276f04adaea77826c9c04dd281a4508463a158d823561f6d2502677d217d2526a3873ef6cb0ea05e97
7
+ data.tar.gz: 9186adb993e282d4dc39008d6bb0b32013cdf1c4d267197e920478e7f89b4a21d56b709eca59bd67f28da28e4c39e23c83552ccddf9807a632500d2b33465458
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Gilson Ferraz
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.md ADDED
@@ -0,0 +1,47 @@
1
+ # Mint::Rails
2
+
3
+ Add Money attributes to your Rails application.
4
+
5
+ ## Usage
6
+
7
+ Currently in development. Please wait for 1.0 release.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "minting-rails"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ bundle
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```bash
26
+ gem install minting-rails
27
+ ```
28
+
29
+ After intalling generate Minting configuration initializer:
30
+
31
+ ```sh
32
+ rails g mint:initializer
33
+ ```
34
+
35
+ ## To do
36
+
37
+ - API Documentation
38
+ - Internationalization
39
+ - Publish 1.0 Beta
40
+
41
+ ## Contributing
42
+
43
+ Contribution directions go here.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'bundler/gem_tasks'
5
+ require 'rake/testtask'
6
+
7
+ task default: :test
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'test'
11
+ t.libs << 'lib'
12
+ t.test_files = FileList['test/**/*_test.rb']
13
+ t.ruby_opts << '-rtest_helper.rb'
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mint
4
+ module Generators
5
+ class InitializerGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __dir__)
7
+
8
+ desc 'Creates Minting initializer.'
9
+
10
+ def copy_initializer
11
+ copy_file 'minting.rb', 'config/initializers/minting.rb'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,44 @@
1
+ # encoding : utf-8
2
+ # frozen_string_literal: true
3
+
4
+ Mint.configure do |config|
5
+ # Register additional currencies
6
+ #
7
+ # Example:
8
+ # config.added_currencies = [
9
+ # Mint.register_currency 'CRC', subunit: 2, symbol: '₡'
10
+ # Mint.register_currency 'NGN', subunit: 3, synbol: '₦'
11
+ # ]
12
+ config.added_currencies = [
13
+ Mint.register_currency('CRC', subunit: 2, symbol: '₡'),
14
+ Mint.register_currency('NGN', subunit: 3, symbol: '₦')
15
+ ]
16
+
17
+ # Enable currencies
18
+ # Only these currencies amounts can be created
19
+ # Examples:
20
+ # 1. All registered currencies are enabled (default)
21
+ # config.enabled_currencies = :all
22
+ # 2. Some registered currencies are enabled
23
+ # config.enabled_currencies = %w[BRL CRC NGN USD]
24
+
25
+ # To set the default currency
26
+ #
27
+ # It must be a registered currency
28
+ # config.default_currency = 'USD'
29
+
30
+ # Specify a rounding mode
31
+ # Any one of:
32
+ #
33
+ # BigDecimal::ROUND_UP,
34
+ # BigDecimal::ROUND_DOWN,
35
+ # BigDecimal::ROUND_HALF_UP,
36
+ # BigDecimal::ROUND_HALF_DOWN,
37
+ # BigDecimal::ROUND_HALF_EVEN,
38
+ # BigDecimal::ROUND_CEILING,
39
+ # BigDecimal::ROUND_FLOOR
40
+ #
41
+ # BigDecimal::ROUND_HALF_EVEN is default
42
+ #
43
+ # config.rounding_mode = BigDecimal::ROUND_HALF_UP
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mint
4
+ include ActiveSupport::Configurable
5
+
6
+ def self.assert_valid_currency!(currency)
7
+ if currency.is_a? Mint::Currency
8
+ code = currency.code
9
+ else
10
+ code = currency.to_s
11
+ currency = Mint.currency(code)
12
+ end
13
+ return currency if valid_currency_codes.include?(code)
14
+
15
+ raise ArgumentError,
16
+ "Invalid currency '#{code}'. Please select a registered currency: #{valid_currency_codes}"
17
+ end
18
+
19
+ def self.default_currency
20
+ @default_currency ||= Mint.assert_valid_currency!(config.default_currency)
21
+ end
22
+
23
+ def self.valid_currency_codes
24
+ @valid_currency_codes ||= if config.enabled_currencies == :all
25
+ Mint.currencies
26
+ else
27
+ config.enabled_currencies.to_set
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc
4
+ class Numeric
5
+ def to_money(currency)
6
+ Mint.money(self, currency)
7
+ end
8
+
9
+ def dollars
10
+ Mint.money(self, 'USD')
11
+ end
12
+
13
+ def euros
14
+ Mint.money(self, 'EUR')
15
+ end
16
+
17
+ alias dollar dollars
18
+ alias euro euros
19
+ alias mint to_money
20
+ end
21
+
22
+ # :nodoc
23
+ class String
24
+ def to_money(currency)
25
+ Mint.money(to_r, currency)
26
+ end
27
+
28
+ alias mint to_money
29
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mint
4
+ # MoneyAttribute
5
+ module MoneyAttribute
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ # Money attribute
10
+ def money_attribute(name, currency: Mint.default_currency, mapping: nil)
11
+ currency = Mint.assert_valid_currency!(currency)
12
+ parser = proc { |amount, code = currency| MoneyAttribute.parse(amount, code) }
13
+ if attribute_names.include? name.to_s
14
+ attribute(name, :mint_money, currency:)
15
+ normalizes(name, with: parser)
16
+ else
17
+ aggregated = find_money_attributes(name, mapping:)
18
+ options = {
19
+ allow_nil: true, class_name: 'Mint::Money',
20
+ constructor: parser, converter: parser,
21
+ mapping: { aggregated[:amount] => :to_i, aggregated[:currency] => :currency_code }
22
+ }
23
+ composed_of(name, options)
24
+ end
25
+ end
26
+
27
+ def find_money_attributes(name, mapping:)
28
+ composite = if mapping.present?
29
+ { amount: mapping.key(:amount).to_s, currency: mapping.key(:currency).to_s }
30
+ else
31
+ { amount: "#{name}_amount", currency: "#{name}_currency" }
32
+ end
33
+ if (composite.values & attribute_names).size != 2
34
+ raise ArgumentError, "Could not find attributes to map to #{name} money attribute"
35
+ end
36
+
37
+ composite
38
+ end
39
+ end
40
+
41
+ def self.parse(amount, currency)
42
+ case amount
43
+ when NilClass
44
+ nil
45
+ when Mint::Money
46
+ amount
47
+ when Numeric
48
+ Mint.money(amount, currency)
49
+ else
50
+ if amount.respond_to? :to_money
51
+ amount.to_money(currency)
52
+ else
53
+ Mint.money(amount.to_s.split[0].to_r, currency)
54
+ end
55
+ end
56
+ # puts "parse(#{amount}, #{currency.inspect}) => #{money.inspect}"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mint
4
+ # MintMoneyType
5
+ class MintMoneyType < ActiveRecord::Type::Value
6
+ def initialize(currency:)
7
+ @currency = Mint.currency currency
8
+ super()
9
+ end
10
+
11
+ def assert_valid_value(value)
12
+ case value
13
+ when Numeric, String
14
+ return
15
+ when Mint::Money
16
+ return if value.currency == @currency
17
+
18
+ message = "'#{value.inspect}' has different currency. Only #{@currency.code} allowed."
19
+ else
20
+ message = "'#{value}' is not a valid type for the attribute."
21
+ end
22
+ raise ArgumentError, message
23
+ end
24
+
25
+ def deserialize(value)
26
+ value && Mint.money(value, @currency)
27
+ end
28
+
29
+ def serialize(value)
30
+ value.to_d
31
+ end
32
+
33
+ def self.type
34
+ :mint_type
35
+ end
36
+ end
37
+ end
38
+
39
+ ActiveSupport.on_load(:active_record) do
40
+ ActiveRecord::Base.include Mint::MoneyAttribute
41
+ ActiveRecord::Type.register(:mint_money, Mint::MintMoneyType)
42
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mint
4
+ module MoneyAttribute
5
+ VERSION = '0.2.0'
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minting/money_attribute/core_ext'
4
+ require 'minting/money_attribute/configuration'
5
+ require 'minting/money_attribute/money_attribute'
6
+ require 'minting/money_attribute/money_type'
7
+ require 'minting/money_attribute/version'
8
+ require 'minting/railties'
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mint
4
+ class Railtie < ::Rails::Railtie
5
+ generators do
6
+ require 'generators/minting/initializer_generator'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # desc "Explaining what the task does"
4
+ # task :minting_rails do
5
+ # # Task goes here
6
+ # end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minting-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Gilson Ferraz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.1.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.1.3.2
27
+ description: ''
28
+ email:
29
+ - gilson@cesar.etc.br
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/generators/minting/initializer_generator.rb
38
+ - lib/generators/templates/minting.rb
39
+ - lib/minting/money_attribute/configuration.rb
40
+ - lib/minting/money_attribute/core_ext.rb
41
+ - lib/minting/money_attribute/money_attribute.rb
42
+ - lib/minting/money_attribute/money_type.rb
43
+ - lib/minting/money_attribute/version.rb
44
+ - lib/minting/rails.rb
45
+ - lib/minting/railties.rb
46
+ - lib/tasks/minting/rails_tasks.rake
47
+ homepage: https://github.com/gferraz/minting-rails
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ allowed_push_host: https://rubygems.org
52
+ homepage_uri: https://github.com/gferraz/minting-rails
53
+ source_code_uri: https://github.com/gferraz/minting-rails
54
+ changelog_uri: https://github.com/gferraz/minting-rails/releases
55
+ bug_tracker_uri: https://github.com/gferraz/minting-rails/issues
56
+ rubygems_mfa_required: 'true'
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '3.3'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.5.10
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Money attributes to ActiveRecord
76
+ test_files: []