artirix_cache_service 0.1.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8925a97c03f8fda41870180966bfef9e8b224a3
4
- data.tar.gz: 453fb7acd85987d1ee0ed7728718a91dc00d68c8
3
+ metadata.gz: 8c799eb1c4173b8f329ca600d6ae11d41e818106
4
+ data.tar.gz: 4ae4f9898571512b39967e59d15e4ff57cfbb7f0
5
5
  SHA512:
6
- metadata.gz: 1dd2bae6ef68232ae2971aa016f3a04173ff4a03fc9d41598ffc07d10a91eeefde48be2fafe1357f03e564bdeee62c28d45e92bf2ee4f551a06f359d3a443721
7
- data.tar.gz: 443f473eccdbf0908a9efdf11e05e046abc052ac8ebe6faf6811cc75717bed1c23c3f13280a6b22929da0c89c20766cf9411e48bb666328b0fd090d35da24064
6
+ metadata.gz: c1cb3f17d2241fa807bae7fe7c5f4c3f579e4ab6ad56172ea2aaa2b4c99a5844a4546ed00a35e4342368edce62a55b3edb302ef21d7873c01d2995a5295f4831
7
+ data.tar.gz: 812862d064c613e2b0360352cf33801e6a5654566e2afd1952d2145fd7117a0abfc1d2fa36718ec46d2b04ee4e4eab69ed4cfeb4634e928b2c5612f24b8258b9
data/README.md CHANGED
@@ -10,7 +10,7 @@ and some extra variables or options, with some helper methods.
10
10
 
11
11
  TODO: also help with the cache options.
12
12
 
13
- ## Usage
13
+ ## Usage: `.key`
14
14
 
15
15
  The basic way of using it is with the `key` method, which will return the key based on the given arguments.
16
16
 
@@ -23,7 +23,7 @@ ArtirixCacheService.key :some_key # => will return a string with the cache key t
23
23
  The service can use a prefix to be applied to all keys
24
24
 
25
25
  ```ruby
26
- ArtirixCacheService.config_params[:key_prefix] = :configured_prefix
26
+ ArtirixCacheService.register_key_prefix = :configured_prefix
27
27
  ArtirixCacheService.key :some_key # => "configured_prefix/some_key"
28
28
  ArtirixCacheService.key :another # => "configured_prefix/another"
29
29
  ```
@@ -35,7 +35,7 @@ We can pass other arguments, that will be treated and appended to the cache key.
35
35
  note: `blank?` arguments will be skipped.
36
36
 
37
37
  ```ruby
38
- ArtirixCacheService.config_params[:key_prefix] = :configured_prefix
38
+ ArtirixCacheService.register_key_prefix :configured_prefix
39
39
 
40
40
  ArtirixCacheService.key :some_key, :arg1, nil, 'arg2'
41
41
  # => "configured_prefix/some_key/arg1/arg2"
@@ -47,7 +47,7 @@ if an argument (including the first argument) responds to `cache_key`,
47
47
  it will be called.
48
48
 
49
49
  ```ruby
50
- ArtirixCacheService.config_params[:key_prefix] = :configured_prefix
50
+ ArtirixCacheService.register_key_prefix :configured_prefix
51
51
 
52
52
  article = Article.find 17
53
53
  article.cache_key # => "cache_key_article_17"
@@ -64,7 +64,7 @@ for example in case that we're giving it a long list.
64
64
  It will use SHA1.
65
65
 
66
66
  ```ruby
67
- ArtirixCacheService.config_params[:key_prefix] = :prfx
67
+ ArtirixCacheService.register_key_prefix :prfx
68
68
 
69
69
  arg3 = { a: 1, b: 2 }
70
70
  ArtirixCacheService.digest arg3
@@ -82,6 +82,59 @@ ArtirixCacheService.key :some_key, :arg1, 'arg2', digest: [arg3, arg4]
82
82
  # => "prfx/some_key/arg1/arg2/7448a071aeee91fc9ee1c705f15445fdd8411224"
83
83
  ```
84
84
 
