rom-roda 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2a2b37009c1e6d3b9bd3bb8895d531ad3428ee1
4
- data.tar.gz: 25463288f42e47240460faeb43b7d2b65b822c68
3
+ metadata.gz: 7e861bfecc7fde3d430801f8b00322a40882cdb0
4
+ data.tar.gz: c948e4e5150d773338385794726a20f4e24fbf1b
5
5
  SHA512:
6
- metadata.gz: d9ef17fbe99c4d04775bed831c976997d5f9dc7abe68941be63b49d7feb6b6784903e4f5b480bea8c94c53efd6523d5ff06c3cb7cedd731e2f3dc405ad38507d
7
- data.tar.gz: bc4ac81939e20bf024f708ac17f118d1a073f949e066c3d22314bc7d9acc7b9b53c32efbad650e1d449c372bdf4b3d7ffe89743c816e67ef66effe08e2edbe87
6
+ metadata.gz: 7d1b5003d145863a943b467f9d78d5c9e08cce5af73e4766887544d314c5988a06daba0f28e4f7a0feb13c59b180045b0470366e0a84a817c313781b3a57f4fb
7
+ data.tar.gz: b6bcc32532e979d73434bd107d132e6875a8432c281017b1f26ad1f139dc7bc99b2c6a78d8c76fc06d3b4a0a8c025913f5e218b33e46f0120084563955499752
data/.gitignore CHANGED
@@ -9,6 +9,7 @@ InstalledFiles
9
9
  _yardoc
10
10
  coverage
11
11
  doc/
12
+ vendor/
12
13
  lib/bundler/man
13
14
  pkg
14
15
  rdoc
@@ -1,3 +1,21 @@
1
+ ## v0.2.0 2015-08-19
2
+
3
+ ## Added
4
+
5
+ - Support for multi-envs (AMHOL)
6
+
7
+ ## Changed
8
+
9
+ - Updated to work with ROM 0.9.0 and `auto_registration` plugin (AMHOL)
10
+
11
+ ## v0.1.1 2015-07-13
12
+
13
+ TODO
14
+
15
+ ## v0.1.0 2015-07-13
16
+
17
+ TODO
18
+
1
19
  ## v0.0.2 2015-04-05
2
20
 
3
21
  - Hook `ROM.finalize` into `Roda.freeze` so that ROM environment is sealed at the same time as the Roda app.
