Skip to content

matass/business-period

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

43 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

CircleCI Maintainability Gem Version

Business period

BusinessPeriod is a ruby library that calculates business period by given hash. This library was designed for lithuanian, latvian and estonian unemployment days.

Installation

Add this line to your application's Gemfile:

gem 'business-period'

And then execute:

$ bundle

Or install it yourself as:

$ gem install business-period

Quickstart

There are two ways to initialize:

Initialization:
# config/initializers/business_period.rb

# Set locale to get config file from config/holidays path.
# Set work_days to define which days of week are work days.

locale = 'lt'
work_days = [1, 2, 3, 4, 5]

BusinessPeriod::Config.locale = locale
BusinessPeriod::Config.work_days = work_days
Initialization with Proc:
locale = 'lt'
work_days = [1, 2, 3, 4, 5]

BusinessPeriod::Config.locale = -> { locale }
BusinessPeriod::Config.work_days = -> { work_days }

How it works

  1. Dynamically calculates how many days we have to add to period end (Saturdays/Sundays/Holidays).
  2. Generates new array.
  3. Extracts weekends and holidays from newly generated array.
  4. Generates result array.

Usage

from = 2
to = 4

# You can pass optional parameters if You want to set primary day as starting point.
# options = { primary_day: Time.now - (3600 * 24) }

# Call BusinessPeriod::Days class to calculate period 
BusinessPeriod::Days.call(from, to, options)

Examples

Let's say we have no holidays this month and today is Wednesday.

work_days = [1, 2, 3, 4, 5] (all days except weekends)

from = 2 to = 4

  • Begins to count period from the coming day.
  • Tomorrow (Thursday) is the first valid day.
  • The second valid day will be Fridary (first business day).
  • Fourth business day will be Tuesday (Saturday and Sunday are not in scope).
irb(main):001:0> Time.current
=> Wed, 12 Sep 2018 05:49:10 UTC +00:00
irb(main):002:0> from = 2
=> 2
irb(main):003:0> to = 4
=> 4
irb(main):004:0> BusinessPeriod::Days.call(from, to)
=> {:from_date=>Fri, 14 Sep 2018, :to_date=>Tue, 18 Sep 2018]

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

License

The gem is available as open source under the terms of the MIT License.

About

๐Ÿ“… calculate business period

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages