smerp-common 0.1.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: 1c8ba3a20a3dc040be402897ca0ecc74f140388ceb426ca6c1ea85123df40fc3
4
+ data.tar.gz: 153946e9f44cefdd2f1904dfea72bdda168bc17a1736313b6c2c9f5aa43e2b93
5
+ SHA512:
6
+ metadata.gz: 9ba1252b8545157d8018406d8b6524087412dd1c415af08c330f3d5595461dd83b8c1892f6d361df1d7142e75cbd081307e57103d76b579c19d623823d22c90e
7
+ data.tar.gz: f39883f0b8c80587da4bf0fb8fbe590e3a99727063d673ed672882de218f5faad39fa42dde00dde92f9e05578dffe1a2cc0bf60c090d5028a4211d58ac038d33
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in smerp-common.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ smerp-common (0.1.0)
5
+ teLogger
6
+ toolrack
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ base58 (0.2.3)
12
+ devops_assist (0.3.0)
13
+ git_cli
14
+ git_cli_prompt
15
+ gvcs
16
+ teLogger
17
+ toolrack
18
+ tty-prompt
19
+ diff-lcs (1.5.0)
20
+ git_cli (0.10.0)
21
+ gvcs
22
+ ptools (~> 1.4.0)
23
+ teLogger
24
+ toolrack
25
+ git_cli_prompt (0.3.1)
26
+ teLogger
27
+ toolrack
28
+ tty-prompt
29
+ gvcs (0.1.0)
30
+ pastel (0.8.0)
31
+ tty-color (~> 0.5)
32
+ ptools (1.4.2)
33
+ rake (13.0.6)
34
+ rspec (3.11.0)
35
+ rspec-core (~> 3.11.0)
36
+ rspec-expectations (~> 3.11.0)
37
+ rspec-mocks (~> 3.11.0)
38
+ rspec-core (3.11.0)
39
+ rspec-support (~> 3.11.0)
40
+ rspec-expectations (3.11.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.11.0)
43
+ rspec-mocks (3.11.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.11.0)
46
+ rspec-support (3.11.0)
47
+ teLogger (0.2.0)
48
+ toolrack (0.19.1)
49
+ base58
50
+ tty-color (0.6.0)
51
+ tty-cursor (0.7.1)
52
+ tty-prompt (0.23.1)
53
+ pastel (~> 0.8)
54
+ tty-reader (~> 0.8)
55
+ tty-reader (0.9.0)
56
+ tty-cursor (~> 0.7)
57
+ tty-screen (~> 0.8)
58
+ wisper (~> 2.0)
59
+ tty-screen (0.8.1)
60
+ wisper (2.0.1)
61
+
62
+ PLATFORMS
63
+ x86_64-linux
64
+
65
+ DEPENDENCIES
66
+ devops_assist
67
+ rake (~> 13.0)
68
+ rspec (~> 3.0)
69
+ smerp-common!
70
+
71
+ BUNDLED WITH
72
+ 2.2.28
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Smerp::Common
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/smerp/common`. 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 'smerp-common'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install smerp-common
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 the created tag, 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]/smerp-common.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ require 'devops_assist'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "smerp/common"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,125 @@
1
+
2
+
3
+ module Smerp
4
+ module Common
5
+ module CurrencyConversion
6
+
7
+ class CurrencyConversionError < StandardError; end
8
+
9
+ class ConversionContext
10
+ include TR::CondUtils
11
+
12
+ attr_accessor :from_currency, :to_currency
13
+
14
+ def initialize(value)
15
+ @value = value
16
+ end
17
+
18
+ def from(bc)
19
+ @from_currency = bc
20
+ self
21
+ end
22
+
23
+ def to(tc)
24
+ @to_currency = tc
25
+
26
+ @from_currency = CConverter.instance.home_currency if is_empty?(@from_currency)
27
+
28
+ if CConverter.instance.is_home_currency?(@from_currency)
29
+ rate = CConverter.instance.rate(@to_currency)
30
+ @value * rate
31
+ elsif CConverter.instance.is_home_currency?(@to_currency)
32
+ rate = CConverter.instance.rate(@from_currency)
33
+ @value / rate
34
+ else
35
+
36
+ raise CurrencyConversionError, "From currency '#{@from_currency}' rate is not defined" if not CConverter.instance.is_rate_defined?(@from_currency)
37
+ raise CurrencyConversionError, "To currency '#{@to_currency}' rate is not defined" if not CConverter.instance.is_rate_defined?(@to_currency)
38
+
39
+ # both from and to is not home currency
40
+ # use local currency as intermediary
41
+ hc = CConverter.instance.home_currency
42
+ fcc = ConversionContext.new(@value)
43
+ mv = fcc.from(@from_currency).to(hc)
44
+
45
+ tcc = ConversionContext.new(mv)
46
+ tcc.from(hc).to(@to_currency)
47
+
48
+ end
49
+
50
+ end
51
+ end
52
+
53
+ class CConverter
54
+ include Singleton
55
+ include TR::CondUtils
56
+
57
+ attr_accessor :home_currency
58
+
59
+ def conversion=(val)
60
+ raise CurrencyConversionError, "Home currency must set first" if is_empty?(@home_currency)
61
+ raise CurrencyConversionError, "Hash is expected" if not val.is_a?(Hash)
62
+
63
+ val.each do |k,v|
64
+ v = 1 if is_empty?(v)
65
+ rate_table[k] = v.to_f
66
+ end
67
+ end
68
+
69
+ def rate(currency)
70
+ rate = rate_table[currency]
71
+ rate = 1.0 if is_empty?(rate)
72
+ rate
73
+ end
74
+
75
+ def is_rate_defined?(currency)
76
+ not_empty?(rate_table[currency])
77
+ end
78
+
79
+ def set_rate_table(hash)
80
+ @rate = hash if not hash.nil?
81
+ end
82
+
83
+ def is_home_currency?(sym)
84
+ @home_currency == sym
85
+ end
86
+
87
+ private
88
+ def rate_table
89
+ if @rate.nil?
90
+ @rate = {}
91
+ end
92
+ @rate
93
+ end
94
+
95
+ end # Cconverter
96
+
97
+ module CurrencyUtils
98
+
99
+ def cconvert
100
+ ConversionContext.new(self.to_f)
101
+ end
102
+
103
+ def to_other_currency(country)
104
+ self * CurrencyConversion::CConverter.instance.rate(country)
105
+ end
106
+
107
+ def to_home_currency(country)
108
+ self / CurrencyConversion::CConverter.instance.rate(country)
109
+ end
110
+
111
+
112
+ end
113
+
114
+ end
115
+ end
116
+ end
117
+
118
+ class Integer
119
+ include Smerp::Common::CurrencyConversion::CurrencyUtils
120
+ end
121
+
122
+ class Float
123
+ include Smerp::Common::CurrencyConversion::CurrencyUtils
124
+ end
125
+
@@ -0,0 +1,253 @@
1
+
2
+
3
+ module Smerp
4
+ module Common
5
+
6
+ module FinUtils
7
+
8
+ ##
9
+ # Number Representation
10
+ # A integer/decimal/bigdecimal can be a raw value or a percentage value
11
+ #
12
+ module NumberRep
13
+ attr_accessor :type, :value
14
+ end
15
+ class RawValue
16
+ include NumberRep
17
+
18
+ def initialize(value, type)
19
+ @value = value
20
+ @type = type
21
+ end
22
+
23
+ def is_percent?
24
+ false
25
+ end
26
+ alias_method :is_percentage?, :is_percent?
27
+
28
+ def is_value?
29
+ true
30
+ end
31
+
32
+ def rep_value
33
+ case @type
34
+ when :int
35
+ @value.to_i
36
+ when :float, :bigDec
37
+ @value.to_f
38
+ else
39
+ @value
40
+ end
41
+ end
42
+ end
43
+
44
+ class Percent
45
+ include NumberRep
46
+ def initialize(value)
47
+ @type = :percent
48
+ @value = value
49
+ end
50
+
51
+ def is_percent?
52
+ true
53
+ end
54
+ alias_method :is_percentage?, :is_percent?
55
+
56
+ def is_value?
57
+ false
58
+ end
59
+
60
+ def rep_value
61
+ @value / 100.0
62
+ end
63
+ end
64
+ ##
65
+ # End Number Representation
66
+ ##
67
+
68
+ ##
69
+ # Start mixin methods for included class
70
+ # Designed to be included into class
71
+ # - Integer
72
+ # - Float
73
+ # - BigDecimal
74
+ #
75
+ def percentage_equals?(percent)
76
+ if is_empty?(percent)
77
+ false
78
+ else
79
+ if percent.is_percent?
80
+ self.send(:percent).rep_value == percent.rep_value
81
+ else
82
+ false
83
+ end
84
+ end
85
+ end
86
+
87
+ def percent
88
+ Percent.new(self.to_f)
89
+ end
90
+
91
+ def value
92
+ case self
93
+ when Integer
94
+ RawValue.new(self.to_f, :int)
95
+ when Float
96
+ RawValue.new(self.to_f, :float)
97
+ when BigDecimal
98
+ RawValue.new(self.to_f, :bigDec)
99
+ else
100
+ raise FinUtilError, "Unsupported type '#{self.class}'"
101
+ end
102
+ end
103
+
104
+ def percent_of(val, rounding = 0)
105
+ vv = self.to_f / val.to_f * 100.0
106
+ if rounding > 0
107
+ vv.round(rounding)
108
+ else
109
+ vv
110
+ end
111
+ end
112
+
113
+ ## End mixin methods
114
+
115
+
116
+ #
117
+ # Markup is % from cost
118
+ #
119
+ module Markup
120
+ include TR::CondUtils
121
+
122
+ def markup(percentage)
123
+ val = self.to_f
124
+
125
+ case percentage
126
+ when NumberRep
127
+ if percentage.is_percentage?
128
+ res = val * (1+percentage.rep_value)
129
+ elsif percentage.is_value?
130
+ res = val + percentage.rep_value
131
+ else
132
+ res = val + percentage.to_i
133
+ end
134
+ else
135
+ res = val + percentage.to_i
136
+ end
137
+
138
+ res
139
+ end
140
+
141
+ def markup_percentage_from_base(cost)
142
+ cost = cost.to_f
143
+ price = self.to_f
144
+ per = ((price - cost) / cost) * 100.0
145
+ per
146
+ end
147
+
148
+ end # markup
149
+
150
+ #
151
+ # Margin is % from price
152
+ #
153
+ module Margin
154
+ include TR::CondUtils
155
+
156
+ def margin(percentage)
157
+ val = self.to_f
158
+
159
+ case percentage
160
+ when NumberRep
161
+ if percentage.is_percentage?
162
+ res = val/(1-percentage.rep_value)
163
+ elsif percentage.is_value?
164
+ res = val+percentage.rep_value
165
+ else
166
+ res = val + percentage.to_i
167
+ end
168
+ else
169
+ res = val + percentage.to_i
170
+ end
171
+
172
+ res
173
+ end
174
+
175
+ def margin_percentage_from_base(cost)
176
+ cost = cost.to_f
177
+ price = self.to_f
178
+ per = ((price - cost) / price) * 100.0
179
+ per
180
+ end
181
+
182
+ end # margin
183
+
184
+ ##
185
+ # Rounding info
186
+ ##
187
+ class RoundingInfo
188
+ attr_accessor :original_value, :rounding_param, :rounded_value
189
+ end
190
+ # make the rounding operation more humanize
191
+ module RoundingHelper
192
+
193
+ #include TeLogger::TeLogHelper
194
+ #teLogger_tag :rounding
195
+
196
+ def round_to_nearest(val = 0)
197
+
198
+ v = "#{val}"
199
+ if v.include?(".")
200
+ rv = v.split(".")[1].length
201
+ else
202
+ rv = (v.length-1)*-1
203
+ end
204
+
205
+ res = RoundingInfo.new
206
+ res.original_value = self.to_f
207
+ res.rounding_param = val
208
+ res.rounded_value = self.to_f.round(rv)
209
+
210
+ # rounding in financial is not the same as mathematical rounding
211
+ if rv < 0 and res.rounded_value > 0 and res.rounded_value < res.original_value
212
+
213
+ # adjusted base value
214
+ abv = "1#{"0"*(rv*-1)}"
215
+ # - means left of .
216
+ # - - become +
217
+ vv = res.original_value + abv.to_f
218
+ res.rounded_value = vv.to_f.round(rv)
219
+ end
220
+
221
+ res
222
+ end
223
+
224
+ end # Rounding Helper
225
+
226
+ ## End Rounding Info
227
+
228
+ end
229
+
230
+ end
231
+ end
232
+
233
+ class Integer
234
+ include Smerp::Common::FinUtils
235
+ include Smerp::Common::FinUtils::Markup
236
+ include Smerp::Common::FinUtils::Margin
237
+ include Smerp::Common::FinUtils::RoundingHelper
238
+ end
239
+
240
+ class Float
241
+ include Smerp::Common::FinUtils
242
+ include Smerp::Common::FinUtils::Markup
243
+ include Smerp::Common::FinUtils::Margin
244
+ include Smerp::Common::FinUtils::RoundingHelper
245
+ end
246
+
247
+ class BigDecimal
248
+ include Smerp::Common::FinUtils
249
+ include Smerp::Common::FinUtils::Markup
250
+ include Smerp::Common::FinUtils::Margin
251
+ include Smerp::Common::FinUtils::RoundingHelper
252
+ end
253
+
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smerp
4
+ module Common
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'teLogger'
4
+ require 'toolrack'
5
+
6
+ require_relative "common/version"
7
+
8
+ require_relative 'common/fin_utils'
9
+ require_relative 'common/currency_conversion'
10
+
11
+ module Smerp
12
+ module Common
13
+ class Error < StandardError; end
14
+ # Your code goes here...
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/smerp/common/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "smerp-common"
7
+ spec.version = Smerp::Common::VERSION
8
+ spec.authors = ["Ian"]
9
+ spec.email = ["cameronian0@protonmail.com"]
10
+
11
+ spec.summary = ""
12
+ spec.description = ""
13
+ spec.homepage = ""
14
+ spec.required_ruby_version = ">= 2.4.0"
15
+
16
+ #spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
+
18
+ #spec.metadata["homepage_uri"] = spec.homepage
19
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
20
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency 'teLogger'
34
+ spec.add_dependency 'toolrack'
35
+
36
+ spec.add_development_dependency "devops_assist"
37
+
38
+ # Uncomment to register a new dependency of your gem
39
+ # spec.add_dependency "example-gem", "~> 1.0"
40
+
41
+ # For more information and examples about making a new gem, checkout our
42
+ # guide at: https://bundler.io/guides/creating_gem.html
43
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smerp-common
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: teLogger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: toolrack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: devops_assist
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email:
57
+ - cameronian0@protonmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/smerp/common.rb
70
+ - lib/smerp/common/currency_conversion.rb
71
+ - lib/smerp/common/fin_utils.rb
72
+ - lib/smerp/common/version.rb
73
+ - smerp-common.gemspec
74
+ homepage: ''
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.4.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.2.22
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: ''
96
+ test_files: []