Skip to content

Commit 73b84b5

Browse files
authored
Merge pull request #1054 from DalenW/rbs
RBS
2 parents 3a45a68 + 20d54f8 commit 73b84b5

File tree

18 files changed

+1956
-0
lines changed

18 files changed

+1956
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ doc/
2121

2222
# Spec artifacts
2323
/coverage
24+
.gem_rbs_collection
25+
.idea

lib/money/money/allocation.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class Allocation
77
#
88
# The results should always add up to the original amount.
99
#
10+
# The parts can be specified as:
11+
# Numeric — performs the split between a given number of parties evenly
12+
# Array<Numeric> — allocates the amounts proportionally to the given array
13+
#
1014
# @param amount [Numeric] The total amount to be allocated.
1115
# @param parts [Numeric, Array<Numeric>] Number of parts to split into or an array (proportions for allocation)
1216
# @param whole_amounts [Boolean] Specifies whether to allocate whole amounts only. Defaults to true.

money.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Gem::Specification.new do |s|
2222
s.add_development_dependency "rspec", "~> 3.4"
2323
s.add_development_dependency "yard", "~> 0.9.11"
2424
s.add_development_dependency "kramdown", "~> 2.3"
25+
s.add_development_dependency "rbs" if RUBY_VERSION >= "2.7.0"
26+
s.add_development_dependency "typeprof" if RUBY_VERSION >= "2.7.0"
2527

2628
s.required_ruby_version = '>= 3.1'
2729

rbs_collection.lock.yaml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
sources:
3+
- type: git
4+
name: ruby/gem_rbs_collection
5+
revision: d0d7aeed98fa74427b30b336fc402ea86d526f35
6+
remote: https://github.com/ruby/gem_rbs_collection.git
7+
repo_dir: gems
8+
path: ".gem_rbs_collection"
9+
gems:
10+
- name: cgi
11+
version: '0'
12+
source:
13+
type: stdlib
14+
- name: concurrent-ruby
15+
version: '1.1'
16+
source:
17+
type: git
18+
name: ruby/gem_rbs_collection
19+
revision: d0d7aeed98fa74427b30b336fc402ea86d526f35
20+
remote: https://github.com/ruby/gem_rbs_collection.git
21+
repo_dir: gems
22+
- name: i18n
23+
version: '1.10'
24+
source:
25+
type: git
26+
name: ruby/gem_rbs_collection
27+
revision: d0d7aeed98fa74427b30b336fc402ea86d526f35
28+
remote: https://github.com/ruby/gem_rbs_collection.git
29+
repo_dir: gems
30+
- name: logger
31+
version: '0'
32+
source:
33+
type: stdlib
34+
- name: monitor
35+
version: '0'
36+
source:
37+
type: stdlib
38+
- name: optparse
39+
version: '0'
40+
source:
41+
type: stdlib
42+
- name: tempfile
43+
version: '0'
44+
source:
45+
type: stdlib
46+
- name: uri
47+
version: '0'
48+
source:
49+
type: stdlib
50+
- name: yard
51+
version: '0.9'
52+
source:
53+
type: git
54+
name: ruby/gem_rbs_collection
55+
revision: d0d7aeed98fa74427b30b336fc402ea86d526f35
56+
remote: https://github.com/ruby/gem_rbs_collection.git
57+
repo_dir: gems
58+
gemfile_lock_path: Gemfile.lock

rbs_collection.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Download sources
2+
sources:
3+
- type: git
4+
name: ruby/gem_rbs_collection
5+
remote: https://github.com/ruby/gem_rbs_collection.git
6+
revision: main
7+
repo_dir: gems
8+
9+
# You can specify local directories as sources also.
10+
# - type: local
11+
# path: path/to/your/local/repository
12+
13+
# A directory to install the downloaded RBSs
14+
path: .gem_rbs_collection
15+
16+
gems:
17+
# Skip loading rbs gem's RBS.
18+
# It's unnecessary if you don't use rbs as a library.
19+
- name: rbs
20+
ignore: true

