redis-objects-daily-counter 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +23 -20
  3. data/.github/dependabot.yml +19 -0
  4. data/.rubocop.yml +4 -4
  5. data/CHANGELOG.md +38 -16
  6. data/Dockerfile +1 -1
  7. data/Gemfile +1 -1
  8. data/Gemfile.lock +30 -27
  9. data/README.md +81 -33
  10. data/bin/console +1 -2
  11. data/lib/redis/base_counter_object.rb +4 -0
  12. data/lib/redis/base_hash_key_object.rb +40 -0
  13. data/lib/redis/base_set_object.rb +4 -0
  14. data/lib/redis/objects/daily-counter/version.rb +1 -1
  15. data/lib/redis/objects/periodical_counters.rb +40 -0
  16. data/lib/redis/objects/periodical_hashes.rb +50 -0
  17. data/lib/redis/objects/periodical_sets.rb +50 -0
  18. data/lib/redis/periodical_counter.rb +15 -0
  19. data/lib/redis/periodical_hash_key.rb +15 -0
  20. data/lib/redis/periodical_set.rb +15 -0
  21. data/lib/redis/recurring_at_intervals.rb +1 -1
  22. data/lib/redis-objects-daily-counter.rb +16 -38
  23. data/redis-objects-daily-counter.gemspec +1 -1
  24. metadata +12 -28
  25. data/lib/redis/annual_counter.rb +0 -13
  26. data/lib/redis/annual_set.rb +0 -13
  27. data/lib/redis/daily_counter.rb +0 -13
  28. data/lib/redis/daily_set.rb +0 -13
  29. data/lib/redis/hourly_counter.rb +0 -13
  30. data/lib/redis/hourly_set.rb +0 -13
  31. data/lib/redis/minutely_counter.rb +0 -13
  32. data/lib/redis/minutely_set.rb +0 -13
  33. data/lib/redis/monthly_counter.rb +0 -13
  34. data/lib/redis/monthly_set.rb +0 -13
  35. data/lib/redis/objects/annual_counters.rb +0 -41
  36. data/lib/redis/objects/annual_sets.rb +0 -49
  37. data/lib/redis/objects/daily_counters.rb +0 -41
  38. data/lib/redis/objects/daily_sets.rb +0 -49
  39. data/lib/redis/objects/hourly_counters.rb +0 -41
  40. data/lib/redis/objects/hourly_sets.rb +0 -49
  41. data/lib/redis/objects/minutely_counters.rb +0 -41
  42. data/lib/redis/objects/minutely_sets.rb +0 -49
  43. data/lib/redis/objects/monthly_counters.rb +0 -41
  44. data/lib/redis/objects/monthly_sets.rb +0 -49
  45. data/lib/redis/objects/weekly_counters.rb +0 -41
  46. data/lib/redis/objects/weekly_sets.rb +0 -49
  47. data/lib/redis/weekly_counter.rb +0 -13
  48. data/lib/redis/weekly_set.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e0a7c068797a495a2e26fc69aa33991b83b8aaf2f61cbde250e85f79d0d2a0d
4
- data.tar.gz: 369237147cc7c2d7edc50c0fb39845a76430d7cf30428d3d0a4a6eef51de194b
3
+ metadata.gz: d7df523f0449f89d1c4208f2ff1d950e3781fa4d4b7b35a2628176001fde9fb0
4
+ data.tar.gz: 24bd7fdbf06221392984bac90426c6357b93bf70b64b9fe91d43d49df7f7f1b0
5
5
  SHA512:
6
- metadata.gz: e41f54c69a341c6a3809c184cc20c6c0f61b0f7635048242986d5e8e4389f7118c7481a68b715c4de3d9bc7f9c05158400816fe0ec3f97ac0dab3e19598b6b4f
7
- data.tar.gz: 2c3f6b1170202d799ed729477406179c10a60f19ea96280a269377551fda48870f3dc5912954e57cf69c6c732e027a59efbb3c96570f94ab8d93a84308c4a5fc
6
+ metadata.gz: 41e8781fe5227151c9134803d89f92fa1802e1681dbba2b65ce0c1a8e2e43565647927438574639f9d4e3620bd5fcc3746e9ddea6204f3e861db4a1189fd2c88
7
+ data.tar.gz: 6183fe989bae2284fd4ff9fe000468d0ddb8c180baa7c363a1d903267f0b5685c04150d656be9d92a5daf2d81f593b9e304f7ce3df5b0027ef1b2bdf1f4a96a4
data/.circleci/config.yml CHANGED
@@ -12,25 +12,25 @@ references:
12
12
  - &ruby_version
