redis_rejson_models 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1951657418adc33d10e7a78104c32332a2e8a4fa3aa7aa03ab9d7bf965869af3
4
+ data.tar.gz: 17ec8166da2a3044b7228c3204eb62d26bb2dc26ca3cb4641a5c68630b242112
5
+ SHA512:
6
+ metadata.gz: 2840bef4874d834bcf08eb69e05165e43e7f5c55b2345ada6acbd10f97b1b57d8857a06a76f3356c1fbc11d3ef1236b655442c161eebad78e502d72c638b454a
7
+ data.tar.gz: e3e95dd3f9e5648dd3ad94b8f412c0b16c572535cb7870c85770dee1708b0cfa79ae7cbfa66d577fe87696671c32da87fefbb92e502acaea76f504393e2a2cfa
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ desc "Spec"
2
+ task :spec do
3
+ sh "bundle exec rspec"
4
+ end
5
+
6
+ task default: :spec
data/Readme.md ADDED
@@ -0,0 +1,114 @@
1
+ # RedisRejsonModels
2
+
3
+ Document-like ORM based on Redis key value store - uses Redis Re-JSON for full json manipulation
4
+
5
+ ### Install
6
+
7
+ Add to your gemfile:
8
+
9
+ ```rb
10
+ gem 'redis_rejson_models', '~> 0.9.5'
11
+ ```
12
+
13
+ Or without bundler, run in your shell:
14
+
15
+ gem i redis_rejson_models
16
+
17
+
18
+ ### Requirements
19
+
20
+ - Redis (v4+)
21
+ - Re-JSON module installed
22
+
23
+ ### DB API
24
+
25
+ ```ruby
26
+ Model.get 1 #=> returns an object instance (gets it from redis)
27
+
28
+ Model.all #=> returns an array of Model object instances
29
+
30
+ Model.create({}) #=> creates an entry in redis
31
+
32
+ Model.update(1, {}) #=> updates a record in redis
33
+ ```
34
+
35
+ ### Model Definition and Instance API by the `shallow_attributes` gem
36
+
37
+ #### Definition:
38
+
39
+ ```ruby
40
+ class Document
41
+ extend RedisRejsonModelLib
42
+ include RedisRejsonModelMixin
43
+ include ShallowAttributes
44
+
45
+ attribute :id, Integer
46
+ attribute :name, String
47
+ attribute :contents, String
48
+
49
+ def antani
50
+ # "..."
51
+ end
52
+ end
53
+ ```
54
+
55
+ ### Instance API:
56
+
57
+ ```ruby
58
+ doc = Document.new name: "Foo", contents: "Bar123"
59
+ puts doc.name #=> "foo"
60
+ puts doc.antani #=> "..."
61
+ doc.save # saves the record, gives an auto increment id (example: 1)
62
+
63
+ doc = Document.get doc.id
64
+ puts doc.name #=> "foo"
65
+ puts doc.antani #=> "..."
66
+ ```
67
+
68
+ ### Run local Redis (+ Re-JSON module) locally via Docker:
69
+
70
+ in this repo run:
71
+
72
+ ```
73
+ docker-compose up
74
+ ```
75
+
76
+ Which use the docker-compose.yml to start your redis.
77
+
78
+
79
+ ### Set up your Redis and RJSON
80
+
81
+ ```rb
82
+ # sample environment setup
83
+ require 'bundler'
84
+ Bundler.require :default
85
+
86
+ # setup redis
87
+ R = Redis.new
88
+
89
+ # load and configure redis with rejson models
90
+ include RedisRejsonModels
91
+ RJ.configure redis: R
92
+
93
+ class Document
94
+ # ...
95
+ end
96
+ ```
97
+
98
+
99
+ ### Run specs
100
+
101
+ (you need to run a local redis + rejson service - explained above)
102
+
103
+ Run:
104
+
105
+ ```
106
+ rake spec
107
+ ```
108
+
109
+
110
+ ---
111
+
112
+ Enjoy!
113
+
114
+ @makevoid
data/lib/env.rb ADDED
@@ -0,0 +1,16 @@
1
+ # require 'bundler/setup'
2
+ # Bundler.require :default
3
+ require 'redis'
4
+ require 'shallow_attributes'
5
+ # require 'virtus' # old dependency
6
+ require 'dry-inflector'
7
+ require 'rejson'
8
+ # require 'redis_rejson' # old dependency - less mature and featured than `rejson-rb`
9
+
10
+ module RedisRejsonModels
11
+ REDIS_DB = ENV["REDIS_DB"]
12
+
13
+ R = Redis.new db: REDIS_DB || 0
14
+
15
+ class NilValue; end
16
+ end
@@ -0,0 +1,112 @@
1
+ require_relative 'env'
2
+
3
+ module RedisRejsonModels
4
+
5
+ # low level API
6
+ class RJ
7
+ def self.configure(redis:)
8
+ @@redis = redis
9
+ end
10
+
11
+ def self.redis
12
+ @@redis
13
+ end
14
+
15
+ def self.[](key)
16
+ # redis.json_get key
17
+ redis.json_get key, Rejson::Path.root_path
18
+ end
19
+
20
+ def self.[]=(key, val)
21
+ # redis.json_set key, val
22
+ redis.json_set key, Rejson::Path.root_path, val
23
+ end
24
+ end
25
+
26
+ module RedisRejsonModelLib
27
+ INFLECTOR = Dry::Inflector.new
28
+
29
+ def all
30
+ 1.upto(count).map do |entry_id|
31
+ get entry_id
32
+ end
33
+ end
34
+
35
+ def count
36
+ ( RJ["#{resource}:count"] || 0 ).to_i
37
+ end
38
+
39
+ def get(id)
40
+ data = RJ["#{resource}:#{id}"]
41
+ return unless data
42
+ new data
43
+ end
44
+
45
+ def get_attr(id, attr)
46
+ key = "#{resource}:#{id}"
47
+ data = RJ.redis.json_get key, ".name"
48
+ return NilValue.new unless data
49
+ data
50
+ end
51
+
52
+ def create(attrs)
53
+ id = incr
54
+ attrs.merge! id: id
55
+ obj = new attrs
56
+ RJ["#{resource}:#{id}"] = obj.attributes
57
+ obj
58
+ end
59
+
60
+ def update(id, attrs)
61
+ resource = get id
62
+ resource.update attrs
63
+ end
64
+
65
+ def resource
66
+ INFLECTOR.pluralize model_name
67
+ end
68
+
69
+ private
70
+
71
+ # TODO: use incr
72
+ def incr
73
+ RJ["#{resource}:count"] = count + 1
74
+ end
75
+
76
+ def model_name
77
+ self.name.downcase
78
+ end
79
+
80
+ end
81
+
82
+ module RedisRejsonModelMixin
83
+
84
+ def update(attrs_new)
85
+ raise "Can't update a resource without an `id`" unless id
86
+ klass = self.class
87
+ model = klass.get id
88
+ attrs = model.attributes
89
+ attrs.merge! attrs_new
90
+ obj = klass.new attrs
91
+ RJ["#{self.class.resource}:#{id}"] = obj.attributes
92
+ obj
93
+ end
94
+
95
+ def save
96
+ klass = self.class
97
+ attrs = attributes
98
+ id = attrs[:id]
99
+ unless id
100
+ entry_id = klass.send :incr
101
+ attrs.merge! "id" => entry_id
102
+ id = entry_id
103
+ end
104
+
105
+ RJ["#{self.class.resource}:#{id}"] = attrs
106
+ self.id = id
107
+ self
108
+ end
109
+
110
+ end
111
+
112
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module RedisRejsonModels
2
+ VERSION = "0.9.5"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_rejson_models
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.5
5
+ platform: ruby
6
+ authors:
7
+ - "@makevoid (Francesco Canessa)"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: shallow_attributes
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-inflector
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rejson-rb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0
69
+ description: Document-like ORM based on Redis key value store - previously named redis_rejson_models
70
+ - uses Redis Re-JSON for full json manipulation
71
+ email:
72
+ - makevoid@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - Rakefile
78
+ - Readme.md
79
+ - lib/env.rb
80
+ - lib/redis_rejson_models.rb
81
+ - lib/version.rb
82
+ homepage: http://makevoid.com
83
+ licenses:
84
+ - Unlicense
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Document-like ORM based on Redis key value store
105
+ test_files: []