cacchern 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +0 -11
- data/Gemfile.lock +1 -1
- data/README.md +94 -1
- data/lib/cacchern.rb +2 -1
- data/lib/cacchern/object.rb +51 -0
- data/lib/cacchern/{value.rb → sortable_member.rb} +1 -1
- data/lib/cacchern/sorted_set.rb +2 -2
- data/lib/cacchern/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59d307f8d9365a1f5154a2584f58f0b74b09c88a19c5e58d9f7e205692e51d8c
|
4
|
+
data.tar.gz: 3b83aa1c03998c439e1ee63c0838563eb11e82268933ec53fa5c46d8d393d241
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 230469df9e4bb596d0dac5d15030cb00547b7abf98b818bd2ab9c58c1edc1402b457d6708374646e56d1a0ad02639c2c181f20ca9e4afd54c8c898d0f5592935
|
7
|
+
data.tar.gz: '025968b870076cb72b696049cc390049045b4d197f17cb2ff1e22d5fde86617cbaa3fdb755f98c66b202836a27e2084199c8dec5eab3cf7bba149e040460abbc'
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -10,19 +10,8 @@
|
|
10
10
|
# Configuration parameters: CountComments, ExcludedMethods.
|
11
11
|
# ExcludedMethods: refine
|
12
12
|
Metrics/BlockLength:
|
13
|
-
Max: 56
|
14
|
-
|
15
|
-
# Offense count: 6
|
16
|
-
Style/Documentation:
|
17
13
|
Exclude:
|
18
14
|
- 'spec/**/*'
|
19
|
-
- 'test/**/*'
|
20
|
-
- 'lib/cacchern.rb'
|
21
|
-
- 'lib/cacchern/incrementable_sorted_set.rb'
|
22
|
-
- 'lib/cacchern/member.rb'
|
23
|
-
- 'lib/cacchern/set.rb'
|
24
|
-
- 'lib/cacchern/sorted_set.rb'
|
25
|
-
- 'lib/cacchern/value.rb'
|
26
15
|
|
27
16
|
# Offense count: 6
|
28
17
|
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,100 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
1. Create classes
|
24
|
+
|
25
|
+
Create class that overrides Cacchern::Value
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Score < Cacchern::Value
|
29
|
+
validates :key, presence: true, numericality: { only_integer: true }
|
30
|
+
validates :value, presence: true, numericality: true
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
Create class that overrides Cacchern::Value
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class ScoreSet < Cacchern::SortedSet
|
38
|
+
contain_class Score
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
2. Add value
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
base_set = ScoreSet.new('base')
|
46
|
+
#=> #<ScoreSet:0x000056544559a520 @key="score_set:base">
|
47
|
+
score = Score.new(1, 100)
|
48
|
+
#=> #<Score:0x00005654456194b0
|
49
|
+
# @errors=#<ActiveModel::Errors:0x0000565445619050 @base=#<Score:0x00005654456194b0 ...>, @details={}, @messages={}>,
|
50
|
+
# @key=1,
|
51
|
+
# @validation_context=nil,
|
52
|
+
# @value=100>
|
53
|
+
base_set.add(score)
|
54
|
+
base_set.add(Score.new(2,50))
|
55
|
+
|
56
|
+
base_set.add_all([Score.new(3,50),Score.new(4,40)])
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
3. Get Values
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
base_set.order(:desc)
|
64
|
+
#=> [#<Score:0x000056544570ebb8
|
65
|
+
# @errors=#<ActiveModel::Errors:0x000056544570ea28 @base=#<Score:0x000056544570ebb8 ...>, @details={}, @messages={}>,
|
66
|
+
# @key="1",
|
67
|
+
# @validation_context=nil,
|
68
|
+
# @value=100.0>,
|
69
|
+
# #<Score:0x000056544570e230
|
70
|
+
# @errors=#<ActiveModel::Errors:0x000056544570e0a0 @base=#<Score:0x000056544570e230 ...>, @details={}, @messages={}>,
|
71
|
+
# @key="2",
|
72
|
+
# @validation_context=nil,
|
73
|
+
# @value=50.0>]
|
74
|
+
```
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
base_set.order(:asc)
|
78
|
+
#=> [#<Score:0x000056544579aaa0
|
79
|
+
# @errors=#<ActiveModel::Errors:0x000056544579a910 @base=#<Score:0x000056544579aaa0 ...>, @details={}, @messages={}>,
|
80
|
+
# @key="2",
|
81
|
+
# @validation_context=nil,
|
82
|
+
# @value=50.0>,
|
83
|
+
# #<Score:0x000056544579a168
|
84
|
+
# @errors=#<ActiveModel::Errors:0x0000565445799fd8 @base=#<Score:0x000056544579a168 ...>, @details={}, @messages={}>,
|
85
|
+
# @key="1",
|
86
|
+
# @validation_context=nil,
|
87
|
+
# @value=100.0>]
|
88
|
+
```
|
89
|
+
|
90
|
+
4. Remove value
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
base_set.remove(2)
|
94
|
+
#=> true
|
95
|
+
base_set.order(:asc)
|
96
|
+
#=> [#<Score:0x0000565445850990
|
97
|
+
# @errors=#<ActiveModel::Errors:0x0000565445850800 @base=#<Score:0x0000565445850990 ...>, @details={}, @messages={}>,
|
98
|
+
# @key="1",
|
99
|
+
# @validation_context=nil,
|
100
|
+
# @value=100.0>]
|
101
|
+
base_set.remove_all
|
102
|
+
```
|
103
|
+
|
104
|
+
- IncrementableSortedSet
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
class IScoreSet < Cacchern::IncrementableSortedSet
|
108
|
+
contain_class Score
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
base_set = IScoreSet.new('base')
|
114
|
+
base_set.add(Score.new(2,50))
|
115
|
+
base_set.increment(Score.new(2,50))
|
116
|
+
```
|
24
117
|
|
25
118
|
## Development
|
26
119
|
|
data/lib/cacchern.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cacchern
|
4
|
+
class Object
|
5
|
+
include ActiveModel::Model
|
6
|
+
|
7
|
+
attr_reader :id
|
8
|
+
attr_reader :key
|
9
|
+
attr_reader :value
|
10
|
+
|
11
|
+
define_model_callbacks :save
|
12
|
+
before_save { throw(:abort) unless valid? }
|
13
|
+
|
14
|
+
def initialize(id, value)
|
15
|
+
@id = id
|
16
|
+
@key = "#{self.class.name.underscore}:#{id}"
|
17
|
+
@value = value
|
18
|
+
end
|
19
|
+
|
20
|
+
def save
|
21
|
+
valid? ? create_or_update : false
|
22
|
+
end
|
23
|
+
|
24
|
+
def save!
|
25
|
+
valid? ? create_or_update : raise_validation_error
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete
|
29
|
+
Redis.current.del(key)
|
30
|
+
end
|
31
|
+
|
32
|
+
class << self
|
33
|
+
def key(id)
|
34
|
+
"#{name.underscore}:#{id}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def find(id)
|
38
|
+
key = self.key(id)
|
39
|
+
value = Redis.current.get(key)
|
40
|
+
|
41
|
+
new(id, value) if value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def create_or_update
|
48
|
+
Redis.current.set(key, value) == 'OK'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/cacchern/sorted_set.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'cacchern/
|
3
|
+
require 'cacchern/sortable_member'
|
4
4
|
|
5
5
|
module Cacchern
|
6
6
|
class SortedSet
|
@@ -12,7 +12,7 @@ module Cacchern
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def value_class
|
15
|
-
@value_class ||=
|
15
|
+
@value_class ||= SortableMember
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
data/lib/cacchern/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cacchern
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shuhei TAKASUGI
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -90,9 +90,10 @@ files:
|
|
90
90
|
- lib/cacchern.rb
|
91
91
|
- lib/cacchern/incrementable_sorted_set.rb
|
92
92
|
- lib/cacchern/member.rb
|
93
|
+
- lib/cacchern/object.rb
|
93
94
|
- lib/cacchern/set.rb
|
95
|
+
- lib/cacchern/sortable_member.rb
|
94
96
|
- lib/cacchern/sorted_set.rb
|
95
|
-
- lib/cacchern/value.rb
|
96
97
|
- lib/cacchern/version.rb
|
97
98
|
- redis.yml.erb
|
98
99
|
homepage: https://github.com/shuyuhey/cacchern
|