data/README.md CHANGED
@@ -4,7 +4,7 @@ Roda plugin for [Ruby Object Mapper](https://github.com/rom-rb/rom).
4
4
 
5
5
  ## Issues
6
6
 
7
- Please report any issues in the main [rom-rb/rom](https://github.com/rom-rb/rom/issues) issue tracker.
7
+ Please report any issues in the [issue tracker](https://github.com/rom-rb/rom-roda/issues/new).
8
8
 
9
9
  ## Usage
10
10
 
@@ -21,17 +21,29 @@ Embed a ROM environment in your Roda app using the `plugin` hook:
21
21
 
22
22
  ```ruby
23
23
  class App < Roda
24
- plugin :rom
24
+ plugin :rom, {
25
+ default: {
26
+ setup: :memory
27
+ },
28
+ plugins: [:auto_registration]
29
+ }
25
30
  end
26
31
  ```
27
32
 
28
- Options passed to the plugin are used to set up one or more adapters, using the [repository args format](http://www.rubydoc.info/gems/rom/ROM/Global#setup-instance_method) supported by `ROM.setup`.
33
+ Options passed to the environment under the `:setup` key are used to set up one or more adapters, using the [repository args format](http://www.rubydoc.info/gems/rom/ROM/Global#setup-instance_method) supported by `ROM.setup`.
34
+
35
+ Plugins passed to the environment under the `:plugins` key will be applied to the environment.
29
36
 
30
- To register a single `sql` repository with an Sqlite data source:
37
+ To register a single `sql` repository with an Sqlite data source and use the `auto_registration` environment plugin:
31
38
 
32
39
  ```ruby
33
40
  class App < Roda
34
- plugin :rom, :sql, 'sqlite::memory'
41
+ plugin :rom, {
42
+ default: {
43
+ setup: [:sql, 'sqlite::memory'],
44
+ plugins: [:auto_registration]
45
+ }
46
+ }
35
47
  end
36
48
  ```
37
49
 
@@ -40,22 +52,72 @@ To register a 'default' and a 'warehouse' repository:
40
52
  ```ruby
41
53
  class App < Roda
42
54
  plugin :rom, {
43
- default: [:sql, 'sqlite::memory'],
44
- warehouse: [:sql, 'postgres://localhost/warehouse']
55
+ default: {
56
+ setup: {
57
+ default: [:sql, 'sqlite::memory'],
58
+ warehouse: [:sql, 'postgres://localhost/warehouse']
59
+ }
60
+ }
61
+ }
62
+ end
63
+ ```
64
+
65
+ You can also register multiple ROM environments, when doing so, it can be useful to pass configuration options to environment plugins, the following example will register items defined in the `Web` namespace with the `memory` environment and items defined in the `API` namespace with the `sql` environment:
66
+
67
+ ```ruby
68
+ class App < Roda
69
+ plugin :rom, {
70
+ memory: {
71
+ setup: :memory,
72
+ plugins: {
73
+ auto_registration: {
74
+ if: ->(item) { item.to_s[/(.*)(?=::)/] == 'Web' }
75
+ }
76
+ }
77
+ },
78
+ sql: {
79
+ setup: [:sql, 'sqlite::memory'],
80
+ plugins: {
81
+ auto_registration: {
82
+ if: ->(item) { item.to_s[/(.*)(?=::)/] == 'API' }
83
+ }
84
+ }
85
+ }
45
86
  }
46
87
  end
47
88
  ```
48
89
 
49
90
  ### Loading ROM components
50
91
 
51
- Relations, commands and mappers are automatically registered with the ROM environment when their classes are loaded. This means that these components must be required by the app in a specific order during setup.
92
+ Relations, commands and mappers need to be registered with the ROM environment before the environment is finalized (this can also be done via the `auto_registration` plugin). This means that these components must be required by the app in a specific order during setup.
52
93
 
53
94
  You can autoload ROM components from a local path in your app by passing the `:load_path` option to the plugin:
54
95
 
55
96
  ```ruby
56
97
  class App < Roda
57
98
  # register ROM components from './models'
58
- plugin :rom, :sql, 'sqlite::memory', load_path: 'models'
99
+ plugin :rom, {
100
+ default: {
101
+ setup: [:sql, 'sqlite::memory'],
102
+ plugins: [:auto_registration]
103
+ },
104
+ load_path: 'models'
105
+ }
106
+ end
107
+ ```
108
+
109
+ You can also pass an array of paths to be autoloaded:
110
+
111
+ ```ruby
112
+ class App < Roda
113
+ # register ROM components from './models' and './lib'
114
+ plugin :rom, {
115
+ default: {
116
+ setup: [:sql, 'sqlite::memory'],
117
+ plugins: [:auto_registration]
118
+ },
119
+ load_paths: ['models', 'lib']
120
+ }
59
121
  end
60
122
  ```
61
123
 
@@ -67,7 +129,13 @@ class MyApplication < Roda
67
129
  opts[:root] = 'app'
68
130
 
69
131
  # register ROM components from './app/model'
70
- plugin :rom, :sql, 'sqlite::memory', load_path: 'model'
132
+ plugin :rom, {
133
+ default: {
134
+ setup: [:sql, 'sqlite::memory'],
135
+ plugins: [:auto_registration]
136
+ },
137
+ load_path: 'model'
138
+ }
71
139
  end
72
140
  ```
73
141
 
@@ -81,27 +149,38 @@ Provides an instance of the ROM environment:
81
149
 
82
150
  ```ruby
83
151
  route do |r|
84
- rom
152
+ rom # access :default ROM environment
153
+ rom(:memory) # access :memory ROM environment
85
154
  end
86
155
  ```
87
156
 
88
- #### relation(name)
157
+ #### relation(name, environment = :default)
89
158
 
90
159
  Provides an instance of the named relation:
91
160
 
92
161
  ```ruby
93
162
  route do |r|
94
163
  r.on('catalog') do
164
+ # access :products relation from the :default ROM environment
95
165
  @products = relation(:products).in_stock
96
166
 
97
167
  r.is('category/:category') do |category|
98
168
  @products.by_category(category)
99
169
  end
100
170
  end
171
+
172
+ r.on('users') do
173
+ # access :users relation from the :sql ROM environment
174
+ @users = relation(:users, :sql)
175
+
176
+ r.is(':user_id') do |user_id|
177
+ @users.by_id(user_id)
178
+ end
179
+ end
101
180
  end
102
181
  ```
103
182
 
104
- #### command(name)
183
+ #### command(name, environment = :default)
105
184
 
106
185
  Provides an instance of the named command:
107
186
 
@@ -109,9 +188,17 @@ Provides an instance of the named command:
109
188
  route do |r|
110
189
  r.is('products') do
111
190
  r.post do
191
+ # access :products commands from :default ROM environment
112
192
  command(:products).create.call(r['product'])
113
193
  end
114
194
  end
195
+
196
+ r.is('users') do
197
+ r.post do
198
+ # access :users commands from :sql ROM environment
199
+ command(:users, :sql).create.call(r['user'])
200
+ end
201
+ end
115
202
  end
116
203
  ```
117
204
 
@@ -1,43 +1,63 @@
1
1
  module ROM
2
2
  module Roda
3
3
  module Plugin
4
- def self.configure(app, *args)
5
- args = args.dup
4
+ class << self
5
+ attr_reader :environments
6
6
 
7
- if args.last.is_a?(Hash) && args.last.key?(:load_path)
8
- load_path = File.expand_path(args.pop.delete(:load_path), app.opts[:root])
9
- end
7
+ def configure(app, config)
8
+ load_paths = [*(config.delete(:load_path) || config.delete(:load_paths))].map do |path|
9
+ File.expand_path(
10
+ path,
11
+ app.opts[:root]
12
+ )
13
+ end if config[:load_path] || config[:load_paths]
10
14
 
11
- ROM.setup(*args)
15
+ @environments = config.each_with_object({}) do |(env_name, env_config), container|
16
+ container[env_name] = ROM::Environment.new.tap do |env|
17
+ env.setup(*[env_config.fetch(:setup)].flatten)
12
18
 
13
- self.load_files(load_path) if load_path
14
- end
19
+ env_config.fetch(:plugins, {}).each do |*args|
20
+ env.use(*args.flatten)
21
+ end
22
+ end
23
+ end
24
+
25
+ load_paths.map { |path| load_files(path) } if load_paths
26
+ end
15
27
 
16
- def self.load_files(path)
17
- Dir["#{path}/**/*.rb"].each do |class_file|
18
- require class_file
28
+ def load_files(path)
29
+ Dir["#{path}/**/*.rb"].each do |class_file|
30
+ require class_file
31
+ end
19
32
  end
20
33
  end
21
34
 
22
35
  module ClassMethods
23
36
  def freeze
24
- ROM.finalize
37
+ @__rom__ = ROM::Roda::Plugin.environments.each_with_object({}) do |(name, env), container|
38
+ container[name] = env.finalize.container
39
+ end
25
40
 
26
41
  super
27
42
  end
43
+
44
+ def rom(environment = nil)
45
+ environment = :default if environment.nil?
46
+ @__rom__.fetch(environment)
47
+ end
28
48
  end
29
49
 
30
50
  module InstanceMethods
31
- def rom
32
- ROM.env
51
+ def rom(environment = nil)
52
+ self.class.rom(environment)
33
53
  end
34
54
 
35
- def relation(name)
36
- ROM.env.relation(name)
55
+ def relation(name, environment = nil)
56
+ rom(environment).relation(name)
37
57
  end
38
58
 
39
- def command(name)
40
- ROM.env.command(name)
59
+ def command(name, environment = nil)
60
+ rom(environment).command(name)
41
61
  end
42
62
  end
43
63
  end
@@ -1,5 +1,5 @@
1
1
  module ROM
2
2
  module Roda
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_runtime_dependency 'rom', '~> 0.8', '>= 0.8.1'
20
+ spec.add_runtime_dependency 'rom', '~> 0.9', '>= 0.9.0'
21
21
  spec.add_runtime_dependency 'inflecto'
22
22
 
23
23
  spec.add_development_dependency 'bundler'
@@ -1,11 +1,95 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ROM::Roda::Plugin do
4
- it 'uses load_path relative to application by default' do
5
- subject.should_receive(:load_files).with(File.expand_path('models'))
4
+ before do
5
+ allow(ROM::Roda::Plugin).to receive(:load_files)
6
+ end
7
+
8
+ before do
9
+ Class.new(Roda).plugin :rom, config
10
+ end
11
+
12
+ context 'single path' do
13
+ context 'under :load_path as string' do
14
+ let(:config) do
15
+ {
16
+ default: {
17
+ setup: :memory
18
+ },
19
+ load_path: 'models'
20
+ }
21
+ end
22
+
23
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('models')) }
24
+ end
25
+
26
+ context 'under :load_path as array' do
27
+ let(:config) do
28
+ {
29
+ default: {
30
+ setup: :memory
31
+ },
32
+ load_path: ['models']
33
+ }
34
+ end
35
+
36
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('models')) }
37
+ end
38
+
39
+ context 'under :load_paths as string' do
40
+ let(:config) do
41
+ {
42
+ default: {
43
+ setup: :memory
44
+ },
45
+ load_paths: 'models'
46
+ }
47
+ end
48
+
49
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('models')) }
50
+ end
51
+
52
+ context 'under :load_paths as string' do
53
+ let(:config) do
54
+ {
55
+ default: {
56
+ setup: :memory
57
+ },
58
+ load_paths: ['models']
59
+ }
60
+ end
61
+
62
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('models')) }
63
+ end
64
+ end
65
+
66
+ context 'multiple paths' do
67
+ context 'under :load_path' do
68
+ let(:config) do
69
+ {
70
+ default: {
71
+ setup: :memory
72
+ },
73
+ load_path: ['api/models', 'web/models']
74
+ }
75
+ end
76
+
77
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('api/models')) }
78
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('web/models')) }
79
+ end
80
+
81
+ context 'under :load_paths' do
82
+ let(:config) do
83
+ {
84
+ default: {
85
+ setup: :memory
86
+ },
87
+ load_paths: ['api/models', 'web/models']
88
+ }
89
+ end
6
90
 
