roi_calculator 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1fc2392c4737370ff704dbddb3da2cdeb70a242645cef016dd3850eed816e476
4
- data.tar.gz: d14f14694ae62288b6386c361a3b1c1cccc1b30cf8f8e9700da666712bec0a85
3
+ metadata.gz: fc63825e0b9a50e16646d8a1b918ade35e0a507ae2b5da79a53d0b9759acd010
4
+ data.tar.gz: 8e2cde87e2205d49f98f372c07797bbc2764699c9f77f5e07c8c44daac7178c1
5
5
  SHA512:
6
- metadata.gz: 9611b5bd0bbcb170314b02b79cc315beeb5d2d38661d1722c30d4c941096b128c14c6ddba4a54615ad35bad9427c930e171073eddc0cc10965ee3038dfe8826e
7
- data.tar.gz: 7060394e2cf7022ac6aebcf8d66405c25f5a6854d53d5e6c31ced1246bbbfdc092956229a2a4d50d506f7428f2cf8f77782ee6922bd08d06ea3f83ef7e877afc
6
+ metadata.gz: edba4d7742f9cc1e5903a6d4d141e4be39a5cf9c21aa888e041cfa49953adf41819e41cb6b96785362602cfc48b8f3c9bceaf0b74334bfe8492e3f39dd7b1c11
7
+ data.tar.gz: e09f232462b96ee5b2fc964447210322551b8c5b34959dfdbb7468cc4a7beb5a73c68fb8372b56e19961eda3c577cddea7db74dcfe0d6f563bc5fcf5b2927112
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.2.5
1
+ ruby-2.5.0
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roi_calculator (1.0.0)
4
+ roi_calculator (2.0.0)
5
5
  math_util (~> 0.1.0)
6
6
  memoist (~> 0.15)
7
- work_day (~> 0.3.0)
7
+ work_day (~> 1.0.0)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -21,23 +21,23 @@ GEM
21
21
  math_util (0.1.0)
22
22
  memoist (0.16.0)
23
23
  minitest (5.11.3)
24
- rspec (3.5.0)
25
- rspec-core (~> 3.5.0)
26
- rspec-expectations (~> 3.5.0)
27
- rspec-mocks (~> 3.5.0)
28
- rspec-core (3.5.4)
29
- rspec-support (~> 3.5.0)
30
- rspec-expectations (3.5.0)
24
+ rspec (3.7.0)
25
+ rspec-core (~> 3.7.0)
26
+ rspec-expectations (~> 3.7.0)
27
+ rspec-mocks (~> 3.7.0)
28
+ rspec-core (3.7.1)
29
+ rspec-support (~> 3.7.0)
30
+ rspec-expectations (3.7.0)
31
31
  diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.5.0)
33
- rspec-mocks (3.5.0)
32
+ rspec-support (~> 3.7.0)
33
+ rspec-mocks (3.7.0)
34
34
  diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.5.0)
36
- rspec-support (3.5.0)
35
+ rspec-support (~> 3.7.0)
36
+ rspec-support (3.7.1)
37
37
  thread_safe (0.3.6)
38
38
  tzinfo (1.2.5)
39
39
  thread_safe (~> 0.1)
40
- work_day (0.3.1)
40
+ work_day (1.0.0)
41
41
  activesupport (>= 4.0)
42
42
 
43
43
  PLATFORMS
@@ -1,22 +1,20 @@
1
- # This is a sample build configuration for Ruby.
2
- # Check our guides at https://confluence.atlassian.com/x/8r-5Mw for more examples.
3
- # Only use spaces to indent your .yml configuration.
4
- # -----
5
- # You can specify a custom docker image from Docker Hub as your build environment.
6
1
  image: ruby:2.5.0
7
2
 
8
3
  pipelines:
9
4
  default:
10
5
  - step:
11
- caches:
12
- - bundler
13
- script: # Modify the commands below to build your repository.
14
- - gem install rubocop
6
+ script:
7
+ - git archive --remote=git@bitbucket.org:guideinvestimentos/rails_defaults.git HEAD .rubocop.yml | tar -x
15
8
  - bundle install