sig/lib/money/bank/base.rbs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
class Money
2+
# Provides classes that aid in the ability of exchange one currency with
3+
# another.
4+
module Bank
5+
# The lowest Money::Bank error class.
6+
# All Money::Bank errors should inherit from it.
7+
class Error < StandardError
8+
end
9+
10+
# Raised when the bank doesn't know about the conversion rate
11+
# for specified currencies.
12+
class UnknownRate < Error
13+
end
14+
15+
# Money::Bank::Base is the basic interface for creating a money exchange
16+
# object, also called Bank.
17+
#
18+
# A Bank is responsible for storing exchange rates, take a Money object as
19+
# input and returns the corresponding Money object converted into an other
20+
# currency.
21+
#
22+
# This class exists for aiding in the creating of other classes to exchange
23+
# money between different currencies. When creating a subclass you will
24+
# need to implement the following methods to exchange money between
25+
# currencies:
26+
#
27+
# - #exchange_with(Money) #=> Money
28+
#
29+
# See Money::Bank::VariableExchange for a real example.
30+
#
31+
# Also, you can extend +Money::Bank::VariableExchange+ instead of
32+
# +Money::Bank::Base+ if your bank implementation needs to store rates
33+
# internally.
34+
#
35+
# @abstract Subclass and override +#exchange_with+ to implement a custom
36+
# +Money::Bank+ class. You can also override +#setup+ instead of
37+
# +#initialize+ to setup initial variables, etc.
38+
class Base
39+
@singleton: Money::Bank::Base
40+
41+
# Returns the singleton instance of the Base bank.
42+
#
43+
# @return [Money::Bank::Base]
44+
def self.instance: () -> Money::Bank::Base
45+
46+
# The rounding method to use when exchanging rates.
47+
#
48+
# @return [Proc]
49+
attr_reader rounding_method: untyped
50+
51+
# Initializes a new +Money::Bank::Base+ object. An optional block can be
52+
# passed to dictate the rounding method that +#exchange_with+ can use.
53+
#
54+
# @yield [n] Optional block to use when rounding after exchanging one
55+
# currency for another.
56+
# @yieldparam [Float] n The resulting float after exchanging one currency
57+
# for another.
58+
# @yieldreturn [Integer]
59+
#
60+
# @return [Money::Bank::Base]
61+
#
62+
# @example
63+
# Money::Bank::Base.new #=> #<Money::Bank::Base @rounding_method=nil>
64+
# Money::Bank::Base.new {|n|
65+
# n.floor
66+
# } #=> #<Money::Bank::Base @round_method=#<Proc>>
67+
def initialize: () { () -> untyped } -> void
68+
69+
# Called after initialize. Subclasses can use this method to setup
70+
# variables, etc that they normally would in +#initialize+.
71+
#
72+
# @abstract Subclass and override +#setup+ to implement a custom
73+
# +Money::Bank+ class.
74+
#
75+
# @return [self]
76+
def setup: () -> nil
77+
78+
# Exchanges the given +Money+ object to a new +Money+ object in
79+
# +to_currency+.
80+
#
81+
# @abstract Subclass and override +#exchange_with+ to implement a custom
82+
# +Money::Bank+ class.
83+
#
84+
# @raise NotImplementedError
85+
#
86+
# @param [Money] from The +Money+ object to exchange from.
87+
# @param [Money::Currency, String, Symbol] to_currency The currency
88+
# string or object to exchange to.
89+
# @yield [n] Optional block to use to round the result after making
90+
# the exchange.
91+
# @yieldparam [Float] n The result after exchanging from one currency to
92+
# the other.
93+
# @yieldreturn [Integer]
94+
#
95+
# @return [Money]
96+
def exchange_with: (Money from, (Money::Currency | string | Symbol) to_currency) { () -> int } -> Money
97+
98+
# Given two currency strings or object, checks whether they're both the
99+
# same currency. Return +true+ if the currencies are the same, +false+
100+
# otherwise.
101+
#
102+
# @param [Money::Currency, String, Symbol] currency1 The first currency
103+
# to compare.
104+
# @param [Money::Currency, String, Symbol] currency2 The second currency
105+
# to compare.
106+
#
107+
# @return [Boolean]
108+
#
109+
# @example
110+
# same_currency?("usd", "USD") #=> true
111+
# same_currency?("usd", "EUR") #=> false
112+
# same_currency?("usd", Currency.new("USD")) #=> true
113+
# same_currency?("usd", "USD") #=> true
114+
def same_currency?: ((Money::Currency | string | Symbol) currency1, (Money::Currency | string | Symbol) currency2) -> bool
115+
end
116+
end
117+
end

0 commit comments

Comments
 (0)