7
- class RelativeLoadPathExample < Roda
8
- plugin :rom, :memory, load_path: 'models'
91
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('api/models')) }
92
+ it { expect(ROM::Roda::Plugin).to have_received(:load_files).with(File.expand_path('web/models')) }
9
93
  end
10
94
  end
11
95
  end
@@ -6,12 +6,16 @@ describe ROM::Roda::Plugin do
6
6
  context 'single adapter' do
7
7
  let(:default_gateway) do
8
8
  class MemoryAdapterExample < Roda
9
- plugin :rom, :memory
9
+ plugin :rom, {
10
+ default: {
11
+ setup: :memory
12
+ }
13
+ }
10
14
  end
11
15
 
12
16
  MemoryAdapterExample.freeze
13
17
 
14
- ROM.env.gateways[:default]
18
+ MemoryAdapterExample.rom.gateways[:default]
15
19
  end
16
20
 
17
21
  it 'configures gateway with empty args' do
@@ -22,12 +26,16 @@ describe ROM::Roda::Plugin do
22
26
  context 'single adapter with settings' do
23
27
  let(:default_gateway) do
24
28
  class SqliteAdapterExample < Roda
25
- plugin :rom, :sql, SQL_DSN
29
+ plugin :rom, {
30
+ default: {
31
+ setup: [:sql, SQL_DSN]
32
+ }
33
+ }
26
34
  end
