black_friday 0.1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -11
- data/lib/black_friday/version.rb +1 -1
- data/lib/black_friday.rb +48 -1
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e679dd4cfca531e409eef4065b42bb7d057724366cdee2828749191f908547b
|
4
|
+
data.tar.gz: 606472c6b02891eb1cac69679c07823ba5a6c082f8973fb66a7051cf80be76c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce90f9beccdc0eb519ec21b1663dd180afd585c057ae0b075a16156467931fea49808ab25aadeccd5adec4036306215353034d8d2f9a0fa74d2fe17c0f3736b8
|
7
|
+
data.tar.gz: 0eaf8488af278d3858040b27fb08f798032982355eb93558378b617a9891cad207ae95a26b2aa6fbc3e0d000dad74f0269369e10cc23f1fd9cccbc2af037468a
|
data/README.md
CHANGED
@@ -1,28 +1,67 @@
|
|
1
1
|
# BlackFriday
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/black_friday`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
Add Black Friday sales to your Rails app.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
-
|
11
7
|
Install the gem and add to the application's Gemfile by executing:
|
12
8
|
|
13
9
|
```bash
|
14
|
-
bundle add
|
10
|
+
bundle add black_friday
|
15
11
|
```
|
16
12
|
|
17
|
-
|
13
|
+
## Usage
|
18
14
|
|
19
|
-
|
20
|
-
|
15
|
+
### Date helpers
|
16
|
+
|
17
|
+
Black Friday provides some helpers for dates. You can also pass in the year which can be helpful for making reports for previous years.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
BlackFriday.thanksgiving #=> Thu, 28 Nov 2024
|
21
|
+
BlackFriday.black_friday #=> Fri, 29 Nov 2024
|
22
|
+
BlackFriday.cyber_monday #=> Mon, 2 Dec 2024
|
23
|
+
|
24
|
+
BlackFriday.thanksgiving(2029) #=> Thu, 22 Nov 2029
|
21
25
|
```
|
22
26
|
|
23
|
-
|
27
|
+
### Adding Sales
|
28
|
+
|
29
|
+
Sales are handy to set the date/time range for a sale. The `add_sale` block should return a `Range`.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
BlackFriday.add_sale do
|
33
|
+
thanksgiving.monday.beginning_of_day..cyber_monday.end_of_day
|
34
|
+
end
|
24
35
|
|
25
|
-
|
36
|
+
BlackFriday.add_sale :labor_day do
|
37
|
+
# First Monday in September
|
38
|
+
sep_1st = Date.new(Date.today.year, 9, 1)
|
39
|
+
labor_day = sep_1st.monday? ? sep_1st : sep_1st.next_occurring(:monday)
|
40
|
+
start_day = labor_day - 3.days
|
41
|
+
|
42
|
+
start_day.beginning_of_day..labor_day.end_of_day
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
You can then check if a sale is active:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
BlackFriday.active?
|
50
|
+
#=> true/false
|
51
|
+
|
52
|
+
# Or specific sale(s)
|
53
|
+
BlackFriday.active?(:black_friday, :labor_day)
|
54
|
+
#=> true/false
|
55
|
+
```
|
56
|
+
|
57
|
+
To check the active sales:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
BlackFriday.current_sale #=> :black_friday
|
61
|
+
|
62
|
+
# For multiple sales at once
|
63
|
+
BlackFriday.current_sales #=> [:black_friday]
|
64
|
+
```
|
26
65
|
|
27
66
|
## Development
|
28
67
|
|
data/lib/black_friday/version.rb
CHANGED
data/lib/black_friday.rb
CHANGED
@@ -1,8 +1,55 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "black_friday/version"
|
4
|
+
require "active_support/isolated_execution_state"
|
5
|
+
require "active_support/core_ext/array/wrap"
|
6
|
+
require "active_support/core_ext/numeric/time"
|
4
7
|
|
5
8
|
module BlackFriday
|
9
|
+
extend self
|
10
|
+
|
6
11
|
class Error < StandardError; end
|
7
|
-
|
12
|
+
|
13
|
+
mattr_accessor :sales, default: {}
|
14
|
+
|
15
|
+
def add_sale(name = :black_friday, &block)
|
16
|
+
sales[name] = block
|
17
|
+
end
|
18
|
+
|
19
|
+
def active?(*sale_names)
|
20
|
+
blocks = sale_names.empty? ? sales.values : sales.values_at(*sale_names).compact
|
21
|
+
Array.wrap(blocks).any? do
|
22
|
+
in_range? instance_eval(&_1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def current_sales
|
27
|
+
sales.select do |name, block|
|
28
|
+
in_range? instance_eval(&block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def current_sale
|
33
|
+
current_sales.first
|
34
|
+
end
|
35
|
+
|
36
|
+
def in_range?(range)
|
37
|
+
range.cover?(range.first.is_a?(Date) ? Date.today : Time.current)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Date helpers
|
41
|
+
|
42
|
+
def thanksgiving(year = Date.today.year)
|
43
|
+
nov_1st = Date.new(year, 11, 1)
|
44
|
+
first_thursday = nov_1st.thursday? ? nov_1st : nov_1st.next_occurring(:thursday)
|
45
|
+
first_thursday + 3.weeks
|
46
|
+
end
|
47
|
+
|
48
|
+
def black_friday(year = Date.today.year)
|
49
|
+
thanksgiving(year) + 1.day
|
50
|
+
end
|
51
|
+
|
52
|
+
def cyber_monday(year = Date.today.year)
|
53
|
+
thanksgiving(year) + 4.days
|
54
|
+
end
|
8
55
|
end
|
metadata
CHANGED
@@ -1,16 +1,30 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: black_friday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
12
|
-
dependencies:
|
13
|
-
|
11
|
+
date: 2024-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Black Friday sales in your Rails apps
|
14
28
|
email:
|
15
29
|
- excid3@gmail.com
|
16
30
|
executables: []
|
@@ -51,5 +65,5 @@ requirements: []
|
|
51
65
|
rubygems_version: 3.5.23
|
52
66
|
signing_key:
|
53
67
|
specification_version: 4
|
54
|
-
summary:
|
68
|
+
summary: Black Friday sales in your Rails apps
|
55
69
|
test_files: []
|