receptacle 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -7
- data/CHANGELOG.md +6 -0
- data/Dangerfile +1 -1
- data/README.md +2 -0
- data/examples/simple_repo.rb +35 -18
- data/lib/receptacle/method_cache.rb +4 -4
- data/lib/receptacle/method_delegation.rb +1 -4
- data/lib/receptacle/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9fe755e41253d67a0be871fab1aebf01ed201ec
|
4
|
+
data.tar.gz: 246b2cb847fd611e9886c75992667dae615f6a84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6f2eb8a27fc37b7838c43c1cf2534415bbb76200f4db266cb5ceb4576f09f1f99250862ca8392463295b791a7b63792888b6a806971637b2beab306130ca81d
|
7
|
+
data.tar.gz: 69f486884c18e7eaf28ef651a15c007ac17dde96b4ce40eb99ea53dd06c0f9f162372c6fa2585fad03994f8a621b841bc10e0a07b7de3b27860b21d88f89838f
|
data/.travis.yml
CHANGED
@@ -3,9 +3,9 @@ cache: bundler
|
|
3
3
|
rvm:
|
4
4
|
- 2.1.10
|
5
5
|
- 2.2.6
|
6
|
-
- 2.3.
|
7
|
-
- 2.4.
|
8
|
-
- jruby-9.1.
|
6
|
+
- 2.3.4
|
7
|
+
- 2.4.1
|
8
|
+
- jruby-9.1.12.0
|
9
9
|
jdk:
|
10
10
|
- oraclejdk8
|
11
11
|
env:
|
@@ -19,18 +19,18 @@ matrix:
|
|
19
19
|
- rvm: 2.2.6
|
20
20
|
jdk: oraclejdk8
|
21
21
|
env: "JRUBY_OPTS='-Xcompile.invokedynamic=true --debug'"
|
22
|
-
- rvm: 2.3.
|
22
|
+
- rvm: 2.3.4
|
23
23
|
jdk: oraclejdk8
|
24
24
|
env: "JRUBY_OPTS='-Xcompile.invokedynamic=true --debug'"
|
25
|
-
- rvm: 2.4.
|
25
|
+
- rvm: 2.4.1
|
26
26
|
jdk: oraclejdk8
|
27
27
|
env: "JRUBY_OPTS='-Xcompile.invokedynamic=true --debug'"
|
28
28
|
allow_failures:
|
29
|
-
- rvm: jruby-9.1.
|
29
|
+
- rvm: jruby-9.1.12.0
|
30
30
|
jdk: oraclejdk8
|
31
31
|
env: "JRUBY_OPTS='-Xcompile.invokedynamic=true --debug'"
|
32
32
|
before_install:
|
33
33
|
- gem update --system
|
34
|
-
- gem install bundler -v 1.
|
34
|
+
- gem install bundler -v 1.15.1
|
35
35
|
before_script:
|
36
36
|
- bundle exec danger
|
data/CHANGELOG.md
CHANGED
data/Dangerfile
CHANGED
data/README.md
CHANGED
@@ -10,6 +10,8 @@
|
|
10
10
|
Provides easy and fast means to use the repository pattern to create separation
|
11
11
|
between your business logic and your data sources.
|
12
12
|
|
13
|
+
Battle tested at [Runtastic](https://github.com/runtastic).
|
14
|
+
|
13
15
|
## Installation
|
14
16
|
|
15
17
|
Add this line to your application's Gemfile:
|
data/examples/simple_repo.rb
CHANGED
@@ -1,31 +1,25 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
require 'bundler/inline'
|
4
2
|
|
3
|
+
require 'bundler/inline'
|
5
4
|
gemfile true do
|
6
5
|
source 'https://rubygems.org'
|
7
|
-
gem 'receptacle', '
|
6
|
+
gem 'receptacle', '~>0.3'
|
8
7
|
gem 'mongo'
|
9
8
|
end
|
9
|
+
require 'irb'
|
10
10
|
|
11
|
+
# a simple struct to act as business entity
|
11
12
|
User = Struct.new(:id, :name)
|
12
13
|
|
13
|
-
#
|
14
|
-
module Repository
|
15
|
-
module User
|
16
|
-
include Receptacle::Repo
|
17
|
-
mediate :find
|
18
|
-
mediate :create
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# we should have a global mongo connection which can be easily reused
|
14
|
+
# we have a global mongo connection which can be easily reused
|
23
15
|
module Connection
|
24
16
|
class Mongo
|
25
17
|
include Singleton
|
26
18
|
|
27
19
|
def initialize
|
28
|
-
|
20
|
+
::Mongo::Logger.logger.level = Logger::INFO
|
21
|
+
@client = ::Mongo::Client.new(['127.0.0.1:27017'], database: 'receptacle')
|
22
|
+
client[:users].delete_many # empty collection
|
29
23
|
end
|
30
24
|
attr_reader :client
|
31
25
|
def self.client
|
@@ -34,20 +28,34 @@ module Connection
|
|
34
28
|
end
|
35
29
|
end
|
36
30
|
|
31
|
+
# define our Repository
|
32
|
+
module Repository
|
33
|
+
module User
|
34
|
+
include Receptacle::Repo
|
35
|
+
mediate :find
|
36
|
+
mediate :create
|
37
|
+
mediate :clear
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
37
41
|
# some strategies
|
38
42
|
module Repository
|
39
43
|
module User
|
40
44
|
module Strategy
|
41
45
|
class Mongo
|
42
46
|
def find(id:)
|
43
|
-
mongo_to_model(collection.find(_id: id))
|
47
|
+
mongo_to_model(collection.find(_id: id).first)
|
44
48
|
rescue
|
45
49
|
nil
|
46
50
|
end
|
47
51
|
|
48
52
|
def create(name:)
|
49
53
|
ret = collection.insert_one(name: name)
|
50
|
-
find(id: ret
|
54
|
+
find(id: ret.inserted_id)
|
55
|
+
end
|
56
|
+
|
57
|
+
def clear
|
58
|
+
collection.delete_many
|
51
59
|
end
|
52
60
|
|
53
61
|
private
|
@@ -72,7 +80,11 @@ module Repository
|
|
72
80
|
|
73
81
|
def create(name:)
|
74
82
|
id = BSON::ObjectId.new
|
75
|
-
store[id] = User.new(id, name)
|
83
|
+
store[id] = ::User.new(id, name)
|
84
|
+
end
|
85
|
+
|
86
|
+
def clear
|
87
|
+
self.class.store = {}
|
76
88
|
end
|
77
89
|
|
78
90
|
private
|
@@ -89,15 +101,20 @@ end
|
|
89
101
|
Repository::User.strategy Repository::User::Strategy::InMemory
|
90
102
|
|
91
103
|
user = Repository::User.create(name: 'foo')
|
104
|
+
print 'created user: '
|
92
105
|
p user
|
106
|
+
print 'find user by id: '
|
93
107
|
p Repository::User.find(id: user.id)
|
94
108
|
|
95
109
|
# switching to mongo and we see it's using a different store but keeps the same interface
|
96
110
|
Repository::User.strategy Repository::User::Strategy::Mongo
|
97
111
|
|
112
|
+
print 'search same user in other strategy: '
|
98
113
|
p Repository::User.find(id: user.id)
|
99
114
|
#-> nil
|
100
115
|
|
101
|
-
user = Repository::User.create(name: 'foo')
|
116
|
+
user = Repository::User.create(name: 'foo mongo')
|
117
|
+
print 'create new user: '
|
102
118
|
p user
|
119
|
+
print 'find new user by id: '
|
103
120
|
p Repository::User.find(id: user.id)
|
@@ -16,14 +16,14 @@ module Receptacle
|
|
16
16
|
# @return [Integer] arity of strategy method according to https://ruby-doc.org/core-2.3.3/Method.html#method-i-arity
|
17
17
|
attr_reader :arity
|
18
18
|
|
19
|
-
def initialize(method_name:, strategy:,
|
19
|
+
def initialize(method_name:, strategy:, wrappers:) # rubocop:disable Metrics/AbcSize
|
20
20
|
@strategy = strategy
|
21
21
|
@before_method_name = :"before_#{method_name}"
|
22
22
|
@after_method_name = :"after_#{method_name}"
|
23
23
|
@method_name = method_name.to_sym
|
24
|
-
before_wrappers
|
25
|
-
after_wrappers
|
26
|
-
@wrappers = before_wrappers | after_wrappers
|
24
|
+
before_wrappers = wrappers.select { |w| w.method_defined?(@before_method_name) }
|
25
|
+
after_wrappers = wrappers.select { |w| w.method_defined?(@after_method_name) }
|
26
|
+
@wrappers = wrappers & (before_wrappers | after_wrappers)
|
27
27
|
@skip_before_wrappers = before_wrappers.empty?
|
28
28
|
@skip_after_wrappers = after_wrappers.empty?
|
29
29
|
@arity = strategy.new.method(method_name).arity
|
@@ -38,14 +38,11 @@ module Receptacle
|
|
38
38
|
# @return [MethodCache]
|
39
39
|
def __build_method_call_cache(method_name)
|
40
40
|
config = Registration.repositories[self]
|
41
|
-
before_method_name = :"before_#{method_name}"
|
42
|
-
after_method_name = :"after_#{method_name}"
|
43
41
|
|
44
42
|
raise Errors::NotConfigured, repo: self if config.strategy.nil?
|
45
43
|
MethodCache.new(
|
46
44
|
strategy: config.strategy,
|
47
|
-
|
48
|
-
after_wrappers: config.wrappers.select { |w| w.method_defined?(after_method_name) },
|
45
|
+
wrappers: config.wrappers,
|
49
46
|
method_name: method_name
|
50
47
|
)
|
51
48
|
end
|
data/lib/receptacle/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: receptacle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Eger
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -218,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
218
218
|
version: '0'
|
219
219
|
requirements: []
|
220
220
|
rubyforge_project:
|
221
|
-
rubygems_version: 2.6.
|
221
|
+
rubygems_version: 2.6.11
|
222
222
|
signing_key:
|
223
223
|
specification_version: 4
|
224
224
|
summary: repository pattern
|