# frozen_string_literal: true require 'valkey/objects' # Using ValKey Objects ## Create your class object class ValKey # 1. include valkey-objects layer include VK # 2. stitch your object together. value :myvalue counter :mycounter hashkey :myhash sortedset :mysortedset set :myset queue :myqueue place :myplace pipe :mypipe toggle :mytoggle # 3. define @id in initialize. def initialize k @id = k end # other stuff... end ## then user it. puts %[=== Valkey Object Test ===] @x = ValKey.new("My Special Valkey object.") #### pub/sub pipe ##### on message @x.mypipe.on { |msg| puts %[MSG]; ap msg } ##### publish message @x.mypipe << "Pipe Connected!" ### get/set string value... @x.myvalue.value = "Hello, World" ### get/set/incr/decr float value... @x.mycounter.value = 1.2345 ### get/set key/value pairs... @x.myhash[:key] = "Value" ### sort keys by score... #### set @x.mysortedset["my other key"] = 9.8 #### poke/get @x.mysortedset.poke "my key", @x.mysortedset["my other key"] #### by high score @x.mysortedset.value { |i, e| ap %[Sorted Sets: i: #{i} e: #{e}] } ### collect keys... @x.myset << "my member" @x.myset << "my new member" #### filter by regexp... h = @x.myset[/ new /] puts %[Filtered set members:] ap h #### each collection key.... @x.myset.value { |i, e| puts %[Sets: i: #{i} e: #{e}] } ### points by coordinates... @x.myplace.add "Palermo", 13.361389, 38.115556 @x.myplace.add "Catania", 15.087269, 37.502669 #### distance between points. distance = @x.myplace.distance "Palermo", "Catania" ap %[The meters between the points: #{distance}] #### places within radius by coordinates places = @x.myplace.radius 15.087269, 37.502669, 5000 ap %[The places within 5000 meters of the coordinates: #{places}] puts %[Place Values...] #### collection of places... @x.myplace.value { |i, e| puts %[Places: i: #{i} e: #{e}] } ap VK['*'] ## Or as a collection of "X"s... module X @@X = Hash.new { |h,k| h[k] = Ex.new(k) } class Ex include VK set :stuff pipe :ear def initialize k @id = k end end def self.keys @@X.keys end def self.[] k if !@@X.has_key?(k) @@X[k].ear.on { |msg| puts "MSG[#{k}]:"; ap msg } end @@X[k] end end @x[:a] = X['Aaa'] @x[:b] = X['Bbb'] @x[:c] = X['Ccc']