rk 1.0.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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rk.rb +56 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 08b7982550fff000dda262a080a8aa2e06928062
4
+ data.tar.gz: 07ac2573cdad17788e75b5ff5bc659992465a0d2
5
+ SHA512:
6
+ metadata.gz: 5594dd8895519189ad01c218882d3b62abf06374bcd15686e1de142d3e37140b6495abc7e2f5505f0c50bf34ac2dd8b63bc9e98d92edb7984094be0f4e023ae4
7
+ data.tar.gz: bbc4d2fcecc80530f92e0e2fa49dba41549e3e3203ff53301736c698fd27dd998ceb26ea3580ac9ef3ec59a52befdc35da49aad73e086f98073ae6e86898bf0a
@@ -0,0 +1,56 @@
1
+ # Module for including "rk" to global namespace (class Object)
2
+ module Rk
3
+
4
+ # Build Redis keys of several elements, for example rk("user", 10) => "user:10"
5
+ class Rk
6
+ attr_accessor :separator, :prefix, :suffix
7
+
8
+ # Set defaults, separator to ":", prefix/suffix is empty
9
+ def initialize
10
+ @separator = ":"
11
+ @prefix = ""
12
+ @suffix = ""
13
+ end
14
+
15
+ # Build a string including prefix/suffix, joining all elements with the separator
16
+ def rk(*elements)
17
+ ([@prefix] + elements + [@suffix]).reject { |element| element.to_s.empty? }.join(@separator)
18
+ end
19
+
20
+ # Add methods dynamically to define/access elements, for example
21
+ # rk.user = "user"; rk(rk.user, 10) => "user:10"
22
+ def method_missing(method, *arg)
23
+ # Raise an error if a method gets accessed which was not defined before
24
+ raise RuntimeError, "'rk.#{method.to_s}' is undefined" unless method.to_s.include?("=")
25
+
26
+ # Define a method to return a value as set, for example rk.settings = 'set' defines
27
+ # a method called "settings" which returns the string "set"
28
+ instance_eval(%Q[
29
+ def #{method.to_s.sub(/=/, "")}
30
+ "#{(arg || []).first}"
31
+ end
32
+ ])
33
+ end
34
+ end
35
+
36
+ # Create and call a global instance of Rk to either build a key or set/get attributes
37
+ def rk(*elements)
38
+ $_rk = Rk.new unless $_rk
39
+
40
+ # Differ between calling "rk" without/with params
41
+ if elements.empty?
42
+ # Return instance of Rk for calling methods like "rk.separator"
43
+ $_rk
44
+ else
45
+ # Return key build by Rk.rk
46
+ $_rk.rk(elements)
47
+ end
48
+ end
49
+
50
+ end # module Rk
51
+
52
+ # We could use "extend Rk" to extend only "main", but we would have to "include Rk" in
53
+ # any class, hence we include Rk to the global object.
54
+ class Object
55
+ include Rk
56
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Mensinger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby helper to generate keys for Redis
14
+ email: oliver.mensinger@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rk.rb
20
+ homepage: https://github.com/omensinger/rk
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Redis key builder
44
+ test_files: []