strict_money 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb4adcabdacb785a087ea8549695d625fe7df0ebdedda3746c83da04364ee8bc
4
+ data.tar.gz: f3cc4f0b6fd955857394a27f65eb75a8bd83a772c43a8414106b84576a68dad5
5
+ SHA512:
6
+ metadata.gz: 5f1314942fb80cea05d8f2fc2eed57cac80eb32b3bd760d4b4144a3a31f0e9a30b226b5e7130ea1b5ae6da2f8fbedaeea540b3fcf1dd7269d69e3482e5122d9a
7
+ data.tar.gz: 0e7f81ae2133c5b55fda150bbf056dd787d9e6bdbdd3fe42662c5da189834e69005bcef5ece5ed503c953f333305e78b5937ad81d587263b8505c3be613fb8de
@@ -0,0 +1,51 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.4.1-node-browsers
11
+ - image: circleci/mongo:3.6.9
12
+
13
+ working_directory: ~/repo
14
+
15
+ steps:
16
+ - checkout
17
+
18
+ - run:
19
+ name: Configure Bundler
20
+ command: |
21
+ echo 'export BUNDLER_VERSION=$(cat Gemfile.lock | tail -1 | tr -d " ")' >> $BASH_ENV
22
+ source $BASH_ENV
23
+ gem install bundler
24
+
25
+ - run:
26
+ name: install dependencies
27
+ command: |
28
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
29
+
30
+ # run tests!
31
+ - run:
32
+ name: run tests
33
+ command: |
34
+ mkdir /tmp/test-results
35
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
36
+ circleci tests split --split-by=timings)"
37
+
38
+ bundle exec rspec \
39
+ --format progress \
40
+ -r rspec_junit_formatter \
41
+ --format RspecJunitFormatter \
42
+ --out /tmp/test-results/rspec/results.xml \
43
+ --format progress \
44
+ $TEST_FILES
45
+
46
+ # collect reports
47
+ - store_test_results:
48
+ path: /tmp/test-results
49
+ - store_artifacts:
50
+ path: /tmp/test-results
51
+ destination: test-results
@@ -0,0 +1,38 @@
1
+ version: "2" # required to adjust maintainability checks
2
+ checks:
3
+ argument-count:
4
+ config:
5
+ threshold: 4
6
+ complex-logic:
7
+ config:
8
+ threshold: 4
9
+ file-lines:
10
+ config:
11
+ threshold: 250
12
+ method-complexity:
13
+ config:
14
+ threshold: 5
15
+ method-count:
16
+ config:
17
+ threshold: 20
18
+ method-lines:
19
+ config:
20
+ threshold: 25
21
+ nested-control-flow:
22
+ config:
23
+ threshold: 4
24
+ return-statements:
25
+ config:
26
+ threshold: 4
27
+ similar-code:
28
+ config:
29
+ threshold: 150
30
+ identical-code:
31
+ config:
32
+ threshold: 75
33
+ plugins:
34
+ reek:
35
+ enabled: true
36
+ rubocop:
37
+ enabled: true
38
+
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
@@ -0,0 +1,11 @@
1
+ ---
2
+ detectors:
3
+ DuplicateMethodCall:
4
+ max_calls: 3
5
+ MissingSafeMethod:
6
+ enabled: false
7
+ UtilityFunction:
8
+ public_methods_only: true
9
+
10
+ exclude_paths:
11
+ - spec
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,75 @@
1
+ Layout/ExtraSpacing:
2
+ Enabled: false
3
+
4
+ Layout/SpaceAroundOperators:
5
+ Enabled: false
6
+
7
+ Layout/SpaceInsideHashLiteralBraces:
8
+ Enabled: false
9
+
10
+ Layout/TrailingBlankLines:
11
+ Enabled: false
12
+
13
+ Lint/RescueException:
14
+ Enabled: false
15
+
16
+ Naming/VariableNumber:
17
+ Exclude:
18
+ - 'spec/**/*'
19
+
20
+ Metrics/BlockLength:
21
+ Exclude:
22
+ - 'spec/**/*'
23
+
24
+ Metrics/LineLength:
25
+ Max: 100
26
+ Exclude:
27
+ - 'strict_money.gemspec'
28
+
29
+ # Handled by Reek
30
+ Metrics/MethodLength:
31
+ Enabled: false
32
+
33
+ Naming/BinaryOperatorParameterName:
34
+ Enabled: false
35
+
36
+ Style/BlockDelimiters:
37
+ Enabled: false
38
+
39
+ Style/EmptyMethod:
40
+ Exclude:
41
+ - 'spec/**/*'
42
+
43
+ Style/ExpandPathArguments:
44
+ Enabled: false
45
+
46
+ Style/GuardClause:
47
+ Enabled: false
48
+
49
+ Style/IfUnlessModifier:
50
+ Enabled: false
51
+
52
+ Style/MethodMissing:
53
+ Enabled: false
54
+
55
+ Style/PercentLiteralDelimiters:
56
+ Enabled: false
57
+
58
+ Style/RandomWithOffset:
59
+ Enabled: false
60
+
61
+ Style/StringLiterals:
62
+ Enabled: false
63
+
64
+ Style/SymbolArray:
65
+ Enabled: false
66
+
67
+ Style/SymbolProc:
68
+ Exclude:
69
+ - 'spec/**/*'
70
+
71
+ Style/TrivialAccessors:
72
+ Enabled: false
73
+
74
+ Style/UnneededPercentQ:
75
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in strict_money.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ # StrictMoney
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/strict_money`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'strict_money'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install strict_money
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/strict_money.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "strict_money"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ require "strict_money/amount"
2
+ require "strict_money/configuration"
3
+ require "strict_money/version"
4
+ require "strict_money/railtie" if defined?(Rails::Railtie)
5
+ require "forwardable"
6
+
7
+ # Manipulate money and currency with strictness around currencies and time periods.
8
+ module StrictMoney
9
+ # Exception raised when someone uses a currency that is not supported within
10
+ # the application.
11
+ UnsupportedCurrencyError = Class.new(StandardError)
12
+ # Exception raised when someone uses an invalid currency in a method call.
13
+ WrongCurrencyError = Class.new(StandardError)
14
+
15
+ class << self
16
+ extend Forwardable
17
+
18
+ def_delegators :configuration, :supported_currency?, :supported_currencies
19
+
20
+ attr_reader :configuration
21
+
22
+ def configure
23
+ yield(configuration)
24
+ end
25
+
26
+ def reset_configuration
27
+ @configuration = Configuration.new
28
+ end
29
+ end
30
+ end
31
+
32
+ StrictMoney.reset_configuration
@@ -0,0 +1,72 @@
1
+ require 'forwardable'
2
+
3
+ module StrictMoney
4
+ # A value object to describe an single amount of money with a single
5
+ # currency.
6
+ class Amount
7
+ include Comparable
8
+ extend Forwardable
9
+
10
+ attr_reader :currency
11
+
12
+ def initialize(amount, currency)
13
+ raise IncompleteError unless amount && currency
14
+ raise StrictMoney::UnsupportedCurrencyError unless StrictMoney.supported_currency?(currency)
15
+ @amount = amount
16
+ @currency = currency.upcase
17
+ end
18
+
19
+ def -@
20
+ Amount.new(-@amount, @currency)
21
+ end
22
+
23
+ def <=>(other)
24
+ return unless other.is_a?(Amount) && currency == other.currency
25
+
26
+ as_float(currency) <=> other.as_float(currency)
27
+ end
28
+
29
+ def +(addend)
30
+ raise TypeError unless addend.is_a?(Amount)
31
+ Amount.new(as_float(currency) + addend.as_float(currency), currency)
32
+ end
33
+
34
+ def -(subtrahend)
35
+ raise TypeError unless subtrahend.is_a?(Amount)
36
+ self + (subtrahend * -1)
37
+ end
38
+
39
+ def *(multiplier)
40
+ Amount.new(@amount * multiplier, currency)
41
+ end
42
+
43
+ def /(divisor)
44
+ if divisor.is_a?(Amount)
45
+ @amount / divisor.as_float(@currency)
46
+ else
47
+ Amount.new(@amount / divisor, @currency)
48
+ end
49
+ end
50
+
51
+ def as_float(currency)
52
+ raise WrongCurrencyError unless currency.upcase == @currency
53
+ @amount.to_f
54
+ end
55
+
56
+ def negative?
57
+ as_float(@currency) < 0
58
+ end
59
+
60
+ def positive?
61
+ as_float(@currency) > 0
62
+ end
63
+
64
+ def round(precision)
65
+ Amount.new(@amount.round(precision), @currency)
66
+ end
67
+
68
+ # Exception raised when somebody tries to instantiate an Amount with
69
+ # incomplete information.
70
+ IncompleteError = Class.new(StandardError)
71
+ end
72
+ end
@@ -0,0 +1,18 @@
1
+ module StrictMoney
2
+ # Store configuration options for StrictMoney overall.
3
+ class Configuration
4
+ attr_reader :supported_currencies
5
+
6
+ def initialize
7
+ @supported_currencies = :all
8
+ end
9
+
10
+ def supported_currencies=(sc)
11
+ @supported_currencies = sc
12
+ end
13
+
14
+ def supported_currency?(currency)
15
+ (@supported_currencies == :all) || @supported_currencies.include?(currency)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ require "strict_money/orms/mongoid/amount"
2
+ require "strict_money/orms/mongoid/configuration"
3
+ require "strict_money/orms/mongoid/financiality_validator"
4
+
5
+ class << StrictMoney
6
+ def_delegators :configuration, :demongoize_amount
7
+ end
@@ -0,0 +1,20 @@
1
+ module StrictMoney
2
+ module Orms
3
+ module Mongoid
4
+ # Extend StrictMoney::Amount to work with Mongoid.
5
+ module Amount
6
+ def mongoize
7
+ {'currency' => @currency, 'amount' => @amount}
8
+ end
9
+
10
+ def self.included(base)
11
+ def base.demongoize(object)
12
+ StrictMoney.demongoize_amount(object)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ StrictMoney::Amount.include(StrictMoney::Orms::Mongoid::Amount)
@@ -0,0 +1,20 @@
1
+ module StrictMoney
2
+ module Orms
3
+ module Mongoid
4
+ # Extend StrictMoney::Configuration to work with Mongoid.
5
+ module Configuration
6
+ def demongoize_amount(object = nil, &block)
7
+ if block
8
+ @demongoize_block = block
9
+ elsif @demongoize_block
10
+ @demongoize_block.call(object)
11
+ else
12
+ StrictMoney::Amount.new(object['amount'], object['currency'])
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ StrictMoney::Configuration.include(StrictMoney::Orms::Mongoid::Configuration)
@@ -0,0 +1,26 @@
1
+ # Supply validation macros for Mongoid classes that embed StrictMoney::Amount.
2
+ class FinancialityValidator < ActiveModel::EachValidator
3
+ def check_validity!
4
+ invalid_keys = options.keys - [:positive, :not_negative]
5
+ raise "Invalid keys #{invalid_keys.inspect} for financiality validator" if invalid_keys.present?
6
+ end
7
+
8
+ def validate_each(*args)
9
+ validate_positive(*args) if options[:positive]
10
+ validate_not_negative(*args) if options[:not_negative]
11
+ end
12
+
13
+ private
14
+
15
+ def validate_not_negative(record, attribute, value)
16
+ if value.present? && value.negative?
17
+ record.errors.add(attribute, "can't be negative")
18
+ end
19
+ end
20
+
21
+ def validate_positive(record, attribute, value)
22
+ if value.present? && !value.positive?
23
+ record.errors.add(attribute, "must be greater than 0")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module StrictMoney
2
+ # Initialize StrictMoney inside a Rails application.
3
+ class Railtie < Rails::Railtie
4
+ initializer :strict_money do
5
+ Module.const_defined?(:Mongoid) && \
6
+ require("strict_money/orms/mongoid")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module StrictMoney
2
+ VERSION = "0.2.0".freeze
3
+ end
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "strict_money/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "strict_money"
7
+ spec.version = StrictMoney::VERSION
8
+ spec.authors = ["Francis Hwang"]
9
+ spec.email = ["sera@fhwang.net"]
10
+
11
+ spec.summary = %q{Manipulate money and currency with strictness around currencies and time periods.}
12
+ spec.homepage = "https://github.com/fhwang/strict_money"
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/fhwang/strict_money"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "mongoid"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "rspec_junit_formatter"
31
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strict_money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Francis Hwang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongoid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec_junit_formatter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - sera@fhwang.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".circleci/config.yml"
91
+ - ".codeclimate.yml"
92
+ - ".gitignore"
93
+ - ".reek.yml"
94
+ - ".rspec"
95
+ - ".rubocop.yml"
96
+ - ".travis.yml"
97
+ - Gemfile
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/strict_money.rb
103
+ - lib/strict_money/amount.rb
104
+ - lib/strict_money/configuration.rb
105
+ - lib/strict_money/orms/mongoid.rb
106
+ - lib/strict_money/orms/mongoid/amount.rb
107
+ - lib/strict_money/orms/mongoid/configuration.rb
108
+ - lib/strict_money/orms/mongoid/financiality_validator.rb
109
+ - lib/strict_money/railtie.rb
110
+ - lib/strict_money/version.rb
111
+ - strict_money.gemspec
112
+ homepage: https://github.com/fhwang/strict_money
113
+ licenses: []
114
+ metadata:
115
+ homepage_uri: https://github.com/fhwang/strict_money
116
+ source_code_uri: https://github.com/fhwang/strict_money
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.0.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Manipulate money and currency with strictness around currencies and time
136
+ periods.
137
+ test_files: []