business_date_calculator 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
+ SHA1:
3
+ metadata.gz: d79591779893189bd76540e43a52e10301dae6e2
4
+ data.tar.gz: 82528a4fe8d8aa6680fb54194b96a6103322698d
5
+ SHA512:
6
+ metadata.gz: 9a5dce015ce2d6de49dd463265854d29ee53a77f59906217e0bb06199e7a4282e462dade53159ffab40e9d9859d2d4a49b3c5940ac8c45931aa34faf3eeacc83
7
+ data.tar.gz: 863aed1994203a5513e72e8f752badefe93886506aa192b030b0593d598eb9b35b530462c43a25d81a3badf2cee88f686d776013ed3e84ab8ef2362051d1cad3
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in business_date_calculator.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,77 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec feature)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), the you will want to move the Guardfile
18
+ ## to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: "bundle exec rspec" do
36
+ require "guard/rspec/dsl"
37
+ dsl = Guard::RSpec::Dsl.new(self)
38
+
39
+ # Feel free to open issues for suggestions and improvements
40
+
41
+ # RSpec files
42
+ rspec = dsl.rspec
43
+ watch(rspec.spec_helper) { rspec.spec_dir }
44
+ watch(rspec.spec_support) { rspec.spec_dir }
45
+ watch(rspec.spec_files)
46
+
47
+ # Ruby files
48
+ ruby = dsl.ruby
49
+ dsl.watch_spec_files_for(ruby.lib_files)
50
+
51
+ # Rails files
52
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
53
+ dsl.watch_spec_files_for(rails.app_files)
54
+ dsl.watch_spec_files_for(rails.views)
55
+
56
+ watch(rails.controllers) do |m|
57
+ [
58
+ rspec.spec.("routing/#{m[1]}_routing"),
59
+ rspec.spec.("controllers/#{m[1]}_controller"),
60
+ rspec.spec.("acceptance/#{m[1]}")
61
+ ]
62
+ end
63
+
64
+ # Rails config changes
65
+ watch(rails.spec_helper) { rspec.spec_dir }
66
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
67
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
68
+
69
+ # Capybara features specs
70
+ watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
71
+
72
+ # Turnip features and steps
73
+ watch(%r{^spec/acceptance/(.+)\.feature$})
74
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
75
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
76
+ end
77
+ end
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # BusinessDateCalculator
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/business_date_calculator`. 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 'business_date_calculator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install business_date_calculator
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 `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` to 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
+ 1. Fork it ( https://github.com/[my-github-username]/business_date_calculator/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "business_date_calculator"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'business_date_calculator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "business_date_calculator"
8
+ spec.version = BusinessDateCalculator::VERSION
9
+ spec.authors = ["Lucas Pérez"]
10
+ spec.email = ["lucascperez@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby Library for dealing with business calendar.}
13
+ spec.description = %q{A Ruby Library for dealing with business calendar.}
14
+ spec.homepage = "https://github.com/investtools/business_date_calculator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "guard-rspec"
24
+ spec.add_development_dependency "terminal-notifier-guard"
25
+ spec.add_dependency "activesupport"
26
+ end
@@ -0,0 +1 @@
1
+ require "business_date_calculator/version"
@@ -0,0 +1,101 @@
1
+ module BusinessDateCalculator
2
+ class Calendar
3
+
4
+ # Constroi a estrutura de dados a partir de uma lista de holidays que
5
+ # sao os feriados, e usando um periodo especificado (fechado nas duas pontas).
6
+ def initialize(start_date, end_date, holidays)
7
+ build(start_date, end_date, holidays)
8
+ end
9
+
10
+ def is_holiday?(date)
11
+ date.wday == 0 || date.wday == 6 || @holidays.include?(date)
12
+ end
13
+
14
+ # Retorna o numero de dias uteis entre as duas data especificadas, inclusive.
15
+ # As duas datas devem ser dias uteis, ou caso não seja, deve ser
16
+ # especificado uma convenção de ajuste para cada data.
17
+ # A primeira data deve ser menor ou igual a segunda.
18
+ def networkdays(date1, date2, convention1 = :unadjusted, convention2 = :unadjusted)
19
+ range_check(date1)
20
+ range_check(date2)
21
+ i1 = adjusted_date_index(date1, convention1)
22
+ i2 = adjusted_date_index(date2, convention2)
23
+ raise "Adjusted date1 #{date1} is out of range" if i1 == nil
24
+ raise "Adjusted date2 #{date2} is out of range" if i2 == nil
25
+ i2 - i1
26
+ end
27
+
28
+ def adjust(date, convention)
29
+ range_check(date)
30
+ if not is_holiday?(date)
31
+ date
32
+ elsif convention == :unadjusted
33
+ date
34
+ else
35
+ case convention
36
+ when :following
37
+ @business_dates[@next_business_date_index[date]]
38
+ when :preceding
39
+ raise "Erro pegando data util anterior ao dia #{date}" if @prev_business_date_index[date] == nil
40
+ @business_dates[@prev_business_date_index[date]]
41
+ end
42
+ end
43
+ end
44
+
45
+ def advance(date, n, convention = :following)
46
+ range_check(date)
47
+ @business_dates[adjusted_date_index(date, convention) + n]
48
+ end
49
+
50
+ protected
51
+
52
+ def build(start_date, end_date, holidays)
53
+ # garante que start_date e end_date sao dias uteis
54
+ while start_date.wday == 0 || start_date.wday == 6 || holidays.include?(start_date) do
55
+ start_date -= 1.days
56
+ end
57
+ while end_date.wday == 0 || start_date.wday == 6 || holidays.include?(end_date) do
58
+ end_date += 1.days
59
+ end
60
+
61
+ @start_date = start_date
62
+ @end_date = end_date
63
+ @holidays = holidays
64
+ @business_dates = []
65
+ @business_date_index = {}
66
+ @next_business_date_index = {}
67
+ @prev_business_date_index = {}
68
+
69
+ d = start_date
70
+ i = 0
71
+ while d <= end_date do
72
+ if is_holiday?(d)
73
+ # dia não útil, mapeia o indice do dia util anterior e proximo
74
+ @next_business_date_index[d] = i
75
+ @prev_business_date_index[d] = i - 1
76
+ else
77
+ # dia util, adiciona ao final do array, e mapeia o indice do array no mapa
78
+ @business_dates << d
79
+ @business_date_index[d] = i
80
+ i = i + 1
81
+ end
82
+ d += 1.days
83
+ end
84
+ end
85
+
86
+ # Verifica se a data passada esta entre o periodo desta instancia.
87
+ def range_check(date)
88
+ if date < @start_date
89
+ # puts "Reconstruindo calculadora de feriados pois dia #{date} eh menor que #{@start_date} -> #{@end_date}"
90
+ build(date - 2.days, @end_date, @holidays)
91
+ elsif date > @end_date
92
+ # puts "Reconstruindo calculadora de feriados pois dia #{date} eh maior que #{end_date}"
93
+ build(@start_date, date + 252.days, @holidays)
94
+ end
95
+ end
96
+
97
+ def adjusted_date_index(date, convention)
98
+ @business_date_index[adjust(date, convention)]
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module BusinessDateCalculator
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: business_date_calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Pérez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-03 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: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
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
+ - !ruby/object:Gem::Dependency
56
+ name: terminal-notifier-guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A Ruby Library for dealing with business calendar.
84
+ email:
85
+ - lucascperez@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Guardfile
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - business_date_calculator.gemspec
100
+ - lib/business_date_calculator.rb
101
+ - lib/business_date_calculator/calendar.rb
102
+ - lib/business_date_calculator/version.rb
103
+ homepage: https://github.com/investtools/business_date_calculator
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: A Ruby Library for dealing with business calendar.
127
+ test_files: []