13
13
  ruby_version:
14
14
  type: enum
15
- enum: ['2.6', '2.7', '3.0']
16
- default: '3.0'
15
+ enum: ["2.7", "3.0", "3.1"]
16
+ default: "3.1"
17
17
 
18
18
  executors:
19
19
  default:
20
20
  parameters:
21
21
  <<: *ruby_version
22
22
  docker:
23
- - image: circleci/ruby:<< parameters.ruby_version >>
23
+ - image: cimg/ruby:<< parameters.ruby_version >>
24
24
  - image: redis:latest
25
25
  name: redis
26
26
  working_directory: ~/repo
27
27
 
28
28
  commands:
29
29
  run_rspec:
30
- description: 'Run RSpec'
30
+ description: "Run RSpec"
31
31
  steps:
32
32
  - run:
33
- name: 'Execute RSpec'
33
+ name: "Execute RSpec"
34
34
  command: |
35
35
  mkdir /tmp/test-results
36
36
  TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
@@ -60,7 +60,7 @@ commands:
60
60
  name: Run RuboCop
61
61
  command: bundle exec rubocop
62
62
  yardoc:
63
- description: 'Generate YARDoc'
63
+ description: "Generate YARDoc"
64
64
  steps:
65
65
  - run: bundle exec yardoc -o ./yardoc
66
66
  - store_artifacts:
@@ -76,7 +76,7 @@ commands:
76
76
  - run:
77
77
  name: Rubocop Challenge
78
78
  command: |
79
- gem install rubocop_challenger --pre
79
+ gem install rubocop_challenger
80
80
  rubocop_challenger go --email=ryz310@gmail.com --name=ryz310
81
81
  release:
82
82
  description: Release to RubyGems.org
@@ -98,9 +98,6 @@ commands:
98
98
  <<: *ruby_version
99
99
  steps:
100
100
  - checkout
101
- - run:
102
- name: Install Bundler 2.x
103
- command: gem install bundler:2.1.4
104
101
  - ruby-orbs/bundle-install:
105
102
  cache_key_prefix: v1-dependencies-<< parameters.ruby_version >>
106
103
  test_and_build:
@@ -132,9 +129,14 @@ jobs:
132
129
  parts: 3
133
130
  - code-climate/upload-coverage
134
131
  rubocop:
135
- executor: default
132
+ parameters:
133
+ <<: *ruby_version
134
+ executor:
135
+ name: default
136
+ ruby_version: << parameters.ruby_version >>
136
137
  steps:
137
- - setup
138
+ - setup:
139
+ ruby_version: << parameters.ruby_version >>
138
140
  - rubocop
139
141
  yardoc:
140
142
  executor: default
@@ -157,28 +159,29 @@ workflows:
157
159
 
158
160
  commit:
159
161
  jobs:
160
- - build:
161
- name: build_on_ruby_2.6
162
- ruby_version: '2.6'
163
162
  - build:
164
163
  name: build_on_ruby_2.7
165
- ruby_version: '2.7'
164
+ ruby_version: "2.7"
166
165
  - build:
167
166
  name: build_on_ruby_3.0
168
- ruby_version: '3.0'
169
- - rubocop
167
+ ruby_version: "3.0"
168
+ - build:
169
+ name: build_on_ruby_3.1
170
+ ruby_version: "3.1"
171
+ - rubocop:
172
+ ruby_version: "2.7"
170
173
  - yardoc
171
174
  - upload-coverage:
172
175
  requires:
173
- - build_on_ruby_2.6
174
176
  - build_on_ruby_2.7
175
177
  - build_on_ruby_3.0
178
+ - build_on_ruby_3.1
176
179
  - release:
177
180
  context: RubyGems API Key
178
181
  requires:
