rom 0.6.0.beta2 → 0.6.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/rom/env.rb +3 -13
- data/lib/rom/lint/linter.rb +14 -0
- data/lib/rom/lint/repository.rb +18 -2
- data/lib/rom/mapper_registry.rb +5 -0
- data/lib/rom/setup/finalize.rb +17 -7
- data/lib/rom/support/options.rb +24 -21
- data/lib/rom/version.rb +1 -1
- data/spec/unit/rom/mapper_spec.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3860ac36d7e9d938342e022c7fb0e227fc6273e1
|
4
|
+
data.tar.gz: 1a650f4573310fd5d543839c258338344827989b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1239e346a1626da09367e8d4c37a222a43d9a7f70dd248d427dc2046fc1272033913859b38c9e5183d316bd31296c72bdbff4d0a55be590ab9f94f9c907d669
|
7
|
+
data.tar.gz: 4a711b1a776e137d1e953acc7eb8230f5c7c1695d5e8a812b9a7ef23f2673f3fe91619c83b9a93e317dc72e66f2a39b6ce37d082a19ab03110c84abbea8be7e8
|
data/README.md
CHANGED
@@ -26,6 +26,7 @@ Learn more:
|
|
26
26
|
## Adapters
|
27
27
|
|
28
28
|
* [rom-sql](https://github.com/rom-rb/rom-sql)
|
29
|
+
* [rom-yesql](https://github.com/rom-rb/rom-yesql)
|
29
30
|
* [rom-mongo](https://github.com/rom-rb/rom-mongo)
|
30
31
|
* [rom-yaml](https://github.com/rom-rb/rom-yaml)
|
31
32
|
* [rom-csv](https://github.com/rom-rb/rom-csv)
|
@@ -66,6 +67,7 @@ end
|
|
66
67
|
# could define it explicitly
|
67
68
|
class UserMapper < ROM::Mapper
|
68
69
|
relation :users
|
70
|
+
register_as :entity
|
69
71
|
|
70
72
|
model User
|
71
73
|
|
@@ -89,7 +91,7 @@ rom.command(:users).create.call(name: "Joe", age: 17)
|
|
89
91
|
rom.command(:users).create.call(name: "Jane", age: 18)
|
90
92
|
|
91
93
|
# reading relations using defined mappers
|
92
|
-
puts rom.relation(:users) { |r| r.by_name("Jane").adults }.as(:
|
94
|
+
puts rom.relation(:users) { |r| r.by_name("Jane").adults }.as(:entity).to_a.inspect
|
93
95
|
# => [#<User:0x007fdba161cc48 @id=2, @name="Jane", @age=18>]
|
94
96
|
```
|
95
97
|
|
data/lib/rom/env.rb
CHANGED
@@ -33,12 +33,12 @@ module ROM
|
|
33
33
|
attr_reader :mappers
|
34
34
|
|
35
35
|
# @api private
|
36
|
-
def initialize(repositories, relations,
|
36
|
+
def initialize(repositories, relations, mappers, commands, readers = nil)
|
37
37
|
@repositories = repositories
|
38
38
|
@relations = relations
|
39
|
-
@
|
39
|
+
@mappers = mappers
|
40
40
|
@commands = commands
|
41
|
-
|
41
|
+
@readers = readers
|
42
42
|
freeze
|
43
43
|
end
|
44
44
|
|
@@ -109,15 +109,5 @@ module ROM
|
|
109
109
|
def command(name)
|
110
110
|
commands[name]
|
111
111
|
end
|
112
|
-
|
113
|
-
private
|
114
|
-
|
115
|
-
# @api private
|
116
|
-
def initialize_mappers
|
117
|
-
elements = readers.each_with_object({}) { |(name, reader), h|
|
118
|
-
h[name] = reader.mappers
|
119
|
-
}
|
120
|
-
@mappers = Registry.new(elements)
|
121
|
-
end
|
122
112
|
end
|
123
113
|
end
|
data/lib/rom/lint/linter.rb
CHANGED
@@ -36,7 +36,9 @@ module ROM
|
|
36
36
|
#
|
37
37
|
# @api public
|
38
38
|
def lint(name)
|
39
|
+
before_lint
|
39
40
|
public_send name
|
41
|
+
after_lint
|
40
42
|
true # for assertions
|
41
43
|
end
|
42
44
|
|
@@ -59,6 +61,18 @@ module ROM
|
|
59
61
|
def complain(*args)
|
60
62
|
raise Failure, *args
|
61
63
|
end
|
64
|
+
|
65
|
+
# Hook method executed before each lint method run
|
66
|
+
#
|
67
|
+
# @api private
|
68
|
+
def before_lint
|
69
|
+
end
|
70
|
+
|
71
|
+
# Hook method executed after each lint method run
|
72
|
+
#
|
73
|
+
# @api private
|
74
|
+
def after_lint
|
75
|
+
end
|
62
76
|
end
|
63
77
|
end
|
64
78
|
end
|
data/lib/rom/lint/repository.rb
CHANGED
@@ -31,6 +31,7 @@ module ROM
|
|
31
31
|
@identifier = identifier
|
32
32
|
@repository = repository
|
33
33
|
@uri = uri
|
34
|
+
@repository_instance = setup_repository_instance
|
34
35
|
end
|
35
36
|
|
36
37
|
# Lint: Ensure that +repository+ setups up its instance
|
@@ -63,16 +64,31 @@ module ROM
|
|
63
64
|
complain "#{repository_instance} must respond to dataset?"
|
64
65
|
end
|
65
66
|
|
67
|
+
private
|
68
|
+
|
69
|
+
# Repository instance
|
70
|
+
#
|
71
|
+
# @api private
|
72
|
+
attr_reader :repository_instance
|
73
|
+
|
66
74
|
# Setup repository instance
|
67
75
|
#
|
68
|
-
# @api
|
69
|
-
def
|
76
|
+
# @api private
|
77
|
+
def setup_repository_instance
|
70
78
|
if uri
|
71
79
|
ROM::Repository.setup(identifier, uri)
|
72
80
|
else
|
73
81
|
ROM::Repository.setup(identifier)
|
74
82
|
end
|
75
83
|
end
|
84
|
+
|
85
|
+
# Run Repository#disconnect
|
86
|
+
#
|
87
|
+
# @api private
|
88
|
+
def after_lint
|
89
|
+
super
|
90
|
+
repository_instance.disconnect
|
91
|
+
end
|
76
92
|
end
|
77
93
|
end
|
78
94
|
end
|
data/lib/rom/mapper_registry.rb
CHANGED
@@ -11,6 +11,11 @@ module ROM
|
|
11
11
|
elements.fetch(name) { raise(MapperMissingError, name) }
|
12
12
|
end
|
13
13
|
|
14
|
+
# @api private
|
15
|
+
def key?(name)
|
16
|
+
elements.key?(name)
|
17
|
+
end
|
18
|
+
|
14
19
|
# @api private
|
15
20
|
def by_path(path)
|
16
21
|
elements.fetch(paths(path).detect { |name| elements.key?(name) }) {
|
data/lib/rom/setup/finalize.rb
CHANGED
@@ -50,10 +50,11 @@ module ROM
|
|
50
50
|
infer_schema_relations
|
51
51
|
|
52
52
|
relations = load_relations
|
53
|
-
|
53
|
+
mappers = load_mappers
|
54
54
|
commands = load_commands(relations)
|
55
|
+
readers = load_readers(relations, mappers)
|
55
56
|
|
56
|
-
Env.new(repositories, relations,
|
57
|
+
Env.new(repositories, relations, mappers, commands, readers)
|
57
58
|
end
|
58
59
|
|
59
60
|
private
|
@@ -81,19 +82,28 @@ module ROM
|
|
81
82
|
RelationRegistry.new(relations)
|
82
83
|
end
|
83
84
|
|
85
|
+
# @api private
|
86
|
+
def load_mappers
|
87
|
+
mapper_registry = Mapper.registry(mapper_classes).each_with_object({})
|
88
|
+
registry_hash = mapper_registry.each do |(name, mappers), h|
|
89
|
+
h[name] = MapperRegistry.new(mappers)
|
90
|
+
end
|
91
|
+
Registry.new(registry_hash)
|
92
|
+
end
|
93
|
+
|
84
94
|
# Build entire reader and mapper registries
|
85
95
|
#
|
86
96
|
# @api private
|
87
|
-
def load_readers(relations)
|
97
|
+
def load_readers(relations, mappers)
|
88
98
|
readers = {}
|
89
99
|
|
90
|
-
|
100
|
+
mappers.each do |name, rel_mappers|
|
101
|
+
next unless rel_mappers.key?(name)
|
102
|
+
|
91
103
|
relation = relations[name]
|
92
104
|
methods = relation.exposed_relations
|
93
105
|
|
94
|
-
readers[name] = Reader.build(
|
95
|
-
name, relation, MapperRegistry.new(mappers), methods
|
96
|
-
)
|
106
|
+
readers[name] = Reader.build(name, relation, rel_mappers, methods)
|
97
107
|
end
|
98
108
|
|
99
109
|
ReaderRegistry.new(readers)
|
data/lib/rom/support/options.rb
CHANGED
@@ -45,12 +45,17 @@ module ROM
|
|
45
45
|
@reader = options.fetch(:reader) { false }
|
46
46
|
@allow = options.fetch(:allow) { [] }
|
47
47
|
@default = options.fetch(:default) { Undefined }
|
48
|
+
@ivar = :"@#{name}" if @reader
|
48
49
|
end
|
49
50
|
|
50
51
|
def reader?
|
51
52
|
@reader
|
52
53
|
end
|
53
54
|
|
55
|
+
def assign_reader_value(object, value)
|
56
|
+
object.instance_variable_set(@ivar, value)
|
57
|
+
end
|
58
|
+
|
54
59
|
def default?
|
55
60
|
@default != Undefined
|
56
61
|
end
|
@@ -85,22 +90,19 @@ module ROM
|
|
85
90
|
@options[option.name] = option
|
86
91
|
end
|
87
92
|
|
88
|
-
def
|
89
|
-
options
|
90
|
-
validate_option_value(name, value)
|
91
|
-
end
|
92
|
-
end
|
93
|
+
def process(object, options)
|
94
|
+
ensure_known_options(options)
|
93
95
|
|
94
|
-
def set_defaults(object, options)
|
95
96
|
each do |name, option|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
97
|
+
if option.default? && !options.key?(name)
|
98
|
+
options[name] = option.default_value(object)
|
99
|
+
end
|
100
100
|
|
101
|
-
|
102
|
-
|
103
|
-
|
101
|
+
if options.key?(name)
|
102
|
+
validate_option_value(option, name, options[name])
|
103
|
+
end
|
104
|
+
|
105
|
+
option.assign_reader_value(object, options[name]) if option.reader?
|
104
106
|
end
|
105
107
|
end
|
106
108
|
|
@@ -110,12 +112,16 @@ module ROM
|
|
110
112
|
@options.each(&block)
|
111
113
|
end
|
112
114
|
|
113
|
-
def
|
114
|
-
|
115
|
-
|
116
|
-
|
115
|
+
def ensure_known_options(options)
|
116
|
+
options.each_key do |name|
|
117
|
+
@options.fetch(name) do
|
118
|
+
raise InvalidOptionKeyError,
|
119
|
+
"#{name.inspect} is not a valid option"
|
120
|
+
end
|
117
121
|
end
|
122
|
+
end
|
118
123
|
|
124
|
+
def validate_option_value(option, name, value)
|
119
125
|
unless option.type_matches?(value)
|
120
126
|
raise InvalidOptionValueError,
|
121
127
|
"#{name.inspect}:#{value.inspect} has incorrect type"
|
@@ -178,10 +184,7 @@ module ROM
|
|
178
184
|
# @param [Array] args
|
179
185
|
def initialize(*args)
|
180
186
|
options = args.last ? args.last.dup : {}
|
181
|
-
|
182
|
-
definitions.set_defaults(self, options)
|
183
|
-
definitions.validate_options(options)
|
184
|
-
definitions.set_option_values(self, options)
|
187
|
+
self.class.option_definitions.process(self, options)
|
185
188
|
@options = options.freeze
|
186
189
|
end
|
187
190
|
end
|
data/lib/rom/version.rb
CHANGED
@@ -30,15 +30,17 @@ describe ROM::Mapper do
|
|
30
30
|
describe '.registry' do
|
31
31
|
it 'builds mapper class registry for base and virtual relations' do
|
32
32
|
users = Class.new(ROM::Mapper) { relation(:users) }
|
33
|
+
entity = Class.new(ROM::Mapper) { relation(:users); register_as(:entity) }
|
33
34
|
active = Class.new(users) { relation(:active) }
|
34
35
|
admins = Class.new(users) { relation(:admins) }
|
35
36
|
custom = Class.new(users) { register_as(:custom) }
|
36
37
|
|
37
|
-
registry = ROM::Mapper.registry([users, active, admins, custom])
|
38
|
+
registry = ROM::Mapper.registry([users, entity, active, admins, custom])
|
38
39
|
|
39
40
|
expect(registry).to eql(
|
40
41
|
users: {
|
41
42
|
users: users.build,
|
43
|
+
entity: entity.build,
|
42
44
|
active: active.build,
|
43
45
|
admins: admins.build,
|
44
46
|
custom: custom.build
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.0.
|
4
|
+
version: 0.6.0.beta3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: transproc
|