black_friday 0.1.0 → 1.0.0
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: 7966bbf57ee41cf2517eb0dec3ca6195cb6b64f524cc6a7a05b2b5ffa38c666f
|
4
|
+
data.tar.gz: eb327004e46a88dbbd45ad513142ff790ce5bf85abe287ef5e0537eab6afc042
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49b2d7aa8c84b5c01e0fb451fb72e3c1e2d467fc82bf80a17a44a6281374da6f19d4eb6df0ec10dccb39aac2341680a38d1bdc428e88ef1afb75c648ab122f3d
|
7
|
+
data.tar.gz: 477c1820b56c6d7cb13de244299b81b3aed8cea4c7eb812b57a905f18f0831fd9a2e35a1b8c4ff2032488f683bb5da94d6c6a115019f650238a45f16928e0a4b
|
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.include?(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:
|
4
|
+
version: 1.0.0
|
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: []
|