Module: BankingDateTools

Defined in:
lib/banking_date_tools.rb,
lib/banking_date_tools/version.rb,
lib/banking_date_tools/banking_date_tools.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/banking_date_tools.rb', line 9

def self.included(base)
  base.extend(BankingDateTools)
end

.rootObject



5
6
7
# File 'lib/banking_date_tools.rb', line 5

def self.root
  File.expand_path(File.dirname(File.dirname(__FILE__)))
end

Instance Method Details

#banking_day?(date = Date.today, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/banking_date_tools/banking_date_tools.rb', line 4

def banking_day?(date=Date.today, options={})    
  # defaults
  holidays = options[:holidays] || []
  force_allow_days = options[:force_allow_days] || []

  unless force_allow_days.include?(date)
    return false if options[:no_holidays] && holidays.include?(date)
    if options[:no_weekends] 
      return false if (date.saturday? || date.sunday?)
    else
      return false if options[:no_saturdays] && date.saturday?
      return false if options[:no_sundays] && date.sunday?
    end
  end
  true
end

#get_banking_day(date, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/banking_date_tools/banking_date_tools.rb', line 21

def get_banking_day(date, options={})    
  padding = options[:padding] || 0
  adjusted_padding = (padding.abs + 1)

  increment = (options[:previous] ? -1 : 1)

  adjusted_padding.times do |p|
    date = date.next_day(increment) if p != 0 
    while !banking_day?(date, options)
      date = date.next_day(increment)   
    end
  end
  date
end

#get_next_banking_day(date, options = {}) ⇒ Object



36
37
38
39
40
# File 'lib/banking_date_tools/banking_date_tools.rb', line 36

def get_next_banking_day(date, options={})
  options[:padding] ||= 1
  options[:previous] = false
  get_banking_day(date, options)
end

#get_previous_banking_day(date, options = {}) ⇒ Object



42
43
44
45
46
# File 'lib/banking_date_tools/banking_date_tools.rb', line 42

def get_previous_banking_day(date, options={})
  options[:padding] ||= 1
  options[:previous] = true
  get_banking_day(date, options)
end

#safe_date(date) ⇒ Object



48
49
50
51
52
53
# File 'lib/banking_date_tools/banking_date_tools.rb', line 48

def safe_date(date)
  if date.blank?
    return Date.today
  end
  date.to_date
end

#tick(date = nil) ⇒ Object

creates a date integer formated like a MS.Net date tick



56
57
58
59
60
61
62
63
# File 'lib/banking_date_tools/banking_date_tools.rb', line 56

def tick(date=nil)
  if date.blank?
    date = Time.now
  else
    date = date.to_time
  end
  date.tick
end