16
- - rspec
17
- - rubocop
18
- -
9
+ - gem install rubocop
10
+ - rspec -fdoc
11
+ - rubocop .
19
12
 
20
- definitions:
21
- caches:
22
- bundler: ./vendor
13
+ branches:
14
+ master:
15
+ - step:
16
+ script:
17
+ - gem build roi_calculator.gemspec
18
+ - curl -u $RUBYGEMS_USERNAME:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
19
+ - chmod 0600 ~/.gem/credentials
20
+ - gem push $(ls *.gem)
data/lib/roi.rb CHANGED
@@ -1,7 +1,9 @@
1
- require "roi/rentability"
2
- require "roi/rentability_line"
3
- require "roi/rentability_periods"
4
- require "roi/financial_result"
1
+ # frozen_string_literal: true
2
+
3
+ require 'roi/rentability'
4
+ require 'roi/rentability_line'
5
+ require 'roi/rentability_periods'
6
+ require 'roi/financial_result'
5
7
 
6
8
  module ROI
7
9
  end
@@ -1,6 +1,8 @@
1
- require "work_day"
2
- require "active_support"
3
- require "active_support/core_ext/date"
1
+ # frozen_string_literal: true
2
+
3
+ require 'work_day'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/date'
4
6
 
5
7
  module ROI
6
8
  class FinancialResult
@@ -38,6 +40,7 @@ module ROI
38
40
  end
39
41
 
40
42
  private
43
+
41
44
  def scoped_rentabilities(scope_start, scope_end)
42
45
  rentabilities.values.select.each do |line|
43
46
  line.date.between?(scope_start, scope_end)
@@ -53,12 +56,11 @@ module ROI
53
56
  end
54
57
 
55
58
  def check_gaps_and_format(scope_start, scope_end)
56
- if scope_start >= start_date
57
- scoped_rentabilities(scope_start, scope_end).each do |line|
58
- return if line.try(:have_position) == false
59
- end
60
- format_financial(scope_start, scope_end)
59
+ return unless scope_start >= start_date
60
+ scoped_rentabilities(scope_start, scope_end).each do |line|
61
+ return nil if line.try(:have_position) == false
61
62
  end
63
+ format_financial(scope_start, scope_end)
62
64
  end
63
65
  end
64
66
  end
@@ -1,9 +1,11 @@
1
- require "memoist"
2
- require "roi/financial_result"
3
- require "roi/rentability_periods"
4
- require "math_util"
5
- require "active_support"
6
- require "active_support/core_ext"
1
+ # frozen_string_literal: true
2
+
3
+ require 'memoist'
4
+ require 'roi/financial_result'
5
+ require 'roi/rentability_periods'
6
+ require 'math_util'
7
+ require 'active_support'
8
+ require 'active_support/core_ext'
7
9
 
8
10
  module ROI
9
11
  class Rentability
@@ -20,14 +22,14 @@ module ROI
20
22
  post_initialize(options)
21
23
  end
22
24
 
23
- def post_initialize(options)
24
- end
25
+ def post_initialize(options); end
25
26
 
26
27
  def calculate
27
28
  parse_rentabilities
28
- if @rentabilities.count > 0
29
+ if @rentabilities.count.positive?
29
30
  dates = @rentabilities.keys
30
- @start_date, @end_date = [dates[0], dates[-1]]
31
+ @start_date = dates[0]
32
+ @end_date = dates[-1]
31
33
  end
32
34
  self
33
35
  end
@@ -51,9 +53,10 @@ module ROI
51
53
  end
52
54
 
53
55
  def volatility(months_ago = nil)
54
- _start = months_ago ?
55
- (end_date - months_ago.months + 1.month).beginning_of_month : nil
56
- (volatility_in_window(nil, _start).first || [])[1]
56
+ if months_ago
57
+ start = (end_date - months_ago.months + 1.month).beginning_of_month
58
+ end
59
+ (volatility_in_window(nil, start).first || [])[1]
57
60
  end
58
61
 
59
62
  def volatility_in_window(days = nil, start_date = nil)
@@ -62,23 +65,23 @@ module ROI
62
65
 
63
66
  0.upto(filtered.size - days).map do |low|
