Sha256: 92fdabdb9c7088cda5c513d20cc4353920ecb40738aa14da5ea481bd4007b37a

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require_relative "black_friday/version"
require "active_support/isolated_execution_state"
require "active_support/core_ext/array/wrap"
require "active_support/core_ext/numeric/time"

module BlackFriday
  extend self

  class Error < StandardError; end

  mattr_accessor :sales, default: {}

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

  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

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

  def current_sale
    current_sales.first
  end

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

  # Date helpers

  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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
black_friday-1.0.1 lib/black_friday.rb