179
- - build_on_ruby_2.6
180
182
  - build_on_ruby_2.7
181
183
  - build_on_ruby_3.0
184
+ - build_on_ruby_3.1
182
185
  - rubocop
183
186
  filters:
184
187
  branches:
@@ -0,0 +1,19 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ time: "08:00"
8
+ timezone: Japan
9
+ open-pull-requests-limit: 10
10
+ reviewers:
11
+ - ryz310
12
+ assignees:
13
+ - ryz310
14
+ labels:
15
+ - dependabot
16
+ ignore:
17
+ - dependency-name: simplecov
18
+ versions:
19
+ - ">= 0.19.a, < 0.20"
data/.rubocop.yml CHANGED
@@ -5,7 +5,7 @@ require:
5
5
  - rubocop-rspec
6
6
 
7
7
  AllCops:
8
- TargetRubyVersion: 2.6
8
+ TargetRubyVersion: 2.7
9
9
  NewCops: enable
10
10
 
11
11
  Layout/LineLength:
@@ -13,15 +13,15 @@ Layout/LineLength:
13
13
 
14
14
  Metrics/BlockLength:
15
15
  Exclude:
16
- - 'spec/**/*'
17
- - 'redis-objects-daily-counter.gemspec'
16
+ - "spec/**/*"
17
+ - "redis-objects-daily-counter.gemspec"
18
18
 
19
19
  Metrics/MethodLength:
20
20
  Max: 15
21
21
 
22
22
  Naming/FileName:
23
23
  Exclude:
24
- - 'lib/redis-objects-daily-counter.rb'
24
+ - "lib/redis-objects-daily-counter.rb"
25
25
 
26
26
  Style/TrailingCommaInArrayLiteral:
27
27
  EnforcedStyleForMultiline: comma
data/CHANGELOG.md CHANGED
@@ -1,10 +1,32 @@
1
1
  # Change log
2
2
 
