prefatory 0.1.1
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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +21 -0
- data/README.md +98 -0
- data/Rakefile +6 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/lib/prefatory/config.rb +12 -0
- data/lib/prefatory/errors.rb +7 -0
- data/lib/prefatory/keys.rb +11 -0
- data/lib/prefatory/storage/discover.rb +25 -0
- data/lib/prefatory/storage/memcached_provider.rb +50 -0
- data/lib/prefatory/storage/redis_provider.rb +48 -0
- data/lib/prefatory/storage/test_provider.rb +37 -0
- data/lib/prefatory/version.rb +3 -0
- data/lib/prefatory.rb +82 -0
- data/prefatory.gemspec +34 -0
- metadata +167 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7102730a0aeebb54993b61da17dcf1f1bcf8e45d21b17af26afd1925a7fb3177
|
|
4
|
+
data.tar.gz: 03c2a801624b128938fa4f78f02df0a18c53f0695e53cbd034173c91851d5ca1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f71144bb0a99865d9eba74c9aba2b11dd326680ac0d560f18103777ad902f2f4895883ce38ac4e91aea347fa586cad694a7203a20f04f7d45cff4926712829c0
|
|
7
|
+
data.tar.gz: 66eec9eaf91a357db544cee99022a7ab5ef5acd2b580d5677897eaf8cc6a5a7e1c611ad50578030adadd849d54fe0b8de92bc447706382468e4555260a3ffa71
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at rx@voomify.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
prefatory (0.1.0)
|
|
5
|
+
dry-configurable (~> 0.7.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
coderay (1.1.2)
|
|
11
|
+
concurrent-ruby (1.1.3)
|
|
12
|
+
dalli (2.7.9)
|
|
13
|
+
diff-lcs (1.3)
|
|
14
|
+
docile (1.3.1)
|
|
15
|
+
dry-configurable (0.7.0)
|
|
16
|
+
concurrent-ruby (~> 1.0)
|
|
17
|
+
json (2.1.0)
|
|
18
|
+
method_source (0.9.2)
|
|
19
|
+
pry (0.12.2)
|
|
20
|
+
coderay (~> 1.1.0)
|
|
21
|
+
method_source (~> 0.9.0)
|
|
22
|
+
rake (10.5.0)
|
|
23
|
+
redis (4.0.3)
|
|
24
|
+
rspec (3.8.0)
|
|
25
|
+
rspec-core (~> 3.8.0)
|
|
26
|
+
rspec-expectations (~> 3.8.0)
|
|
27
|
+
rspec-mocks (~> 3.8.0)
|
|
28
|
+
rspec-core (3.8.0)
|
|
29
|
+
rspec-support (~> 3.8.0)
|
|
30
|
+
rspec-expectations (3.8.2)
|
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
32
|
+
rspec-support (~> 3.8.0)
|
|
33
|
+
rspec-mocks (3.8.0)
|
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
35
|
+
rspec-support (~> 3.8.0)
|
|
36
|
+
rspec-support (3.8.0)
|
|
37
|
+
simplecov (0.16.1)
|
|
38
|
+
docile (~> 1.1)
|
|
39
|
+
json (>= 1.8, < 3)
|
|
40
|
+
simplecov-html (~> 0.10.0)
|
|
41
|
+
simplecov-html (0.10.2)
|
|
42
|
+
|
|
43
|
+
PLATFORMS
|
|
44
|
+
ruby
|
|
45
|
+
|
|
46
|
+
DEPENDENCIES
|
|
47
|
+
bundler (~> 1.16)
|
|
48
|
+
dalli
|
|
49
|
+
prefatory!
|
|
50
|
+
pry
|
|
51
|
+
rake (~> 10.0)
|
|
52
|
+
redis
|
|
53
|
+
rspec (~> 3.0)
|
|
54
|
+
simplecov
|
|
55
|
+
|
|
56
|
+
BUNDLED WITH
|
|
57
|
+
1.17.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Russell Edens
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Prefatory
|
|
2
|
+
|
|
3
|
+
Prefatory provides an ultra-lightweight storage of entities (models or values/attributes) in a non-transactional
|
|
4
|
+
preliminary key value store (redis or memcache).
|
|
5
|
+
|
|
6
|
+
Sometimes you need to collect data before you can write it to the database.
|
|
7
|
+
That is the impetus for this gem. The collection of data could happen across multiple client interactions spanning
|
|
8
|
+
pages or even sessions.
|
|
9
|
+
|
|
10
|
+
You can store anything that can be serialized using Ruby's Mashal class.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Add this line to your application's Gemfile:
|
|
15
|
+
|
|
16
|
+
gem 'prefatory'
|
|
17
|
+
|
|
18
|
+
And then execute:
|
|
19
|
+
|
|
20
|
+
$ bundle
|
|
21
|
+
|
|
22
|
+
Or install it yourself as:
|
|
23
|
+
|
|
24
|
+
$ gem install prefatory
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
To store and retrieve an entity/model/value/hash/array:
|
|
29
|
+
|
|
30
|
+
foo_key = Prefatory.save(foo)
|
|
31
|
+
foo = Prefatory.find(foo_key)
|
|
32
|
+
|
|
33
|
+
Save returns nil if the save was unable to save the entity/model/value/hash/array. Find returns nil of the key could not be found.
|
|
34
|
+
|
|
35
|
+
Save also takes a ttl as an optional parameter. See configuration for setting the ttl default.
|
|
36
|
+
|
|
37
|
+
You can also use the bang methods. The find! method raises Prefatory::NotFound if the object could not be found.
|
|
38
|
+
The save! method raises Prefatory::NotSaved if the object could not be saved.
|
|
39
|
+
|
|
40
|
+
You can also store and retrieve ruby built in types including arrays and hashes.
|
|
41
|
+
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
Optional.
|
|
45
|
+
|
|
46
|
+
The gem will automatically look for Redis as the key value store.
|
|
47
|
+
If it doesn't find it it will look for Memcache.
|
|
48
|
+
|
|
49
|
+
Redis:
|
|
50
|
+
|
|
51
|
+
# default configuration:
|
|
52
|
+
Prefatory.configure do |config|
|
|
53
|
+
configure.storage.engine = :redis
|
|
54
|
+
# accepts any additional options redis-rb gem accepts
|
|
55
|
+
configure.storage.options = {url: ENV['REDIS_URL']||Redis.current||'redis://127.0.0.1:0'}
|
|
56
|
+
config.ttl =
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
Memcache:
|
|
60
|
+
|
|
61
|
+
# default configuration
|
|
62
|
+
Prefatory.configure do |config|
|
|
63
|
+
config.storage.engine = :memcache
|
|
64
|
+
# accepts any additional options dalli gem accepts
|
|
65
|
+
config.storage.options = {servers: [ENV['MEMCACHE_URL']]}
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
Logger:
|
|
69
|
+
|
|
70
|
+
Prefatory.configure do |config|
|
|
71
|
+
setting :logger, Logger.new(STDOUT)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
TTL:
|
|
75
|
+
|
|
76
|
+
Prefatory.configure do |config|
|
|
77
|
+
setting :ttl, 86400 # in seconds - defaults to one day
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
You can set this to nil, but it is **NOT** recommended as you may fill up your prefatory storage.
|
|
81
|
+
|
|
82
|
+
## Development
|
|
83
|
+
|
|
84
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
85
|
+
|
|
86
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
87
|
+
|
|
88
|
+
## Contributing
|
|
89
|
+
|
|
90
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rx/prefatory. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
95
|
+
|
|
96
|
+
## Code of Conduct
|
|
97
|
+
|
|
98
|
+
Everyone interacting in the Prefatory project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rx/prefatory/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "prefatory"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
require "pry"
|
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'dry-configurable'
|
|
2
|
+
|
|
3
|
+
module Prefatory
|
|
4
|
+
extend Dry::Configurable
|
|
5
|
+
|
|
6
|
+
setting :logger, Logger.new(STDOUT)
|
|
7
|
+
setting :ttl, 86400 # in seconds - defaults to one day
|
|
8
|
+
setting :storage do
|
|
9
|
+
setting :provider # :memcached or :redis
|
|
10
|
+
setting :options
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module Prefatory
|
|
2
|
+
module Keys
|
|
3
|
+
INCR_KEY = 'prefatory'.freeze
|
|
4
|
+
|
|
5
|
+
def build_key(obj=nil,value=nil, prefix=nil)
|
|
6
|
+
klass = nil
|
|
7
|
+
klass = obj&.class == 'Class' ? obj.name.downcase : obj&.class.name.downcase if obj
|
|
8
|
+
"#{INCR_KEY}#{prefix ? ":#{prefix}" : nil}#{klass ? ":#{klass}" : nil}#{value ? ":#{value}" : nil}"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Prefatory
|
|
2
|
+
module Storage
|
|
3
|
+
class Discover
|
|
4
|
+
def initialize(config, ttl, key_prefix)
|
|
5
|
+
@config = config
|
|
6
|
+
@ttl = ttl
|
|
7
|
+
@key_prefix = key_prefix
|
|
8
|
+
@provider = find_provider(config.provider)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def instance
|
|
12
|
+
require_relative "#{@provider}_provider"
|
|
13
|
+
Object.const_get("Prefatory::Storage::#{@provider.to_s.capitalize}Provider").new(@config.options, @ttl, @key_prefix)
|
|
14
|
+
end
|
|
15
|
+
private
|
|
16
|
+
def find_provider(provider)
|
|
17
|
+
return provider if provider
|
|
18
|
+
return :redis if defined?(Redis)
|
|
19
|
+
return :memcached if defined?(Dalli)
|
|
20
|
+
raise Errors::Configuration, 'Unable to discover a default storage provider! '\
|
|
21
|
+
'You must either have the redis or dalli gem installed.'
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'dalli'
|
|
2
|
+
|
|
3
|
+
module Prefatory
|
|
4
|
+
module Storage
|
|
5
|
+
class MemcachedProvider
|
|
6
|
+
include Prefatory::Keys
|
|
7
|
+
DEFAULT_SERVER = '127.0.0.1:11211'.freeze
|
|
8
|
+
DEFAULT_OPTIONS = {namespace: 'prefatory', compress: true}
|
|
9
|
+
|
|
10
|
+
def initialize(options, ttl=nil, key_prefix=nil)
|
|
11
|
+
@ttl = ttl
|
|
12
|
+
@key_prefix = key_prefix
|
|
13
|
+
@client = Dalli::Client.new(default_servers(options), default_options(options))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def set(key, value, ttl=nil)
|
|
17
|
+
@client.set(key, value, ttl||@ttl)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def get(key)
|
|
21
|
+
@client.get(key)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def delete(key)
|
|
25
|
+
@client.delete(key)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def key?(key)
|
|
29
|
+
v = @client.fetch(key){:does_not_exists}
|
|
30
|
+
v != :does_not_exists
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def next_key(obj=nil)
|
|
34
|
+
build_key(obj, @client.incr(build_key(obj, nil, @key_prefix), 1, nil, 0), @key_prefix)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def default_servers(options)
|
|
40
|
+
return options[:servers] if options&.fetch(:servers) {false}
|
|
41
|
+
DEFAULT_SERVER
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def default_options(options)
|
|
45
|
+
return options.except(:servers) if options
|
|
46
|
+
DEFAULT_OPTIONS
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
require 'redis'
|
|
2
|
+
|
|
3
|
+
module Prefatory
|
|
4
|
+
module Storage
|
|
5
|
+
class RedisProvider
|
|
6
|
+
include Prefatory::Keys
|
|
7
|
+
|
|
8
|
+
def initialize(options, ttl, key_prefix=nil)
|
|
9
|
+
options = default_settings(options)
|
|
10
|
+
@ttl = ttl
|
|
11
|
+
@key_prefix = key_prefix
|
|
12
|
+
@client = options ? Redis.new(options) : Redis.current
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def set(key, value, ttl=nil)
|
|
16
|
+
@client.set(key, value, ex: ttl||@ttl)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def get(key)
|
|
20
|
+
@client.get(key)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def delete(key)
|
|
24
|
+
@client.del(key)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def key?(key)
|
|
28
|
+
@client.exists(key)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def next_key(obj=nil)
|
|
32
|
+
build_key(obj, @client.incr(build_key(obj, nil, @key_prefix)), @key_prefix)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def default_settings(options)
|
|
38
|
+
return options if options&.fetch(:url){false} || options&.fetch(:host){false}
|
|
39
|
+
url = ENV['REDIS_PROVIDER'] || ENV['REDIS_URL']
|
|
40
|
+
if (url)
|
|
41
|
+
options ||= {}
|
|
42
|
+
options = options.merge(url: url)
|
|
43
|
+
end
|
|
44
|
+
options
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'dalli'
|
|
2
|
+
|
|
3
|
+
module Prefatory
|
|
4
|
+
module Storage
|
|
5
|
+
# Used by Specs. This is not safe for production (in any way!) You have been warned.
|
|
6
|
+
class TestProvider
|
|
7
|
+
include Prefatory::Keys
|
|
8
|
+
|
|
9
|
+
def initialize(ttl = nil, key_prefix = nil)
|
|
10
|
+
@ttl = ttl
|
|
11
|
+
@key_prefix = key_prefix
|
|
12
|
+
@hash = {}
|
|
13
|
+
@next_key = 0
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def set(key, value, ttl=nil)
|
|
17
|
+
@hash.store(key, value)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def get(key)
|
|
21
|
+
@hash.fetch(key){nil}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def key?(key)
|
|
25
|
+
@hash.key?(key)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def delete(key)
|
|
29
|
+
@hash.delete(key)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def next_key(obj = nil)
|
|
33
|
+
build_key(obj, @next_key += 1, @key_prefix)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/prefatory.rb
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
require_relative "prefatory/version"
|
|
2
|
+
require_relative 'prefatory/config'
|
|
3
|
+
require_relative 'prefatory/errors'
|
|
4
|
+
require_relative 'prefatory/keys'
|
|
5
|
+
require_relative 'prefatory/storage/discover'
|
|
6
|
+
|
|
7
|
+
module Prefatory
|
|
8
|
+
# A repository that temporarily saves ruby objects to a key value store (Redis or Memcached).
|
|
9
|
+
class Repository
|
|
10
|
+
KEY_NOT_FOUND_MSG = "Unable to find the object with key ?"
|
|
11
|
+
|
|
12
|
+
def initialize(key_prefix: nil, storage: nil,
|
|
13
|
+
config: Prefatory.config,
|
|
14
|
+
marshaler: Marshal)
|
|
15
|
+
@config = config
|
|
16
|
+
@storage = storage || Storage::Discover.new(@config.storage, @config.ttl, key_prefix).instance
|
|
17
|
+
@marshaler = marshaler
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def find(key)
|
|
21
|
+
return nil unless @storage.key?(key)
|
|
22
|
+
value = @storage.get(key)
|
|
23
|
+
@marshaler.load(value)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def find!(key)
|
|
27
|
+
raise Errors::NotFound, KEY_NOT_FOUND_MSG.sub('?',key) unless @storage.key?(key)
|
|
28
|
+
obj = find(key)
|
|
29
|
+
obj
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def save(obj, ttl=nil)
|
|
33
|
+
begin
|
|
34
|
+
value = @marshaler.dump(obj)
|
|
35
|
+
key = @storage.next_key(obj)
|
|
36
|
+
@storage.set(key, value, ttl)
|
|
37
|
+
rescue StandardError => e
|
|
38
|
+
logger.info("An error occurred (#{e.inspect}) storing object: #{obj.inspect}")
|
|
39
|
+
key = nil
|
|
40
|
+
end
|
|
41
|
+
key
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def save!(obj, ttl=nil)
|
|
45
|
+
key = save(obj, ttl)
|
|
46
|
+
raise Errors::NotSaved,
|
|
47
|
+
"Unable to save object! #{obj.inspect}. Check the log for more information." unless @storage.key?(key)
|
|
48
|
+
key
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def update(key, obj, ttl=nil)
|
|
52
|
+
return false unless @storage.key?(key)
|
|
53
|
+
value = @marshaler.dump(obj)
|
|
54
|
+
@storage.set(key, value, ttl)
|
|
55
|
+
true
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def update!(key, obj, ttl=nil)
|
|
59
|
+
raise Errors::NotFound, KEY_NOT_FOUND_MSG.sub('?',key) unless @storage.key?(key)
|
|
60
|
+
update(key, obj, ttl)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def delete(key)
|
|
64
|
+
return false unless @storage.key?(key)
|
|
65
|
+
@storage.delete(key)
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def delete!(key)
|
|
70
|
+
raise Errors::NotFound, KEY_NOT_FOUND_MSG.sub('?',key) unless @storage.key?(key)
|
|
71
|
+
delete(key)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
include Prefatory::Keys
|
|
77
|
+
|
|
78
|
+
def logger
|
|
79
|
+
@config.logger
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
data/prefatory.gemspec
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "prefatory/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "prefatory"
|
|
8
|
+
spec.version = Prefatory::VERSION
|
|
9
|
+
spec.authors = ["Russell Edens"]
|
|
10
|
+
spec.email = ["rx@voomify.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Prefatory provides storage of entities (models or values/attributes) in a non-transactional
|
|
13
|
+
preliminary key value store (redis or memcache).}
|
|
14
|
+
spec.description = %q{Sometimes you need to collect data before you can write it to the database.
|
|
15
|
+
That is the impedius for this gem. The collection of data could happen across multiple
|
|
16
|
+
client interactions spanning pages or even sessions.}
|
|
17
|
+
spec.homepage = "https://github.com/rx/prefatory"
|
|
18
|
+
spec.license = "MIT"
|
|
19
|
+
|
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
|
22
|
+
end
|
|
23
|
+
spec.bindir = "exe"
|
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
spec.add_dependency "dry-configurable", "~> 0.7.0"
|
|
27
|
+
|
|
28
|
+
spec.add_development_dependency "redis"
|
|
29
|
+
spec.add_development_dependency "dalli"
|
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
31
|
+
spec.add_development_dependency "pry"
|
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
34
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: prefatory
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Russell Edens
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-12-07 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: dry-configurable
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.7.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.7.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: redis
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
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: dalli
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: bundler
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '1.16'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '1.16'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: pry
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rake
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '10.0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '10.0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rspec
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '3.0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '3.0'
|
|
111
|
+
description: |-
|
|
112
|
+
Sometimes you need to collect data before you can write it to the database.
|
|
113
|
+
That is the impedius for this gem. The collection of data could happen across multiple
|
|
114
|
+
client interactions spanning pages or even sessions.
|
|
115
|
+
email:
|
|
116
|
+
- rx@voomify.com
|
|
117
|
+
executables: []
|
|
118
|
+
extensions: []
|
|
119
|
+
extra_rdoc_files: []
|
|
120
|
+
files:
|
|
121
|
+
- ".gitignore"
|
|
122
|
+
- ".rspec"
|
|
123
|
+
- ".travis.yml"
|
|
124
|
+
- CODE_OF_CONDUCT.md
|
|
125
|
+
- Gemfile
|
|
126
|
+
- Gemfile.lock
|
|
127
|
+
- LICENSE.txt
|
|
128
|
+
- README.md
|
|
129
|
+
- Rakefile
|
|
130
|
+
- bin/console
|
|
131
|
+
- bin/setup
|
|
132
|
+
- lib/prefatory.rb
|
|
133
|
+
- lib/prefatory/config.rb
|
|
134
|
+
- lib/prefatory/errors.rb
|
|
135
|
+
- lib/prefatory/keys.rb
|
|
136
|
+
- lib/prefatory/storage/discover.rb
|
|
137
|
+
- lib/prefatory/storage/memcached_provider.rb
|
|
138
|
+
- lib/prefatory/storage/redis_provider.rb
|
|
139
|
+
- lib/prefatory/storage/test_provider.rb
|
|
140
|
+
- lib/prefatory/version.rb
|
|
141
|
+
- prefatory.gemspec
|
|
142
|
+
homepage: https://github.com/rx/prefatory
|
|
143
|
+
licenses:
|
|
144
|
+
- MIT
|
|
145
|
+
metadata: {}
|
|
146
|
+
post_install_message:
|
|
147
|
+
rdoc_options: []
|
|
148
|
+
require_paths:
|
|
149
|
+
- lib
|
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
151
|
+
requirements:
|
|
152
|
+
- - ">="
|
|
153
|
+
- !ruby/object:Gem::Version
|
|
154
|
+
version: '0'
|
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - ">="
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: '0'
|
|
160
|
+
requirements: []
|
|
161
|
+
rubyforge_project:
|
|
162
|
+
rubygems_version: 2.7.6
|
|
163
|
+
signing_key:
|
|
164
|
+
specification_version: 4
|
|
165
|
+
summary: Prefatory provides storage of entities (models or values/attributes) in a
|
|
166
|
+
non-transactional preliminary key value store (redis or memcache).
|
|
167
|
+
test_files: []
|