27
35
 
28
36
  SqliteAdapterExample.freeze
29
37
 
30
- ROM.env.gateways[:default]
38
+ SqliteAdapterExample.rom.gateways[:default]
31
39
  end
32
40
 
33
41
  it 'configures gateway with a connection string' do
@@ -39,15 +47,19 @@ describe ROM::Roda::Plugin do
39
47
  let(:gateways) do
40
48
  class MultipleAdaptersExample < Roda
41
49
  plugin :rom, {
42
- default: [:sql, SQL_DSN],
43
- warehouse: [:sql, SQL_DSN],
44
- transient: :memory
50
+ default: {
51
+ setup: {
52
+ default: [:sql, SQL_DSN],
53
+ warehouse: [:sql, SQL_DSN],
54
+ transient: :memory
55
+ }
56
+ }
45
57
  }
46
58
  end
47
59
 
48
60
  MultipleAdaptersExample.freeze
49
61
 
50
- ROM.env.gateways
62
+ MultipleAdaptersExample.rom.gateways
51
63
  end
52
64
 
53
65
  it 'configures gateways with given settings' do
@@ -56,4 +68,100 @@ describe ROM::Roda::Plugin do
56
68
  expect(gateways[:transient]).to be_a(ROM::Memory::Gateway)
57
69
  end