3
+ ## v0.4.0 (Jul 26, 2022)
4
+
5
+ ### Feature
6
+
7
+ - [#17](https://github.com/ryz310/redis-object-daily-counter/pull/17) Support `Redis::HashKey` as a recurring object ([@ryz310](https://github.com/ryz310))
8
+
9
+ ### Breaking Change
10
+
11
+ - [#18](https://github.com/ryz310/redis-object-daily-counter/pull/18) Bump up Ruby 3.1 and drop support Ruby 2.6 ([@ryz310](https://github.com/ryz310))
12
+
13
+ ### Dependabot
14
+
15
+ - [#16](https://github.com/ryz310/redis-object-daily-counter/pull/16) Bump yard from 0.9.26 to 0.9.28 ([@ryz310](https://github.com/ryz310))
16
+ - [#15](https://github.com/ryz310/redis-object-daily-counter/pull/15) Bump rspec_junit_formatter from 0.4.1 to 0.5.1 ([@ryz310](https://github.com/ryz310))
17
+ - [#13](https://github.com/ryz310/redis-object-daily-counter/pull/13) Bump rspec from 3.10.0 to 3.11.0 ([@ryz310](https://github.com/ryz310))
18
+ - [#14](https://github.com/ryz310/redis-object-daily-counter/pull/14) Bump timecop from 0.9.4 to 0.9.5 ([@ryz310](https://github.com/ryz310))
19
+ - [#12](https://github.com/ryz310/redis-object-daily-counter/pull/12) Bump redis-objects from 1.5.1 to 1.7.0 ([@ryz310](https://github.com/ryz310))
20
+
21
+ ### Misc
22
+
23
+ - [#19](https://github.com/ryz310/redis-object-daily-counter/pull/19) Reduce similar source codes with meta programming ([@ryz310](https://github.com/ryz310))
24
+
3
25
  ## v0.3.0 (Sep 23, 2021)
4
26
 
5
27
  ### Feature
6
28
 
7
- * [#7](https://github.com/ryz310/redis-object-daily-counter/pull/7) Add daily set ([@ryz310](https://github.com/ryz310))
29
+ - [#7](https://github.com/ryz310/redis-object-daily-counter/pull/7) Add daily set ([@ryz310](https://github.com/ryz310))
8
30
 
9
31
  > You can use `daily_set` in addition to the standard features of Redis::Objects.
10
32
  >
@@ -47,24 +69,24 @@
47
69
 
48
70
  ### Breaking Change
49
71
 
50
- * [#7](https://github.com/ryz310/redis-object-daily-counter/pull/7) Add daily set ([@ryz310](https://github.com/ryz310))
72
+ - [#7](https://github.com/ryz310/redis-object-daily-counter/pull/7) Add daily set ([@ryz310](https://github.com/ryz310))
51
73
 
52
74
  > Rename the method from #delete to #delete_at a73251f
53
- >
75
+ >
54
76
  > ```rb
55
77
  > # Before
56
78
  > homepage.pv.delete(Date.new(2021, 4, 1))
57
- >
79
+ >
58
80
  > # After
59
81
  > homepage.pv.delete_at(Date.new(2021, 4, 1))
60
82
  > ```
61
- >
83
+ >
62
84
  > Modify returning value of RecurringAtIntervals#at 1c8cc79
63
- >
85
+ >
64
86
  > ```rb
65
87
  > # Before
66
88
  > homepage.pv.at(Date.new(2021, 4, 2)) # => 2
67
- >
89
+ >
68
90
  > # After
69
91
  > homepage.pv.at(Date.new(2021, 4, 2)) # => #<Redis::Counter key="homepage:1:pv:2021-04-02">
70
92
  > ```
@@ -73,18 +95,18 @@
73
95
 
74
96
  ### Feature
75
97
 
76
- * [#3](https://github.com/ryz310/redis-objects-daily-counter/pull/3) Support time zone ([@ryz310](https://github.com/ryz310))
77
- * [#4](https://github.com/ryz310/redis-objects-daily-counter/pull/4) Add daily counter family ([@ryz310](https://github.com/ryz310))
78
- * `annual_counter`
79
- * `monthly_counter`
80
- * `weekly_counter`
81
- * `hourly_counter`
82
- * `minutely_counter`
98
+ - [#3](https://github.com/ryz310/redis-objects-daily-counter/pull/3) Support time zone ([@ryz310](https://github.com/ryz310))
99
+ - [#4](https://github.com/ryz310/redis-objects-daily-counter/pull/4) Add daily counter family ([@ryz310](https://github.com/ryz310))
100
+ - `annual_counter`
101
+ - `monthly_counter`
102
+ - `weekly_counter`
103
+ - `hourly_counter`
104
+ - `minutely_counter`
83
105
 
84
106
  ### Misc
85
107
 
86
- * [#2](https://github.com/ryz310/redis-objects-daily-counter/pull/2) Install circle ci ([@ryz310](https://github.com/ryz310))
108
+ - [#2](https://github.com/ryz310/redis-objects-daily-counter/pull/2) Install circle ci ([@ryz310](https://github.com/ryz310))
87
109
 
88
110
  ## v0.1.0 (Sep 12, 2021)
89
111
 
90
- * The first release :tada:
112
+ - The first release :tada:
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM ruby:2.6
1
+ FROM ruby:2.7
2
2
 
3
3
  RUN mkdir /my_app
4
4
  WORKDIR /my_app
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ gemspec
8
8
  gem 'bundler', '>= 2.0'
9
9
  gem 'pry-byebug'
10
10
  gem 'rake', '~> 13.0'
11
- gem 'rspec', '~> 3.0'
11
+ gem 'rspec', '~> 3.11'
12
12
  gem 'rspec_junit_formatter'
13
13
  gem 'rubocop', '~> 0.80'
14
14
  gem 'rubocop-performance'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redis-objects-daily-counter (0.3.0)
4
+ redis-objects-daily-counter (0.4.0)
5
5
  redis-objects
6
6
 
7
7
  GEM
@@ -10,11 +10,11 @@ GEM
10
10
  ast (2.4.2)
11
11
  byebug (11.1.3)
12
12
  coderay (1.1.3)
13
- diff-lcs (1.4.4)
13
+ diff-lcs (1.5.0)
14
14
  docile (1.4.0)
15
15
  method_source (1.0.0)
16
- parallel (1.21.0)
17
- parser (3.0.2.0)
16
+ parallel (1.22.1)
17
+ parser (3.1.2.0)
18
18
  ast (~> 2.4.1)
19
19
  pry (0.13.1)
20
20
  coderay (~> 1.1)
@@ -22,27 +22,27 @@ GEM
22
22
  pry-byebug (3.9.0)
23
23
  byebug (~> 11.0)
24
24
  pry (~> 0.13.0)
25
- rainbow (3.0.0)
25
+ rainbow (3.1.1)
26
26
  rake (13.0.6)
27
- redis (4.4.0)
28
- redis-objects (1.5.1)
29
- redis (~> 4.2)
30
- regexp_parser (2.1.1)
27
+ redis (4.7.1)
28
+ redis-objects (1.7.0)
29
+ redis
30
+ regexp_parser (2.5.0)
31
31
  rexml (3.2.5)
32
- rspec (3.10.0)
33
- rspec-core (~> 3.10.0)
34
- rspec-expectations (~> 3.10.0)
35
- rspec-mocks (~> 3.10.0)
36
- rspec-core (3.10.1)
37
- rspec-support (~> 3.10.0)
38
- rspec-expectations (3.10.1)
32
+ rspec (3.11.0)
33
+ rspec-core (~> 3.11.0)
34
+ rspec-expectations (~> 3.11.0)
35
+ rspec-mocks (~> 3.11.0)
36
+ rspec-core (3.11.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-expectations (3.11.0)
39
39
  diff-lcs (>= 1.2.0, < 2.0)
40
- rspec-support (~> 3.10.0)
41
- rspec-mocks (3.10.2)
40
+ rspec-support (~> 3.11.0)
41
+ rspec-mocks (3.11.1)
42
42
  diff-lcs (>= 1.2.0, < 2.0)
43
- rspec-support (~> 3.10.0)
44
- rspec-support (3.10.2)
45
- rspec_junit_formatter (0.4.1)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-support (3.11.0)
45
+ rspec_junit_formatter (0.5.1)
46
46
  rspec-core (>= 2, < 4, != 2.12.0)
47
47
  rubocop (0.93.1)
48
48
  parallel (~> 1.10)
@@ -53,8 +53,8 @@ GEM
53
53
  rubocop-ast (>= 0.6.0)
54
54
  ruby-progressbar (~> 1.7)
55
55
  unicode-display_width (>= 1.4.0, < 2.0)
56
- rubocop-ast (1.11.0)
57
- parser (>= 3.0.1.1)
56
+ rubocop-ast (1.19.1)
57
+ parser (>= 3.1.1.0)
58
58
  rubocop-performance (1.10.2)
59
59
  rubocop (>= 0.90.0, < 2.0)
60
60
  rubocop-ast (>= 0.4.0)
@@ -67,14 +67,17 @@ GEM
67
67
  simplecov-html (~> 0.11)
68
68
  simplecov_json_formatter (~> 0.1)
69
69
  simplecov-html (0.12.3)
70
- simplecov_json_formatter (0.1.3)
71
- timecop (0.9.4)
70
+ simplecov_json_formatter (0.1.4)
71
+ timecop (0.9.5)
72
72
  unicode-display_width (1.8.0)
73
- yard (0.9.26)
73
+ webrick (1.7.0)
74
+ yard (0.9.28)
75
+ webrick (~> 1.7.0)
74
76
 
75
77
  PLATFORMS
76
78
  aarch64-linux
77
79
  x86_64-darwin-19
80
+ x86_64-darwin-20
78
81
  x86_64-linux
79
82
 
80
83
  DEPENDENCIES
@@ -82,7 +85,7 @@ DEPENDENCIES
82
85
  pry-byebug
83
86
  rake (~> 13.0)
84
87
  redis-objects-daily-counter!
85
- rspec (~> 3.0)
88
+ rspec (~> 3.11)
86
89
  rspec_junit_formatter
87
90
  rubocop (~> 0.80)
88
91
  rubocop-performance
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![CircleCI](https://circleci.com/gh/ryz310/redis-objects-daily-counter.svg?style=svg)](https://circleci.com/gh/ryz310/redis-objects-daily-counter) [![Gem Version](https://badge.fury.io/rb/redis-objects-daily-counter.svg)](https://badge.fury.io/rb/redis-objects-daily-counter) [![Maintainability](https://api.codeclimate.com/v1/badges/1e1cb0d70e4f80e0fdd5/maintainability)](https://codeclimate.com/github/ryz310/redis-objects-daily-counter/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/1e1cb0d70e4f80e0fdd5/test_coverage)](https://codeclimate.com/github/ryz310/redis-objects-daily-counter/test_coverage) [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=ryz310/redis-objects-daily-counter)](https://dependabot.com)
1
+ [![CircleCI](https://circleci.com/gh/ryz310/redis-objects-daily-counter.svg?style=svg)](https://circleci.com/gh/ryz310/redis-objects-daily-counter) [![Gem Version](https://badge.fury.io/rb/redis-objects-daily-counter.svg)](https://badge.fury.io/rb/redis-objects-daily-counter) [![Maintainability](https://api.codeclimate.com/v1/badges/1e1cb0d70e4f80e0fdd5/maintainability)](https://codeclimate.com/github/ryz310/redis-objects-daily-counter/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/1e1cb0d70e4f80e0fdd5/test_coverage)](https://codeclimate.com/github/ryz310/redis-objects-daily-counter/test_coverage)
2
2
 
3
3
  # Redis::Objects::Daily::Counter and Daily::Set
4
4
 
@@ -30,6 +30,7 @@ class Homepage
30
30
  include Redis::Objects
31
31
 
32
32
  daily_counter :pv, expireat: -> { Time.now + 2_678_400 } # about a month
33
+ daily_hash_key :browsing_history, expireat: -> { Time.now + 2_678_400 } # about a month
33
34
  daily_set :dau, expireat: -> { Time.now + 2_678_400 } # about a month
34
35
 
35
36
  def id
@@ -57,7 +58,7 @@ end_date = Date.new(2021, 4, 2)
57
58
  homepage.pv.range(start_date, end_date) # [3, 2]
58
59
  ```
59
60
 
60
- ### Daily Counter
61
+ ### Daily Counters
61
62
 
62
63
  The daily counter automatically switches the save destination when the date changes.
63
64
  You can access past dates counted values like Ruby arrays:
@@ -82,23 +83,71 @@ homepage.pv.at(Date.new(2021, 4, 2)) # => #<Redis::Counter key="homepage:1:pv:20
82
83
  homepage.pv.at(Date.new(2021, 4, 2)).value # 2
83
84
  ```
84
85
 
85
- #### Daily Counter Family
86
+ #### Daily Counters Family
86
87
 
87
- * `annual_counter`
88
- * Key format: `model_name:id:field_name:yyyy`
89
- * Redis is a highly volatile key-value store, so I don't recommend using it.
90
- * `monthly_counter`
91
- * Key format: `model_name:id:field_name:yyyy-mm`
92
- * `weekly_counter`
93
- * Key format: `model_name:id:field_name:yyyyWw`
94
- * `daily_counter`
95
- * Key format: `model_name:id:field_name:yyyy-mm-dd`
96
- * `hourly_counter`
97
- * Key format: `model_name:id:field_name:yyyy-mm-ddThh`
98
- * `minutely_counter`
99
- * Key format: `model_name:id:field_name:yyyy-mm-ddThh:mi`
88
+ - `annual_counter`
89
+ - Key format: `model_name:id:field_name:yyyy`
90
+ - Redis is a highly volatile key-value store, so I don't recommend using it.
91
+ - `monthly_counter`
92
+ - Key format: `model_name:id:field_name:yyyy-mm`
93
+ - `weekly_counter`
94
+ - Key format: `model_name:id:field_name:yyyyWw`
95
+ - `daily_counter`
96
+ - Key format: `model_name:id:field_name:yyyy-mm-dd`
97
+ - `hourly_counter`
98
+ - Key format: `model_name:id:field_name:yyyy-mm-ddThh`
99
+ - `minutely_counter`
100
+ - Key format: `model_name:id:field_name:yyyy-mm-ddThh:mi`
100
101
 
101
- ### Daily Set
102
+ ### Daily Hashes
103
+
104
+ The daily hash also automatically switches the save destination when the date changes.
105
+
106
+ ```rb
107
+ # 2021-04-01
108
+ homepage.browsing_history.incr('item1')
109
+ homepage.browsing_history.incr('item2')
110
+ homepage.browsing_history.incr('item2')
111
+ puts homepage.browsing_history.all # { 'item1' => '1', 'item2' => '2' }
112
+
113
+ # 2021-04-02 (next day)
114
+ puts homepage.browsing_history.all # {}
115
+
116
+ homepage.browsing_history.bulk_set('item1' => 3, 'item3' => 5)
117
+ puts homepage.browsing_history.all # { 'item1' => '3', 'item3' => '5' }
118
+
119
+ # 2021-04-03 (next day)
120
+ homepage.browsing_history.incr('item2')
121
+ homepage.browsing_history.incr('item4')
122
+ puts homepage.browsing_history.all # { 'item2' => '1', 'item4' => '1' }
123
+
124
+ homepage.browsing_history[Date.new(2021, 4, 1)] # => { 'item1' => '1', 'item2' => '2' }
125
+ homepage.browsing_history[Date.new(2021, 4, 1), 3] # => { 'item1' => '4', 'item2' => '3', 'item3' => '5', 'item4' => '1' }
126
+ homepage.browsing_history[Date.new(2021, 4, 1)..Date.new(2021, 4, 2)] # => { 'item1' => '4', 'item2' => '2', 'item3' => '5' }
127
+
128
+ homepage.browsing_history.delete_at(Date.new(2021, 4, 1))
129
+ homepage.browsing_history.range(Date.new(2021, 4, 1), Date.new(2021, 4, 3)) # => { 'item1' => '3', 'item2' => '1', 'item3' => '5', 'item4' => '1' }
130
+ homepage.browsing_history.at(Date.new(2021, 4, 2)) # => #<Redis::HashKey key="homepage:1:browsing_history:2021-04-02">
131
+ homepage.browsing_history.at(Date.new(2021, 4, 2)).all # { 'item1' => '3', 'item3' => '5' }
132
+ ```
133
+
134
+ #### Daily Hashes Family
135
+
136
+ - `annual_hash_key`
137
+ - Key format: `model_name:id:field_name:yyyy`
138
+ - Redis is a highly volatile key-value store, so I don't recommend using it.
139
+ - `monthly_hash_key`
140
+ - Key format: `model_name:id:field_name:yyyy-mm`
141
+ - `weekly_hash_key`
142
+ - Key format: `model_name:id:field_name:yyyyWw`
143
+ - `daily_hash_key`
144
+ - Key format: `model_name:id:field_name:yyyy-mm-dd`
145
+ - `hourly_hash_key`
146
+ - Key format: `model_name:id:field_name:yyyy-mm-ddThh`
147
+ - `minutely_hash_key`
148
+ - Key format: `model_name:id:field_name:yyyy-mm-ddThh:mi`
149
+
150
+ ### Daily Sets
102
151
 
103
152
  The daily set also automatically switches the save destination when the date changes.
104
153
 
@@ -130,22 +179,21 @@ homepage.dau.at(Date.new(2021, 4, 2)) # => #<Redis::Set key="homepage:1:dau:2021
130
179
  homepage.dau.at(Date.new(2021, 4, 2)).members # ['user2', 'user3']
131
180
  ```
132
181
 
133
- #### Daily Set Family
134
-
135
- * `annual_set`
136
- * Key format: `model_name:id:field_name:yyyy`
137
- * Redis is a highly volatile key-value store, so I don't recommend using it.
138
- * `monthly_set`
139
- * Key format: `model_name:id:field_name:yyyy-mm`
140
- * `weekly_set`
141
- * Key format: `model_name:id:field_name:yyyyWw`
142
- * `daily_set`
143
- * Key format: `model_name:id:field_name:yyyy-mm-dd`
144
- * `hourly_set`
145
- * Key format: `model_name:id:field_name:yyyy-mm-ddThh`
146
- * `minutely_set`
147
- * Key format: `model_name:id:field_name:yyyy-mm-ddThh:mi`
148
-
182
+ #### Daily Sets Family
183
+
184
+ - `annual_set`
185
+ - Key format: `model_name:id:field_name:yyyy`
186
+ - Redis is a highly volatile key-value store, so I don't recommend using it.
187
+ - `monthly_set`
188
+ - Key format: `model_name:id:field_name:yyyy-mm`
189
+ - `weekly_set`
190
+ - Key format: `model_name:id:field_name:yyyyWw`
191
+ - `daily_set`
192
+ - Key format: `model_name:id:field_name:yyyy-mm-dd`
193
+ - `hourly_set`
194
+ - Key format: `model_name:id:field_name:yyyy-mm-ddThh`
195
+ - `minutely_set`
196
+ - Key format: `model_name:id:field_name:yyyy-mm-ddThh:mi`
149
197
 
150
198
  ### Timezone
151
199
 
data/bin/console CHANGED
@@ -4,8 +4,7 @@
4
4
  require 'bundler/setup'
5
5
  require 'redis-objects-daily-counter'
6
6
 
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
7
+ Redis::Objects.redis = Redis.new(host: 'redis', port: 6379)
9
8
 
10
9
  # (If you use this, don't forget to add pry to your Gemfile!)
11
10
  # require "pry"
@@ -19,5 +19,9 @@ class Redis
19
19
  def delete_from_redis(key)
20
20
  redis.del(key)
21
21
  end
22
+
23
+ def empty_value
24
+ []
25
+ end
22
26
  end
23
27
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Redis
4
+ module BaseHashKeyObject
5
+ private
6
+
7
+ def get_redis_object(key)
8
+ Redis::HashKey.new(key)
9
+ end
10
+
11
+ def get_value_from_redis(key)
12
+ h = redis.hgetall(key) || {}
13
+ h.each { |k, v| h[k] = unmarshal(v, options[:marshal_keys][k]) }
14
+ h
15
+ end
16
+
17
+ def get_values_from_redis(keys) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
18
+ keys.inject({}) do |memo, key|
19
+ memo.merge(get_value_from_redis(key)) do |_, self_val, other_val|
20
+ values = [self_val, other_val]
21
+ if values.all? { |val| val =~ /\A\d+\z/ }
22
+ values.sum(&:to_i).to_s
23
+ elsif values.all? { |val| val =~ /\A\d+(\.\d+)?\z/ }
24
+ values.sum(&:to_f).to_s
25
+ else
26
+ values.join(',')
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def delete_from_redis(key)
33
+ redis.del(key)
34
+ end
35
+
36
+ def empty_value
37
+ {}
38
+ end
39
+ end
40
+ end
@@ -20,5 +20,9 @@ class Redis
20
20
  def delete_from_redis(key)
21
21
  redis.del(key)
22
22
  end
23
+
24
+ def empty_value
25
+ []
26
+ end
23
27
  end
24
28
  end
@@ -4,7 +4,7 @@ class Redis
4
4
  module Objects
5
5
  module Daily
6
6
  module Counter
7
- VERSION = '0.3.0'
7
+ VERSION = '0.4.0'
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis/periodical_counter'
4
+
5
+ Redis::PERIODICALS.each do |periodical| # rubocop:disable Metrics/BlockLength
6
+ new_module = Module.new
7
+ new_module.module_eval <<~RUBY, __FILE__, __LINE__ + 1
8
+ def self.included(klass)
9
+ klass.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def #{periodical}_counter(name, options = {})
14
+ options[:start] ||= 0
15
+ options[:type] ||= (options[:start]).zero? ? :increment : :decrement
16
+ redis_objects[name.to_sym] = options.merge(type: :counter)
17
+
18
+ mod = Module.new do
19
+ define_method(name) do
20
+ Redis::#{periodical.capitalize}Counter.new(
21
+ redis_field_key(name), redis_field_redis(name), redis_options(name)
22
+ )
23
+ end
24
+ end
25
+
26
+ if options[:global]
27
+ extend mod
28
+
29
+ # dispatch to class methods
30
+ define_method(name) do
31
+ self.class.public_send(name)
32
+ end
33
+ else
34
+ include mod
35
+ end
36
+ end
37
+ end
38
+ RUBY
39
+ Redis::Objects.const_set("#{periodical.capitalize}Counters", new_module)
40
+ end