Module: BlackFriday

Extended by:
BlackFriday
Included in:
BlackFriday
Defined in:
lib/black_friday.rb,
lib/black_friday/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"1.0.4"

Instance Method Summary collapse

Instance Method Details

#active?(*sale_names) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/black_friday.rb', line 19

def active?(*sale_names)
  blocks = sale_names.empty? ? sales.values : sales.values_at(*sale_names).compact
  Array.wrap(blocks).any? do
    in_range? instance_eval(&_1)
  end
end

#add_sale(name = :black_friday, &block) ⇒ Object



15
16
17
# File 'lib/black_friday.rb', line 15

def add_sale(name = :black_friday, &block)
  sales[name] = block
end

#black_friday(year = Date.today.year) ⇒ Object



58
59
60
# File 'lib/black_friday.rb', line 58

def black_friday(year = Date.today.year)
  thanksgiving(year) + 1.day
end

#canadian_thanksgiving(year = Date.today.year) ⇒ Object



52
53
54
55
56
# File 'lib/black_friday.rb', line 52

def canadian_thanksgiving(year = Date.today.year)
  oct_1st = Date.new(year, 10, 1)
  first_monday = oct_1st.monday? ? oct_1st : oct_1st.next_occurring(:monday)
  first_monday + 1.weeks
end

#current_saleObject



32
33
34
# File 'lib/black_friday.rb', line 32

def current_sale
  current_sales.first
end

#current_salesObject



26
27
28
29
30
# File 'lib/black_friday.rb', line 26

def current_sales
  sales.filter_map do |name, block|
    name if in_range? instance_eval(&block)
  end
end

#cyber_monday(year = Date.today.year) ⇒ Object



62
63
64
# File 'lib/black_friday.rb', line 62

def cyber_monday(year = Date.today.year)
  thanksgiving(year) + 4.days
end

#in_range?(range) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/black_friday.rb', line 36

def in_range?(range)
  range.cover?(range.first.is_a?(Date) ? Date.today : Time.current)
end

#range_for(sale_name) ⇒ Object



40
41
42
# File 'lib/black_friday.rb', line 40

def range_for(sale_name)
  instance_eval(&sales.fetch(sale_name))
end

#thanksgiving(year = Date.today.year) ⇒ Object

Date helpers



46
47
48
49
50
# File 'lib/black_friday.rb', line 46

def thanksgiving(year = Date.today.year)
  nov_1st = Date.new(year, 11, 1)
  first_thursday = nov_1st.thursday? ? nov_1st : nov_1st.next_occurring(:thursday)
  first_thursday + 3.weeks
end