class BusinessPeriod::Days

Public Class Methods

call(from_date = nil, to_date = nil, options = {}) click to toggle source
# File lib/business_period/days.rb, line 5
def self.call(from_date = nil, to_date = nil, options = {})
  new.perform(from_date, to_date, options)
end

Public Instance Methods

perform(from_date, to_date, options) click to toggle source
# File lib/business_period/days.rb, line 9
def perform(from_date, to_date, options)
  return [] unless valid_params(from_date, to_date, options)

  period = calculate_period(to_date, options)
  result = business_days(period)

  {
    from_date: result[from_date],
    to_date: result[to_date]
  }
end

Private Instance Methods

business_days(period) click to toggle source
# File lib/business_period/days.rb, line 40
def business_days(period)
  period.each_with_object([]) do |day, container|
    if config.work_days.include?(day.wday)
      container << day unless day_is_holiday?(day)
    end
  end
end
primary_day_present?(options) click to toggle source
# File lib/business_period/days.rb, line 34
def primary_day_present?(options)
  return unless options.is_a?(Hash)

  options[:primary_day] ? (options[:primary_day].methods.include? :strftime) : true
end
valid_options(options) click to toggle source
# File lib/business_period/days.rb, line 30
def valid_options(options)
  options ? primary_day_present?(options) : true
end
valid_params(from_date, to_date, options) click to toggle source
# File lib/business_period/days.rb, line 23
def valid_params(from_date, to_date, options)
  valid_options(options) &&
    from_date.is_a?(Integer) && to_date.is_a?(Integer) &&
    from_date >= 0 && to_date >= 0 &&
    (from_date <= to_date)
end