64
67
  series = filtered.slice(low, days + 1)
65
- vol = MathUtil.volatility(series)
66
- date = dates_with_position.fetch(low + days) { dates_with_position.last }
67
- [date, vol] if date
68
+ date =
69
+ dates_with_position.fetch(low + days) { dates_with_position.last }
70
+ [date, MathUtil.volatility(series)] if date
68
71
  end
69
72
  end
70
73
 
71
74
  def to_a
72
75
  dates_with_position.map do |date|
73
76
  if @rentabilities[date]
74
- [date.strftime("%Y-%m-%d"), @rentabilities[date].share - 1]
77
+ [date.strftime('%Y-%m-%d'), @rentabilities[date].quota - 1]
75
78
  end
76
79
  end.compact
77
80
  end
78
81
 
79
82
  protected
80
- def before_parse_rentabilities
81
- end
83
+
84
+ def before_parse_rentabilities; end
82
85
 
83
86
  def work_days
84
87
  start_date && end_date ? WorkDay.between(start_date, end_date) : []
@@ -86,6 +89,7 @@ module ROI
86
89
  memoize :work_days
87
90
 
88
91
  private
92
+
89
93
  def parse_rentabilities
90
94
  @rentabilities = {}
91
95
  before_parse_rentabilities
@@ -99,10 +103,10 @@ module ROI
99
103
  last_value = rentabilities_source(last_date)
100
104
  return rentabilities unless this_value && last_value
101
105
 
102
- last_share = rentabilities[last_date].try(:share) || 1
106
+ last_quota = rentabilities[last_date].try(:quota) || 1
103
107
  rentability = (this_value / last_value - 1).round(8)
104
- new_share = (last_share * (1 + rentability)).round(8)
105
- line = RentabilityLine.new(this_date, rentability, nil, new_share, true)
108
+ new_quota = (last_quota * (1 + rentability)).round(8)
109
+ line = RentabilityLine.new(this_date, rentability, new_quota, true)
106
110
  after_parse(this_date, last_date, rentabilities, line)
107
111
  end
108
112
  end
@@ -1,21 +1,14 @@
1
- module ROI
2
- class RentabilityLine < Struct.new(
3
- :date, :rentability, :daily_result, :share, :have_position, :total_balance)
1
+ # frozen_string_literal: true
4
2
 
5
- def self.build(date: nil, share: 1, have_position: false)
6
- self.new(date, nil, nil, share, have_position, nil)
7
- end
3
+ module ROI
4
+ class RentabilityLine
5
+ attr_reader :date, :rentability, :daily_result, :quota, :have_position
8
6
 
9
- def self.create(date, assets, last_balance, last_share)
10
- line = build(date: date, share: last_share || 1)
11
- line.total_balance = assets.map(&:value).inject(:+)
12
- if last_balance.to_f > 0
13
- line.daily_result = assets.map(&:daily_result).inject(:+)
14
- line.rentability = line.daily_result / last_balance
15
- line.share = (last_share * (1 + line.rentability)).round(8)
16
- line.have_position = true
17
- end
18
- line
7
+ def initialize(date, rentability, quota, have_position)
8
+ @date = date
9
+ @rentability = rentability
10
+ @quota = quota
11
+ @have_position = have_position
19
12
  end
20
13
  end
21
14
  end
@@ -1,4 +1,6 @@
1
- require "work_day"
1
+ # frozen_string_literal: true
2
+
3
+ require 'work_day'
2
4
 
3
5
  module ROI
4
6
  module RentabilityPeriods
@@ -34,29 +36,35 @@ module ROI
34
36
  end
35
37
 
36
38
  private
39
+
37
40
  def adjust_start_date(date)
38
41
  [start_date, date].compact.max
39
42
  end
40
43
 
41
44
  def adjust_end_date(date)
42
- WorkDay.last_until(date.strftime("%Y-%m") == end_date.strftime("%Y-%m") ?
43
- end_date : date.end_of_month)
45
+ scoped_end =
46
+ if date.strftime('%Y-%m') == end_date.strftime('%Y-%m')
47
+ end_date
48
+ else
49
+ date.end_of_month
50
+ end
51
+ WorkDay.last_until(scoped_end)
44
52
  end