58
70
  end
71
+
72
+ context 'multiple environments' do
73
+ let(:gateways) do
74
+ class MultipleEnvironmentsExample < Roda
75
+ plugin :rom, {
76
+ memory: {
77
+ setup: :memory
78
+ },
79
+ sql: {
80
+ setup: {
81
+ default: [:sql, SQL_DSN],
82
+ warehouse: [:sql, SQL_DSN],
83
+ transient: :memory
84
+ }
85
+ }
86
+ }
87
+ end
88
+
89
+ MultipleEnvironmentsExample.freeze
90
+
91
+ {
92
+ memory: MultipleEnvironmentsExample.rom(:memory).gateways,
93
+ sql: MultipleEnvironmentsExample.rom(:sql).gateways
94
+ }
95
+ end
96
+
97
+ it 'configures gateways with given settings' do
98
+ expect(gateways[:memory][:default]).to be_a(ROM::Memory::Gateway)
99
+ expect(gateways[:sql][:default]).to be_a(ROM::SQL::Gateway)
100
+ expect(gateways[:sql][:warehouse]).to be_a(ROM::SQL::Gateway)
101
+ expect(gateways[:sql][:transient]).to be_a(ROM::Memory::Gateway)
102
+ end
103
+ end
104
+
105
+ context 'with environment plugin settings' do
106
+ context 'without plugin config' do
107
+ let(:relations) do
108
+ class PluginExample < Roda
109
+ plugin :rom, {
110
+ default: {
111
+ setup: :memory,
112
+ plugins: [:auto_registration]
113
+ }
114
+ }
115
+ end
116
+
117
+ class Users < ROM::Relation[:memory]
118
+ dataset :users
119
+ end
120
+
121
+ PluginExample.freeze
122
+
123
+ PluginExample.rom.relations
124
+ end
125
+
126
+ it 'configures gateway with a connection string' do
127
+ expect(relations[:users]).to be_a(ROM::Memory::Relation)
128
+ end
129
+ end
130
+
131
+ context 'with plugin config' do
132
+ let(:relations) do
133
+ class PluginWithConfigExample < Roda
134
+ plugin :rom, {
135
+ default: {
136
+ setup: :memory,
137
+ plugins: {
138
+ auto_registration: {
139
+ if: ->(item) { item.to_s[/(.*)(?=::)/] == 'API' }
140
+ }
141
+ }
142
+ }
143
+ }
144
+ end
145
+
146
+ class Posts < ROM::Relation[:memory]
147
+ dataset :posts
148
+ end
149
+
150
+ module API
151
+ class Comments < ROM::Relation[:memory]
152
+ dataset :comments
153
+ end
154
+ end
155
+
156
+ PluginWithConfigExample.freeze
157
+
158
+ PluginWithConfigExample.rom.relations
159
+ end
160
+
161
+ it 'configures gateway with a connection string' do
162
+ expect { relations[:posts] }.to raise_error ROM::Registry::ElementNotFoundError
163
+ expect(relations[:comments]).to be_a(ROM::Memory::Relation)
164
+ end
165
+ end
166
+ end
59
167
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-roda
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
  - Mark Rickerby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-13 00:00:00.000000000 Z
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rom
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.8'
19
+ version: '0.9'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.8.1
22
+ version: 0.9.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0.8'
29
+ version: '0.9'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.8.1
32
+ version: 0.9.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: inflecto
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -127,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  version: '0'
128
128
  requirements: []
129
129
  rubyforge_project:
130
- rubygems_version: 2.2.2
130
+ rubygems_version: 2.4.5
131
131
  signing_key:
132
132
  specification_version: 4
133
133
  summary: Integrate Ruby Object Mapper with Roda
@@ -135,4 +135,3 @@ test_files:
135
135
  - spec/integration/load_path_spec.rb
136
136
  - spec/integration/plugin_setup_spec.rb
137
137
  - spec/spec_helper.rb
138
- has_rdoc: