rk 1.0.0 → 1.1.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 +4 -4
  2. data/lib/rk.rb +40 -12
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08b7982550fff000dda262a080a8aa2e06928062
4
- data.tar.gz: 07ac2573cdad17788e75b5ff5bc659992465a0d2
3
+ metadata.gz: 072511d6420663d7e00bfcfd1ad0fad43b589333
4
+ data.tar.gz: 1e3407fb1d77308adb7f783ddddd8a3df046640e
5
5
  SHA512:
6
- metadata.gz: 5594dd8895519189ad01c218882d3b62abf06374bcd15686e1de142d3e37140b6495abc7e2f5505f0c50bf34ac2dd8b63bc9e98d92edb7984094be0f4e023ae4
7
- data.tar.gz: bbc4d2fcecc80530f92e0e2fa49dba41549e3e3203ff53301736c698fd27dd998ceb26ea3580ac9ef3ec59a52befdc35da49aad73e086f98073ae6e86898bf0a
6
+ metadata.gz: 3bef8fd553c1c3f1a0befe58c20af4c86b7b741296af168043218147ade21b7b2cecafffec8b4651efac3d4a7f4456b2d9c14b93c055225bbb05de9c6c3c0418
7
+ data.tar.gz: c88add90b37662301a02f22e471eabdb8bf327a4f1153c841d70e5674ab3bcd21e75420a74d3dd3b6d5873f3acfd3731fec2c38d73f792139a8d6d6696adbc1f
data/lib/rk.rb CHANGED
@@ -1,39 +1,67 @@
1
- # Module for including "rk" to global namespace (class Object)
1
+ # Module for including +rk+ to global namespace (class Object).
2
2
  module Rk
3
3
 
4
- # Build Redis keys of several elements, for example rk("user", 10) => "user:10"
4
+ # Build Redis keys of several elements.
5
+ # rk("user", 10) => "user:10"
5
6
  class Rk
6
- attr_accessor :separator, :prefix, :suffix
7
+ attr_accessor :separator, :prefix, :suffix, :keys
7
8
 
8
- # Set defaults, separator to ":", prefix/suffix is empty
9
+ # Set defaults
10
+ # * <tt>separator</tt> - :
11
+ # * <tt>prefix</tt> - empty
12
+ # * <tt>suffix</tt> - empty
13
+ # * <tt>keys</tt> - empty
9
14
  def initialize
10
15
  @separator = ":"
11
16
  @prefix = ""
12
17
  @suffix = ""
18
+ @keys = {}
13
19
  end
14
20
 
15
- # Build a string including prefix/suffix, joining all elements with the separator
21
+ # Build a string including prefix/suffix, joining all elements with the separator.
16
22
  def rk(*elements)
17
23
  ([@prefix] + elements + [@suffix]).reject { |element| element.to_s.empty? }.join(@separator)
18
24
  end
19
25
 
20
- # Add methods dynamically to define/access elements, for example
21
- # rk.user = "user"; rk(rk.user, 10) => "user:10"
26
+ # Add methods dynamically to define/access elements.
27
+ # rk.user = "user"
28
+ # rk(rk.user, 10) => "user:10"
22
29
  def method_missing(method, *arg)
23
30
  # Raise an error if a method gets accessed which was not defined before
24
31
  raise RuntimeError, "'rk.#{method.to_s}' is undefined" unless method.to_s.include?("=")
25
32
 
26
33
  # Define a method to return a value as set, for example rk.settings = 'set' defines
27
34
  # a method called "settings" which returns the string "set"
35
+ key = method.to_s.sub(/=/, "")
36
+ val = (arg || []).first
37
+
28
38
  instance_eval(%Q[
29
- def #{method.to_s.sub(/=/, "")}
30
- "#{(arg || []).first}"
39
+ def #{key}
40
+ "#{val}"
31
41
  end
32
42
  ])
43
+
44
+ # Store user-defined key
45
+ @keys[key] = val
33
46
  end
34
- end
35
47
 
36
- # Create and call a global instance of Rk to either build a key or set/get attributes
48
+ # Define key elements by key/value pairs of a hash, symbols and strings allowed for
49
+ # keys/values.
50
+ #
51
+ # rk.keys = { user: "user", "statistics" => "stats", "session" => :sess }
52
+ def keys=(h)
53
+ # Allow hash only
54
+ raise TypeError, "invalid type" unless h.is_a?(Hash)
55
+
56
+ # Call rk.<key> = <val> for each pair of hash
57
+ h.each_pair do |key, val|
58
+ self.send("#{key}=", val)
59
+ end
60
+ end
61
+
62
+ end # class Rk
63
+
64
+ # Create and call a global instance of Rk to either build a key or set/get attributes.
37
65
  def rk(*elements)
38
66
  $_rk = Rk.new unless $_rk
39
67
 
@@ -49,7 +77,7 @@ module Rk
49
77
 
50
78
  end # module Rk
51
79
 
52
- # We could use "extend Rk" to extend only "main", but we would have to "include Rk" in
80
+ # We could use +extend Rk+ to extend only +main+, but we would have to +include Rk+ in
53
81
  # any class, hence we include Rk to the global object.
54
82
  class Object
55
83
  include Rk
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Mensinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-19 00:00:00.000000000 Z
11
+ date: 2016-12-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby helper to generate keys for Redis
14
14
  email: oliver.mensinger@gmail.com