45
53
 
46
54
  def format(start_date, end_date)
47
55
  scoped_start, scoped_end = rentabilities.values_at(start_date, end_date)
48
56
  if scoped_start && scoped_end
49
- ((scoped_end.share / scoped_start.share - 1) * 100).round(8)
57
+ ((scoped_end.quota / scoped_start.quota - 1) * 100).round(8)
50
58
  end
51
59
  end
52
60
 
53
61
  def check_gaps_and_format(scoped_start, scoped_end)
54
- _start, _end = rentabilities.values_at(scoped_start, scoped_end)
55
- return unless _start && _end
62
+ local_start, local_end = rentabilities.values_at(scoped_start, scoped_end)
63
+ return unless local_start && local_end
56
64
  if skip_on_gap
57
65
  rentabilities.each_value do |line|
58
- return if line.date.between?(_start.date, _end.date) &&
59
- !line.have_position
66
+ return nil if line.date.between?(local_start.date, local_end.date) &&
67
+ !line.have_position
60
68
  end
61
69
  end
62
70
  format(scoped_start, scoped_end)
data/lib/roi/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ROI
2
- VERSION = "1.0.0"
4
+ VERSION = '2.0.0'
3
5
  end
@@ -1,25 +1,27 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('lib', __dir__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require 'roi/version'
5
7
 
6
8
  Gem::Specification.new do |spec|
7
- spec.name = "roi_calculator"
9
+ spec.name = 'roi_calculator'
8
10
  spec.version = ROI::VERSION
9
- spec.authors = ["Adriano Bacha"]
10
- spec.email = ["abacha@gmail.com"]
11
+ spec.authors = ['Adriano Bacha']
12
+ spec.email = ['abacha@gmail.com']
11
13
 
12
- spec.summary = %q{Rentability and financial calculations}
13
- spec.license = "MIT"
14
+ spec.summary = 'Rentability and financial calculations'
15
+ spec.license = 'MIT'
14
16
 
15
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
18
  f.match(%r{^(test|spec|features)/})
17
19
  end
18
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
19
21
 
20
- spec.add_development_dependency "bundler", "~> 1.13"
21
- spec.add_development_dependency "rspec", "~> 3.0"
22
- spec.add_runtime_dependency "memoist", "~> 0.15"
23
- spec.add_runtime_dependency "math_util", "~> 0.1.0"
24
- spec.add_runtime_dependency "work_day", "~> 0.3.0"
22
+ spec.add_development_dependency 'bundler', '~> 1.13'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_runtime_dependency 'math_util', '~> 0.1.0'
25
+ spec.add_runtime_dependency 'memoist', '~> 0.15'
26
+ spec.add_runtime_dependency 'work_day', '~> 1.0.0'
25
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roi_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adriano Bacha
@@ -39,47 +39,47 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: memoist
42
+ name: math_util
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.15'
47
+ version: 0.1.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.15'
54
+ version: 0.1.0
55
55
  - !ruby/object:Gem::Dependency
56
- name: math_util
56
+ name: memoist
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.1.0
61
+ version: '0.15'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.1.0
68
+ version: '0.15'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: work_day
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.3.0
75
+ version: 1.0.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.3.0
82
+ version: 1.0.0
83
83
  description:
84
84
  email:
85
85
  - abacha@gmail.com
@@ -89,7 +89,6 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
91
  - ".rspec"
92
- - ".ruby-gemset"
93
92
  - ".ruby-version"
94
93
  - ".travis.yml"
95
94
  - Gemfile
@@ -97,7 +96,6 @@ files:
97
96
  - LICENSE.txt
98
97
  - README.md
99
98
  - bitbucket-pipelines.yml
100
- - circle.yml
101
99
  - lib/roi.rb
102
100
  - lib/roi/financial_result.rb
103
101
  - lib/roi/rentability.rb
data/.ruby-gemset DELETED
@@ -1 +0,0 @@
1
- bigbang_services
data/circle.yml DELETED
@@ -1,3 +0,0 @@
1
- dependencies:
2
- pre:
3
- - gem install bundler