valkey-objects 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0812801baee7485f097e11e3edeccc34cbd6dfbf8f057db3193141e6a7a936c0'
4
- data.tar.gz: c2865f06b3a7d8032ca23451b5fd0bb0e49de3ae331e1f0106f051eb471a960d
3
+ metadata.gz: c0a9c6aca1b5339587ab8d14cba5dee0fa05f0ffdb3460fd349282d384c76038
4
+ data.tar.gz: 4d542d22a756ae8ce4dff600aae73f6f9a4cc41deb0db7abe91b6395a042fb02
5
5
  SHA512:
6
- metadata.gz: f5d83247f95f62b696d0fcb5ce26376e9cc8e83b76d268fe92c24df4eedc5787f3622fd0601ad7368a02b86b49789c7774040cf7cebd02f54859560d42afc8bf
7
- data.tar.gz: 9a29d72d9b648fbf8f5ff3d9e8dad53b8436a844d58411e0e5bf492abaae17b524b621e7cbae227665a5b9d11b8729156dec5511a6da92259adfebced73fc7df
6
+ metadata.gz: 50cff3351928a9b7f66f871f9fdab25f65646e7b4978eb981f1e9671babe40472e95a1d8e5c8121aa285cc64fb6148ee8488af852b8de9b87ed17338274e1bbc
7
+ data.tar.gz: e823ed4e7c79546888de0e527384fb02860c992b8ac5ad8c09072ba9ba9d461c312cb64dfc7867a60dda0aca55ca0aee889a6fc0e1c5db2db222da5da403be9a
data/#class.rb# ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'valkey/objects'
4
+
5
+ require 'pry'
6
+
7
+ class Game
8
+ include VK
9
+ sortedset :points
10
+ queue :log
11
+ def initialize k
12
+ @id = k
13
+ end
14
+ def score p, h={ points: 1 }
15
+ self.points.poke p, h[:points]
16
+ end
17
+ end
18
+
19
+ @game = Hash.new { |h,k| h[k] = Game.new(k) }
20
+
21
+ Pry.start
data/README.md CHANGED
@@ -1,39 +1,97 @@
1
- # Valkey::Objects
2
-
3
- TODO: Delete this and the text below, and describe your gem
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/valkey/objects`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- ## Installation
8
-
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
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
-
15
- If bundler is not being used to manage dependencies, install the gem by executing:
16
-
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Development
24
-
25
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
-
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
-
29
- ## Contributing
30
-
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/valkey-objects. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/valkey-objects/blob/master/CODE_OF_CONDUCT.md).
32
-
33
- ## License
34
-
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
-
37
- ## Code of Conduct
38
-
39
- Everyone interacting in the Valkey::Objects project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/valkey-objects/blob/master/CODE_OF_CONDUCT.md).
1
+ # ValKey Objects
2
+ Based upon the redis-objects library, VK allows for ValKey backed ruby objects.
3
+
4
+ ## installation
5
+ ```
6
+ gem install valkey-objects
7
+ ```
8
+
9
+ ## usage
10
+
11
+ ### simple
12
+ ```
13
+ require 'valkey/objects'
14
+ class ValKey
15
+ # 1. include valkey-objects layer
16
+ include VK
17
+ # 2. stitch your object together.
18
+ value :myvalue
19
+ counter :mycounter
20
+ hashkey :myhash
21
+ sortedset :mysortedset
22
+ set :myset
23
+ queue :myqueue
24
+ place :myplace
25
+ pipe :mypipe
26
+ toggle :mytoggle
27
+ # 3. define @id in initialize.
28
+ def initialize k
29
+ @id = k
30
+ end
31
+ # other stuff...
32
+ end
33
+
34
+ @x = ValKey.new("My Special Valkey object.")
35
+ @x.mypipe.on { |msg| puts %[MSG]; ap msg }
36
+ @x.mypipe << "Pipe Connected!"
37
+ @x.myvalue.value = "Hello, World"
38
+ @x.mycounter.value = 1.2345
39
+ @x.myhash[:key] = "Value"
40
+ @x.mysortedset["my other key"] = 9.8
41
+ @x.mysortedset.poke "my key", @x.mysortedset["my other key"]
42
+ @x.mysortedset.value { |i, e| puts %[Sorted Sets: i: #{i} e: #{e}] }
43
+ @x.myset << "my member"
44
+ @x.myset << "my new member"
45
+ h = @x.myset[/ new /]
46
+ @x.myset.value { |i, e| puts %[Sets: i: #{i} e: #{e}] }
47
+ @x.myplace.add "Palermo", 13.361389, 38.115556
48
+ @x.myplace.add "Catania", 15.087269, 37.502669
49
+ distance = @x.myplace.distance "Palermo", "Catania"
50
+ places = @x.myplace.radius 15.087269, 37.502669, 5000
51
+ @x.myplace.value { |i, e| puts %[Places: i: #{i} e: #{e}] }
52
+ ```
53
+
54
+ ### advanced
55
+ ```
56
+ class Game
57
+ include VK
58
+ sortedset :points
59
+ def initialize k
60
+ @id = k
61
+ end
62
+ def score p, h={ points: 1 }
63
+ self.points.poke p, h[:points]
64
+ end
65
+ end
66
+
67
+ @game = Hash.new { |h,k| h[k] = Game.new(k) }
68
+ ```
69
+
70
+ ### modular
71
+ ```
72
+ module X
73
+ @@X = Hash.new { |h,k| h[k] = Ex.new(k) }
74
+ class Ex
75
+ include VK
76
+ set :stuff
77
+ pipe :ear
78
+ def initialize k
79
+ @id = k
80
+ end
81
+ end
82
+ def self.keys
83
+ @@X.keys
84
+ end
85
+ def self.[] k
86
+ if !@@X.has_key?(k)
87
+ @@X[k].ear.on { |msg| puts "MSG[#{k}]:"; ap msg }
88
+ end
89
+ @@X[k]
90
+ end
91
+ end
92
+
93
+ X['Aaa'].ear << %[A]
94
+ X['Bbb'].ear << %[B]
95
+ X['Ccc'].ear << %[C]
96
+
97
+ ```
data/class.rb~ ADDED
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'valkey/objects'
4
+
5
+ # Using ValKey Objects
6
+ ## Create your class object
7
+ class ValKey
8
+ # 1. include valkey-objects layer
9
+ include VK
10
+ # 2. stitch your object together.
11
+ value :myvalue
12
+ counter :mycounter
13
+ hashkey :myhash
14
+ sortedset :mysortedset
15
+ set :myset
16
+ queue :myqueue
17
+ place :myplace
18
+ pipe :mypipe
19
+ toggle :mytoggle
20
+ # 3. define @id in initialize.
21
+ def initialize k
22
+ @id = k
23
+ end
24
+ # other stuff...
25
+ end
26
+
27
+ ## then user it.
28
+ puts %[=== Valkey Object Test ===]
29
+ @x = ValKey.new("My Special Valkey object.")
30
+ #### pub/sub pipe
31
+ ##### on message
32
+ @x.mypipe.on { |msg| puts %[MSG]; ap msg }
33
+ ##### publish message
34
+ @x.mypipe << "Pipe Connected!"
35
+ ### get/set string value...
36
+ @x.myvalue.value = "Hello, World"
37
+ ### get/set/incr/decr float value...
38
+ @x.mycounter.value = 1.2345
39
+ ### get/set key/value pairs...
40
+ @x.myhash[:key] = "Value"
41
+ ### sort keys by score...
42
+ #### set
43
+ @x.mysortedset["my other key"] = 9.8
44
+ #### poke/get
45
+ @x.mysortedset.poke "my key", @x.mysortedset["my other key"]
46
+ #### by high score
47
+ @x.mysortedset.value { |i, e| ap %[Sorted Sets: i: #{i} e: #{e}] }
48
+ ### collect keys...
49
+ @x.myset << "my member"
50
+ @x.myset << "my new member"
51
+ #### filter by regexp...
52
+ h = @x.myset[/ new /]
53
+ puts %[Filtered set members:]
54
+ ap h
55
+ #### each collection key....
56
+ @x.myset.value { |i, e| puts %[Sets: i: #{i} e: #{e}] }
57
+ ### points by coordinates...
58
+ @x.myplace.add "Palermo", 13.361389, 38.115556
59
+ @x.myplace.add "Catania", 15.087269, 37.502669
60
+ #### distance between points.
61
+ distance = @x.myplace.distance "Palermo", "Catania"
62
+ ap %[The meters between the points: #{distance}]
63
+ #### places within radius by coordinates
64
+ places = @x.myplace.radius 15.087269, 37.502669, 5000
65
+ ap %[The places within 5000 meters of the coordinates: #{places}]
66
+ puts %[Place Values...]
67
+ #### collection of places...
68
+ @x.myplace.value { |i, e| puts %[Places: i: #{i} e: #{e}] }
69
+ ap VK['*']
70
+
71
+
72
+
73
+ ## Or as a collection of "X"s...
74
+ module X
75
+ @@X = Hash.new { |h,k| h[k] = Ex.new(k) }
76
+ class Ex
77
+ include VK
78
+ set :stuff
79
+ pipe :ear
80
+ def initialize k
81
+ @id = k
82
+ end
83
+ end
84
+ def self.keys
85
+ @@X.keys
86
+ end
87
+ def self.[] k
88
+ if !@@X.has_key?(k)
89
+ @@X[k].ear.on { |msg| puts "MSG[#{k}]:"; ap msg }
90
+ end
91
+ @@X[k]
92
+ end
93
+ end
94
+
95
+ @x[:a] = X['Aaa']
96
+ @x[:b] = X['Bbb']
97
+ @x[:c] = X['Ccc']
data/example.rb~ ADDED
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'valkey/objects'
4
+
5
+ # Using ValKey Objects
6
+ ## Create your class object
7
+ class ValKey
8
+ # 1. include valkey-objects layer
9
+ include VK
10
+ # 2. stitch your object together.
11
+ value :myvalue
12
+ counter :mycounter
13
+ hashkey :myhash
14
+ sortedset :mysortedset
15
+ set :myset
16
+ queue :myqueue
17
+ place :myplace
18
+ pipe :mypipe
19
+ toggle :mytoggle
20
+ # 3. define @id in initialize.
21
+ def initialize k
22
+ @id = k
23
+ end
24
+ # other stuff...
25
+ end
26
+
27
+
28
+
29
+ require 'awesome_print'
30
+
31
+ @x = {}
32
+
33
+ ## then user it.
34
+ puts %[=== Valkey Object Test ===]
35
+ @x[:x] = ValKey.new("My Special Valkey object.")
36
+ #### pub/sub pipe
37
+ ##### on message
38
+ @x[:x].mypipe.on { |msg| puts %[MSG]; ap msg }
39
+ ##### publish message
40
+ @x[:x].mypipe << "Pipe Connected!"
41
+ ### get/set string value...
42
+ @x[:x].myvalue.value = "Hello, World"
43
+ ### get/set/incr/decr float value...
44
+ @x[:x].mycounter.value = 1.2345
45
+ ### get/set key/value pairs...
46
+ @x[:x].myhash[:key] = "Value"
47
+ ### sort keys by score...
48
+ #### set
49
+ @x[:x].mysortedset["my other key"] = 9.8
50
+ #### poke/get
51
+ @x[:x].mysortedset.poke "my key", @x[:x].mysortedset["my other key"]
52
+ #### by high score
53
+ @x[:x].mysortedset.value { |i, e| ap %[Sorted Sets: i: #{i} e: #{e}] }
54
+ ### collect keys...
55
+ @x[:x].myset << "my member"
56
+ @x[:x].myset << "my new member"
57
+ #### filter by regexp...
58
+ h = @x[:x].myset[/ new /]
59
+ puts %[Filtered set members:]
60
+ ap h
61
+ #### each collection key....
62
+ @x[:x].myset.value { |i, e| puts %[Sets: i: #{i} e: #{e}] }
63
+ ### points by coordinates...
64
+ @x[:x].myplace.add "Palermo", 13.361389, 38.115556
65
+ @x[:x].myplace.add "Catania", 15.087269, 37.502669
66
+ #### distance between points.
67
+ distance = @x[:x].myplace.distance "Palermo", "Catania"
68
+ ap %[The meters between the points: #{distance}]
69
+ #### places within radius by coordinates
70
+ places = @x[:x].myplace.radius 15.087269, 37.502669, 5000
71
+ ap %[The places within 5000 meters of the coordinates: #{places}]
72
+ puts %[Place Values...]
73
+ #### collection of places...
74
+ @x[:x].myplace.value { |i, e| puts %[Places: i: #{i} e: #{e}] }
75
+ ap VK['*']
76
+
77
+ ## Or as a collection of "X"s...
78
+ module X
79
+ @@X = Hash.new { |h,k| h[k] = Ex.new(k) }
80
+ class Ex
81
+ include VK
82
+ set :stuff
83
+ pipe :ear
84
+ def initialize k
85
+ @id = k
86
+ end
87
+ end
88
+ def self.keys
89
+ @@X.keys
90
+ end
91
+ def self.[] k
92
+ if !@@X.has_key?(k)
93
+ @@X[k].ear.on { |msg| puts "MSG[#{k}]:"; ap msg }
94
+ end
95
+ @@X[k]
96
+ end
97
+ end
98
+
99
+ @x[:a] = X['Aaa']
100
+ @x[:b] = X['Bbb']
101
+ @x[:c] = X['Ccc']
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Valkey
4
4
  module Objects
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
@@ -3,21 +3,157 @@
3
3
  require_relative "objects/version"
4
4
 
5
5
  require 'redis-client'
6
+ require 'json'
7
+ require 'ruby-duration'
8
+
6
9
  module VK
7
10
  def self.included(x)
8
11
  x.extend VK
9
12
  end
10
13
 
11
14
  def self.extended(x)
12
- define_method(:value) { |k| define_method(k.to_sym) { V.new(%[#{x}:V:#{k}:#{@id}]) } };
13
- define_method(:counter) { |k| define_method(k.to_sym) { C.new(%[#{x}:C:#{k}:#{@id}]); } };
14
- define_method(:hashkey) { |k| define_method(k.to_sym) { H.new(%[#{x}:H:#{k}:#{@id}]); } };
15
- define_method(:sortedset) { |k| define_method(k.to_sym) { S.new(%[#{x}:S:#{k}:#{@id}]); } };
16
- define_method(:set) { |k| define_method(k.to_sym) { G.new(%[#{x}:S:#{k}:#{@id}]); } };
17
- define_method(:queue) { |k| define_method(k.to_sym) { Q.new(%[#{x}:Q:#{k}:#{@id}]); } };
18
- define_method(:place) { |k| define_method(k.to_sym) { P.new(%[#{x}:P:#{k}:#{@id}]); } };
15
+ ##
16
+ # Example:
17
+ # class ExmpleObject
18
+ # include VK
19
+ # value :myValue
20
+ # counter :myCounter
21
+ # hashkey :myHashKey
22
+ # sortedset :mySortedSet
23
+ # set :mySet
24
+ # queue :myQueue
25
+ # place :myPlace
26
+ # pipe :myPipe
27
+ # toggle :myToggle
28
+ # def initialize k
29
+ # @id = k
30
+ # end
31
+ # end
32
+ #
33
+ # Instantiation: @obj = ExampleObject.new('object id...')
34
+ xx = x.name.gsub("::", "-")
35
+ ##
36
+ # value :myValue
37
+ # @obj.myValue => A string
38
+ # @obj.myValue.value = "my value"
39
+ # @obj.myValue.value => "my value"
40
+ define_method(:value) { |k| define_method(k.to_sym) { V.new(%[#{xx}:value:#{k}:#{@id}]) } };
41
+ ##
42
+ # counter :myCounter => A number
43
+ # @obj.myCounter
44
+ # @obj.myCounter.value = number
45
+ # @obj.myCounter.value => ...
46
+ # @obj.myCounter.incr number
47
+ # @obj.myCounter.decr number
48
+ #
49
+ define_method(:counter) { |k| define_method(k.to_sym) { C.new(%[#{xx}:counter:#{k}:#{@id}]); } };
50
+ ##
51
+ #
52
+ define_method(:timestamp) { |k| define_method(k.to_sym) { N.new(%[#{xx}:counter:#{k}:#{@id}]) } }
53
+ ##
54
+ # hashkey :myHashKey => The ubiquitous Ruby Hash in valkey/redis
55
+ # @obj.myHashKey
56
+ # @obj.myHashKey[:key] = value
57
+ # @obj.myHashKey[:key] => ...
58
+ #
59
+ define_method(:hashkey) { |k| define_method(k.to_sym) { H.new(%[#{xx}:hash:#{k}:#{@id}]); } };
60
+ ##
61
+ # sortedset :mySortedSet => A ranked collection of members
62
+ # @obj.mySortedSet
63
+ # @obj.mySortedSet[:key] = value
64
+ # @obj.mySortedSet[:key] => ...
65
+ # @obj.mySortedSet.value { |key, i| ... }
66
+ # @obj.mySortedSet.poke key, number
67
+ #
68
+ define_method(:sortedset) { |k| define_method(k.to_sym) { S.new(%[#{xx}:sortedset:#{k}:#{@id}]); } };
69
+ ##
70
+ # set :mySet => A collect of unique members
71
+ # @obj.mySet
72
+ # @obj.mySet << "x"
73
+ # @obj.myset.rm "x"
74
+ # @obj.mySet & @obj.otherSet
75
+ # @obj.mySet | @obj.otherSet
76
+ # @obj.myset["pattern"]
77
+ # @obj.mySet.value { |key, i| ... }
78
+ #
79
+ define_method(:set) { |k| define_method(k.to_sym) { G.new(%[#{xx}:set:#{k}:#{@id}]); } };
80
+ ##
81
+ # queue :myQueue => An array with push and pop utility
82
+ # @obj.myQueue
83
+ # @obj.myQueue << "x"
84
+ # @obj.myQueue.front => "x" and pop
85
+ # @obj.myQueue.value { |key, i| ... }
86
+ #
87
+ define_method(:queue) { |k| define_method(k.to_sym) { Q.new(%[#{xx}:queue:#{k}:#{@id}]); } };
88
+ ##
89
+ # place :myPlace => GPS Coordinates
90
+ # @obj.myPlace
91
+ # @obj.myPlace.add "key", longitude, latitude
92
+ # @obj.myPlace["key"] => { longitude: xx, latitude: yy }
93
+ # @obj.myPlace.distance "key", "other key"
94
+ # @obj.myPlace.radius longitude, latitude, distance
95
+ # @obj.myPlace.value { |key, i| ... }
96
+ #
97
+ define_method(:place) { |k| define_method(k.to_sym) { P.new(%[#{xx}:place:#{k}:#{@id}]); } };
98
+ ##
99
+ # pipe :myPipe => Subscibe and handle, and publish.
100
+ # @obj.myPipe
101
+ # @obj.myPipe.on { |msg| ... }
102
+ # @obj.myPipe << "input" => publish { input: "input" }
103
+ # @obj.myPipe << ["input","input"] => publish { inputs: ["input", "input"] }
104
+ # @obj.myPipe << {} => publish {}
105
+ #
106
+ define_method(:pipe) { |k| define_method(k.to_sym) { B.new(%[#{xx}:pipe:#{k}:#{@id}]); } };
107
+ ##
108
+ # toggle :myToggle => Boolean toggle.
109
+ # @obj.myToggle
110
+ # @obj.value = bool
111
+ # @obj.value => ...
112
+ # @obj.value! => value = !value
113
+ #
114
+ define_method(:toggle) { |k| define_method(k.to_sym) { T.new(%[#{xx}:toggle:#{k}:#{@id}]); } };
115
+ end
116
+
117
+ def id
118
+ @id
19
119
  end
20
120
 
121
+ module AGO
122
+ class Error < StandardError; end
123
+ class Clock
124
+ def initialize t
125
+ @t = AGO.now
126
+ @s = @t.to_i - t.to_i
127
+ @t = Time.new(t.to_i).utc
128
+ @d = Duration.new(@s.abs)
129
+ end
130
+ def to_i
131
+ @s
132
+ end
133
+ def to_s *s
134
+ if s[0]
135
+ @d.format(s[0])
136
+ else
137
+ @d.format('%w %~w %d %~d %H:%M:%S')
138
+ end
139
+ end
140
+ end
141
+ def self.now
142
+ Time.now.utc
143
+ end
144
+ def self.[] k
145
+ Clock.new(k)
146
+ end
147
+ end
148
+
149
+ def self.clock *t
150
+ if t[0]
151
+ AGO[t[0]]
152
+ else
153
+ AGO.now
154
+ end
155
+ end
156
+
21
157
  def self.redis
22
158
  RedisClient.config(host: "127.0.0.1", port: 6379, db: 0).new_client
23
159
  end
@@ -28,6 +164,60 @@ module VK
28
164
  @key = k
29
165
  end
30
166
  end
167
+
168
+ class N < O
169
+ def value
170
+ VK.redis.call("GET", key).to_i;
171
+ end
172
+ def value!
173
+ VK.redis.call("SET", key, "#{VK.clock.to_i}");
174
+ end
175
+ end
176
+
177
+ class T < O
178
+ def value
179
+ VK.redis.call("GET", key) == 'true' ? true : false
180
+ end
181
+ def value= x
182
+ VK.redis.call("SET", key, "#{x.to_s}")
183
+ end
184
+ def value!
185
+ if self.value
186
+ self.value = false
187
+ else
188
+ self.value = true
189
+ end
190
+ end
191
+ end
192
+
193
+ class B < O
194
+ def on &b
195
+ pubsub = VK.redis.pubsub
196
+ pubsub.call("SUBSCRIBE", key)
197
+ Process.detach( fork do
198
+ loop do
199
+ if m = pubsub.next_event(0)
200
+ cn, ty, na, id = key.split(":")
201
+ if m[0] == "message"
202
+ b.call({ stub: na, object: cn.gsub("-", "::"), type: ty, id: id, event: m[0], data: JSON.parse(m[2]) })
203
+ else
204
+ ap({ stub: na, object: cn.gsub("-", "::"), type: ty, id: id, event: m[0], data: m[2] })
205
+ end
206
+ end
207
+ end
208
+ end
209
+ );
210
+ end
211
+ def << x
212
+ if x.class == String
213
+ VK.redis.call("PUBLISH", key, JSON.generate({ input: x }))
214
+ elsif x.class == Array
215
+ VK.redis.call("PUBLISH", key, JSON.generate({ inputs: x }))
216
+ elsif x.class == Hash
217
+ VK.redis.call("PUBLISH", key, JSON.generate(x))
218
+ end
219
+ end
220
+ end
31
221
 
32
222
  class V < O
33
223
  def value
@@ -47,7 +237,10 @@ module VK
47
237
  end
48
238
  def value
49
239
  VK.redis.call("GET", key).to_f
50
- end
240
+ end
241
+ def value= n
242
+ VK.redis.call("SET", key, n.to_f)
243
+ end
51
244
  end
52
245
 
53
246
  class H < O
@@ -60,6 +253,9 @@ module VK
60
253
  end
61
254
 
62
255
  class Q < O
256
+ def value &b
257
+ VK.redis.call("LRANGE", key, 0, -1).each_with_index { |e, i| b.call(i, e) }
258
+ end
63
259
  def length
64
260
  VK.redis.call("LLEN", key)
65
261
  end
@@ -73,7 +269,7 @@ module VK
73
269
 
74
270
  class S < O
75
271
  def value &b
76
- VK.redis.call("ZREVRANGE", key, 0, -1, 'WITHSCORES').each_with_index { |e, i| b.call(i, e[0], e[1]) }
272
+ VK.redis.call("ZREVRANGE", key, 0, -1, 'WITHSCORES').each_with_index { |e, i| b.call(i, e) }
77
273
  end
78
274
  def [] k
79
275
  VK.redis.call("ZSCORE", key, k).to_f;
@@ -87,8 +283,8 @@ module VK
87
283
  end
88
284
 
89
285
  class G < O
90
- def value
91
- VK.redis.call("SMEMBERS", key)
286
+ def value &b
287
+ VK.redis.call("SMEMBERS", key).each_with_index { |e, i| b.call(i, e) }
92
288
  end
93
289
  def length
94
290
  VK.redis.call("SCARD", key)
@@ -105,11 +301,16 @@ module VK
105
301
  def | k
106
302
  VK.redis.call("SUNION", key, k.key)
107
303
  end
304
+ def [] k
305
+ r, h = Regexp.new(k), {}
306
+ VK.redis.call("SMEMBERS", key).each { |e| if m = r.match(e); h[e] = m; end; }
307
+ return h
308
+ end
108
309
  end
109
-
310
+
110
311
  class P < O
111
- def value
112
- VK.redis.call("ZRANGE", key, 0, -1);
312
+ def value &b
313
+ VK.redis.call("ZRANGE", key, 0, -1).each_with_index { |e, i| b.call(i, e) };
113
314
  end
114
315
  def add i, lon, lat
115
316
  VK.redis.call("GEOADD", key, lon, lat, i)
@@ -127,6 +328,11 @@ module VK
127
328
  return h
128
329
  end
129
330
  end
331
+ def self.flushdb!
332
+ VK.redis.call("FLUSHDB")
333
+ end
334
+ def self.[] k
335
+ VK.redis.call("KEYS", k)
336
+ end
130
337
  end
131
338
 
132
-
data/module.rb~ ADDED
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'valkey/objects'
4
+
5
+ # Using ValKey Objects
6
+ ## Create your class object
7
+ class ValKey
8
+ # 1. include valkey-objects layer
9
+ include VK
10
+ # 2. stitch your object together.
11
+ value :myvalue
12
+ counter :mycounter
13
+ hashkey :myhash
14
+ sortedset :mysortedset
15
+ set :myset
16
+ queue :myqueue
17
+ place :myplace
18
+ pipe :mypipe
19
+ toggle :mytoggle
20
+ # 3. define @id in initialize.
21
+ def initialize k
22
+ @id = k
23
+ end
24
+ # other stuff...
25
+ end
26
+
27
+
28
+
29
+ require 'awesome_print'
30
+
31
+ @x = {}
32
+
33
+ ## Or as a collection of "X"s...
34
+ module X
35
+ @@X = Hash.new { |h,k| h[k] = Ex.new(k) }
36
+ class Ex
37
+ include VK
38
+ set :stuff
39
+ pipe :ear
40
+ def initialize k
41
+ @id = k
42
+ end
43
+ end
44
+ def self.keys
45
+ @@X.keys
46
+ end
47
+ def self.[] k
48
+ if !@@X.has_key?(k)
49
+ @@X[k].ear.on { |msg| puts "MSG[#{k}]:"; ap msg }
50
+ end
51
+ @@X[k]
52
+ end
53
+ end
54
+
55
+ X['Aaa'].ear << %[A]
56
+ X['Bbb'].ear << %[B]
57
+ X['Ccc'].ear << %[C]
Binary file
Binary file
@@ -32,7 +32,8 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  # Uncomment to register a new dependency of your gem
34
34
  spec.add_dependency "redis-client"
35
-
35
+ spec.add_dependency "json"
36
+ spec.add_dependency "ruby-duration"
36
37
  # For more information and examples about making a new gem, check out our
37
38
  # guide at: https://bundler.io/guides/creating_gem.html
38
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valkey-objects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Olson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-18 00:00:00.000000000 Z
11
+ date: 2024-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-duration
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  description: A ruby valkey client inspired by the redis-objects gem.
28
56
  email:
29
57
  - xorgnak@gmail.com
@@ -31,14 +59,20 @@ executables: []
31
59
  extensions: []
32
60
  extra_rdoc_files: []
33
61
  files:
62
+ - "#class.rb#"
34
63
  - CHANGELOG.md
35
64
  - CODE_OF_CONDUCT.md
36
65
  - LICENSE.txt
37
66
  - README.md
38
67
  - Rakefile
68
+ - class.rb~
69
+ - example.rb~
39
70
  - lib/valkey/objects.rb
40
71
  - lib/valkey/objects/version.rb
72
+ - module.rb~
41
73
  - sig/valkey/objects.rbs
74
+ - valkey-objects-0.1.0.gem
75
+ - valkey-objects-0.1.1.gem
42
76
  - valkey-objects.gemspec
43
77
  homepage: https://github.com/xorgnak/valkey-client
44
78
  licenses: