Class: ChronicBetween

Inherits:
Object
  • Object
show all
Includes:
AppRoutes
Defined in:
lib/chronic_between.rb

Instance Method Summary collapse

Constructor Details

#initialize(x, debug: false) ⇒ ChronicBetween

Returns a new instance of ChronicBetween.



12
13
14
15
16
17
# File 'lib/chronic_between.rb', line 12

def initialize(x, debug: false)    
  
  @debug = debug
  @times = x.is_a?(String) ? x.split(/[,;&]/).map(&:strip) : x
  super()
end

Instance Method Details

#within?(raw_date) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chronic_between.rb', line 19

def within?(raw_date)
  
  date = raw_date.respond_to?(:to_datetime) ? raw_date.to_datetime : raw_date
  @timezone = date.strftime("%z")
  @params[:timezone] = @timezone
  @year = date.year
  dates = []
  
  ranges(@params, date)
  
  negatives = '(not|except)\s+'
  times, closed_times = @times.partition {|x| !x[/^#{negatives}/]}
  closed_times.map!{|x| x[/^#{negatives}(.*)/,2]}
  
  dates = build times
  inside_range = dates.detect {|d1, d2| date.between? d1, d2}    
  
  neg_dates = build closed_times
  inside_restrictions = neg_dates.detect {|d1, d2| date.between? d1, d2}

  if inside_restrictions then
    return false 
  elsif closed_times.any? and times.empty? then
    return true
  elsif inside_range then
    return true
  else
    return false
  end
  
end