Module: InBusiness::InstanceMethods

Included in:
InBusiness
Defined in:
lib/in_business.rb

Instance Method Summary collapse

Instance Method Details

#closed?(datetime = DateTime.now) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/in_business.rb', line 52

def closed?(datetime=DateTime.now)
  !open?(datetime)
end

#daysObject

Maps values of [DateTime/Date/Time]#wday to English days



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/in_business.rb', line 61

def days
  {
    "0" => "sunday",
    "1" => "monday",
    "2" => "tuesday",
    "3" => "wednesday",
    "4" => "thursday",
    "5" => "friday",
    "6" => "saturday"
  }
end

#holidaysObject



13
14
15
# File 'lib/in_business.rb', line 13

def holidays
  @holidays ||= []
end

#holidays=(array) ⇒ Object



17
18
19
# File 'lib/in_business.rb', line 17

def holidays=(array)
  @holidays = array
end

#hoursObject



21
22
23
# File 'lib/in_business.rb', line 21

def hours
  @hours ||= OpenStruct.new
end

#hours=(hash) ⇒ Object



25
26
27
# File 'lib/in_business.rb', line 25

def hours=(hash)
  @hours = OpenStruct.new(hash)
end

#is_holiday?(date = DateTime.now) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/in_business.rb', line 56

def is_holiday?(date=DateTime.now)
  holidays.include? date.to_date
end

#open?(datetime = DateTime.now) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/in_business.rb', line 36

def open?(datetime=DateTime.now)
  
  # If this is included in the list of holidays, return false
  return false if is_holiday? datetime

  # If we don't know the opening hours for datetime's day, assume we're closed
  return false unless hours.send(days[datetime.wday.to_s].to_sym)

  # We have opening hours, so check if the current time is within them
  if !hours.send(days[datetime.wday.to_s].to_sym).cover? datetime.strftime("%H:%M")
    return false
  end

  true # It's not not open, so it must be open ;)
end

#resetObject



29
30
31
32
33
34
# File 'lib/in_business.rb', line 29

def reset
  # Used for clearing the state of InBusiness between specs
  self.holidays = []
  self.hours = {}
  true
end