85
+ ## Usage: `.options`
86
+
87
+ used for getting the cache options based on the registered defaults and the registered options.
88
+
89
+ ```ruby
90
+
91
+ # unless registered otherwise, the default options is an empty array
92
+ ArtirixCacheService.default_options # => {}
93
+
94
+
95
+ # sets the options to be used as default when needed
96
+ ArtirixCacheService.register_default_options expires_in: 300
97
+
98
+ # we can register some options based on a name (Symbol)
99
+ ArtirixCacheService.registered_options? :my_options # => false
100
+ ArtirixCacheService.registered_options :my_options # => nil
101
+
102
+ ArtirixCacheService.register_options :my_options, race_condition_ttl: 1
103
+
104
+ ArtirixCacheService.registered_options? :my_options # => true
105
+ ArtirixCacheService.registered_options :my_options # => { race_condition_ttl: 1 }
106
+
107
+ ```
108
+
109
+ once we have our different options registered, we can use the Service to get the
110
+ desired final options.
111
+
112
+ Given a list of names, it will use the first one that is registered. It will
113
+ return the options on that name, merged over the default options
114
+
115
+ ```ruby
116
+ ArtirixCacheService.options :missing, :my_options
117
+ # => { expires_in: 300, race_condition_ttl: 1 }
118
+ ```
119
+
120
+ If no registered option is found from the given list, then it will return
121
+ - `nil` (if passing keyword `return_if_missing: :nil`)
122
+ - default options (if passing keyword `return_if_missing: :default`)
123
+ - an empty hash (default behaviour, or passing keyword `return_if_missing` with any other value)
124
+
125
+ ```ruby
126
+ ArtirixCacheService.options :missing, :another_missing
127
+ # => {}
128
+
129
+ ArtirixCacheService.options :missing, :another_missing, return_if_missing: :default
130
+ # => { expires_in: 300 }
131
+
132
+ ArtirixCacheService.options :missing, :another_missing, return_if_missing: :nil
133
+ # => nil
134
+
135
+ ArtirixCacheService.options :missing, :another_missing, return_if_missing: :empty
136
+ # => {}
137
+ ```
85
138
 
86
139
  ## Installation
87
140
 
@@ -109,3 +162,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
109
162
 
110
163
  Bug reports and pull requests are welcome on GitHub at https://github.com/artirix/artirix_cache_service.
111
164
 
165
+
166
+ # Changeset
167
+
168
+ ## v 0.2.0
169
+
170
+ - removed `ArtirixCacheService.config_params` support, now using `register_key_prefix` method
171
+ - add `options` support
@@ -1,12 +1,12 @@
1
1
  module ArtirixCacheService
2
2
  class Service
3
- # Instance
4
- def config_params
5
- @config_params ||= { key_prefix: nil }
3
+ def register_key_prefix(key_prefix)
4
+ @key_prefix = key_prefix
5
+ self
6
6
  end
7
7
 
8
8
  def key_prefix
9
- config_params[:key_prefix]
9
+ @key_prefix
10
10
  end
11
11
 
12
12
  def key(*given_args)
@@ -16,5 +16,60 @@ module ArtirixCacheService
16
16
  def digest(arg)
17
17
  Digest::SHA1.hexdigest arg.to_s
18
18
  end
19
+
20
+ def default_options
21
+ @default_options ||= {}
22
+ end
23
+
24
+ def register_default_options(default_options)
25
+ @default_options = Hash(default_options)
26
+ self
27
+ end
28
+
29
+ def register_options(name, options)
30
+ raise ArgumentError if name.blank?
31
+ options_map[name.to_sym] = Hash(options)
32
+ self
33
+ end
34
+
35
+ def registered_options(name)
36
+ return nil unless name.present?
37
+ options_map[name.to_sym]
38
+ end
39
+
40
+ def registered_options?(name)
41
+ !registered_options(name).nil?
42
+ end
43
+
44
+ def options(*names, return_if_missing: :empty)
45
+ name = names.detect { |name| registered_options? name }
46
+ if name.present?
47
+ get_options(name)
48
+ else
49
+ missing_options(return_if_missing)
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def missing_options(return_if_missing)
56
+ case return_if_missing
57
+ when :default
58
+ default_options.dup
59
+ when :nil, nil
60
+ nil
61
+ else
62
+ {}
63
+ end
64
+ end
65
+
66
+ def get_options(name)
67
+ default_options.merge registered_options(name)
68
+ end
69
+
70
+ def options_map
71
+ @options_map ||= {}
72
+ end
73
+
19
74
  end
20
75
  end
@@ -1,3 +1,3 @@
1
1
  module ArtirixCacheService
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -7,7 +7,13 @@ module ArtirixCacheService
7
7
 
8
8
  # Delegation of static methods to the Service instance
9
9
  class << self
10
- delegate :key, :config_params, :key_prefix, :digest, to: :service
10
+ delegate :key, :digest,
11
+ :register_key_prefix, :key_prefix,
12
+ :default_options, :register_default_options,
13
+ :register_options, :registered_options,
14
+ :registered_options?, :registered_options,
15
+ :options,
16
+ to: :service
11
17
  end
12
18
 
13
19
  def self.service
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artirix_cache_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño