rom-relation 0.1.0
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.
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +21 -0
- data/.yardopts +4 -0
- data/Gemfile +24 -0
- data/Gemfile.devtools +55 -0
- data/Guardfile +25 -0
- data/LICENSE +20 -0
- data/README.md +21 -0
- data/Rakefile +4 -0
- data/TODO.md +4 -0
- data/config/devtools.yml +2 -0
- data/config/flay.yml +3 -0
- data/config/flog.yml +2 -0
- data/config/mutant.yml +8 -0
- data/config/reek.yml +97 -0
- data/config/rubocop.yml +41 -0
- data/lib/rom/environment.rb +133 -0
- data/lib/rom/mapping/definition.rb +127 -0
- data/lib/rom/mapping.rb +81 -0
- data/lib/rom/relation.rb +339 -0
- data/lib/rom/repository.rb +62 -0
- data/lib/rom/schema/definition/relation/base.rb +25 -0
- data/lib/rom/schema/definition/relation.rb +44 -0
- data/lib/rom/schema/definition.rb +82 -0
- data/lib/rom/schema.rb +49 -0
- data/lib/rom/support/axiom/adapter/data_objects.rb +39 -0
- data/lib/rom/support/axiom/adapter/memory.rb +25 -0
- data/lib/rom/support/axiom/adapter/postgres.rb +19 -0
- data/lib/rom/support/axiom/adapter/sqlite3.rb +19 -0
- data/lib/rom/support/axiom/adapter.rb +100 -0
- data/lib/rom/version.rb +7 -0
- data/lib/rom-relation.rb +45 -0
- data/rom-relation.gemspec +26 -0
- data/spec/integration/environment_setup_spec.rb +22 -0
- data/spec/integration/mapping_relations_spec.rb +64 -0
- data/spec/integration/schema_definition_spec.rb +94 -0
- data/spec/shared/unit/environment_context.rb +6 -0
- data/spec/shared/unit/relation_context.rb +25 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/support/helper.rb +17 -0
- data/spec/support/test_mapper.rb +23 -0
- data/spec/unit/rom/environment/class_methods/setup_spec.rb +25 -0
- data/spec/unit/rom/environment/element_reader_spec.rb +23 -0
- data/spec/unit/rom/environment/mapping_spec.rb +26 -0
- data/spec/unit/rom/environment/repository_spec.rb +21 -0
- data/spec/unit/rom/environment/schema_spec.rb +33 -0
- data/spec/unit/rom/mapping/class_methods/build_spec.rb +77 -0
- data/spec/unit/rom/relation/class_methods/build_spec.rb +19 -0
- data/spec/unit/rom/relation/delete_spec.rb +15 -0
- data/spec/unit/rom/relation/drop_spec.rb +11 -0
- data/spec/unit/rom/relation/each_spec.rb +23 -0
- data/spec/unit/rom/relation/first_spec.rb +19 -0
- data/spec/unit/rom/relation/inject_mapper_spec.rb +17 -0
- data/spec/unit/rom/relation/insert_spec.rb +13 -0
- data/spec/unit/rom/relation/last_spec.rb +19 -0
- data/spec/unit/rom/relation/one_spec.rb +49 -0
- data/spec/unit/rom/relation/replace_spec.rb +13 -0
- data/spec/unit/rom/relation/restrict_spec.rb +25 -0
- data/spec/unit/rom/relation/sort_by_spec.rb +25 -0
- data/spec/unit/rom/relation/take_spec.rb +11 -0
- data/spec/unit/rom/relation/to_a_spec.rb +20 -0
- data/spec/unit/rom/relation/update_spec.rb +25 -0
- data/spec/unit/rom/repository/class_methods/build_spec.rb +27 -0
- data/spec/unit/rom/repository/element_reader_spec.rb +21 -0
- data/spec/unit/rom/repository/element_writer_spec.rb +18 -0
- data/spec/unit/rom/schema/class_methods/build_spec.rb +103 -0
- metadata +249 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rom
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.9.3
|
data/.travis.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
language: ruby
|
|
2
|
+
bundler_args: --without yard guard benchmarks
|
|
3
|
+
script: "bundle exec rake ci"
|
|
4
|
+
rvm:
|
|
5
|
+
- 1.9.3
|
|
6
|
+
- 2.0.0
|
|
7
|
+
- rbx-19mode
|
|
8
|
+
- ruby-head
|
|
9
|
+
matrix:
|
|
10
|
+
allow_failures:
|
|
11
|
+
- rvm: jruby-19mode
|
|
12
|
+
- rvm: ruby-head
|
|
13
|
+
notifications:
|
|
14
|
+
irc:
|
|
15
|
+
channels:
|
|
16
|
+
- "irc.freenode.org#rom-rb"
|
|
17
|
+
on_success: never
|
|
18
|
+
on_failure: change
|
|
19
|
+
email:
|
|
20
|
+
on_success: never
|
|
21
|
+
on_failure: change
|
data/.yardopts
ADDED
data/Gemfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
source 'https://rubygems.org'
|
|
4
|
+
|
|
5
|
+
gemspec
|
|
6
|
+
|
|
7
|
+
gem 'rom-relation', path: '.'
|
|
8
|
+
|
|
9
|
+
gem 'axiom', git: 'https://github.com/dkubb/axiom.git'
|
|
10
|
+
gem 'rom-mapper', git: 'https://github.com/rom-rb/rom-mapper.git'
|
|
11
|
+
|
|
12
|
+
group :test do
|
|
13
|
+
gem 'bogus', '~> 0.1'
|
|
14
|
+
gem 'randexp'
|
|
15
|
+
gem 'ruby-graphviz'
|
|
16
|
+
gem 'axiom-memory-adapter', git: 'https://github.com/dkubb/axiom-memory-adapter.git'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
group :development do
|
|
20
|
+
gem 'devtools', git: 'https://github.com/rom-rb/devtools.git'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# added by devtools
|
|
24
|
+
eval_gemfile 'Gemfile.devtools'
|
data/Gemfile.devtools
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
group :development do
|
|
4
|
+
gem 'rake', '~> 10.1.0'
|
|
5
|
+
gem 'rspec', '~> 2.14.1'
|
|
6
|
+
gem 'yard', '~> 0.8.7'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
group :yard do
|
|
10
|
+
gem 'kramdown', '~> 1.1.0'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
group :guard do
|
|
14
|
+
gem 'guard', '~> 1.8.1'
|
|
15
|
+
gem 'guard-bundler', '~> 1.0.0'
|
|
16
|
+
gem 'guard-rspec', '~> 3.0.2'
|
|
17
|
+
gem 'guard-rubocop', '~> 0.2.0'
|
|
18
|
+
gem 'guard-mutant', '~> 0.0.1'
|
|
19
|
+
|
|
20
|
+
# file system change event handling
|
|
21
|
+
gem 'listen', '~> 1.2.2'
|
|
22
|
+
gem 'rb-fchange', '~> 0.0.6', require: false
|
|
23
|
+
gem 'rb-fsevent', '~> 0.9.3', require: false
|
|
24
|
+
gem 'rb-inotify', '~> 0.9.0', require: false
|
|
25
|
+
|
|
26
|
+
# notification handling
|
|
27
|
+
gem 'libnotify', '~> 0.8.0', require: false
|
|
28
|
+
gem 'rb-notifu', '~> 0.0.4', require: false
|
|
29
|
+
gem 'terminal-notifier-guard', '~> 1.5.3', require: false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
group :metrics do
|
|
33
|
+
gem 'coveralls', '~> 0.6.7'
|
|
34
|
+
gem 'flay', '~> 2.4.0'
|
|
35
|
+
gem 'flog', '~> 4.1.1'
|
|
36
|
+
gem 'reek', '~> 1.3.2'
|
|
37
|
+
gem 'rubocop', '~> 0.11.0'
|
|
38
|
+
gem 'simplecov', '~> 0.7.1'
|
|
39
|
+
gem 'yardstick', '~> 0.9.7', git: 'https://github.com/dkubb/yardstick.git'
|
|
40
|
+
|
|
41
|
+
platforms :ruby_19, :ruby_20 do
|
|
42
|
+
gem 'mutant', git: 'https://github.com/mbj/mutant.git'
|
|
43
|
+
gem 'yard-spellcheck', '~> 0.1.5'
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
group :benchmarks do
|
|
48
|
+
gem 'rbench', '~> 0.2.3'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
platform :jruby do
|
|
52
|
+
group :jruby do
|
|
53
|
+
gem 'jruby-openssl', '~> 0.8.5'
|
|
54
|
+
end
|
|
55
|
+
end
|
data/Guardfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
guard :rspec do
|
|
2
|
+
#run all specs if configuration is modified
|
|
3
|
+
watch('Guardfile') { 'spec' }
|
|
4
|
+
watch('Gemfile.lock') { 'spec' }
|
|
5
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
|
6
|
+
|
|
7
|
+
# run all specs if supporting files files are modified
|
|
8
|
+
watch(%r{\Aspec/(?:lib|support|shared)/.+\.rb\z}) { 'spec' }
|
|
9
|
+
|
|
10
|
+
# run unit specs if associated lib code is modified
|
|
11
|
+
watch(%r{\Alib/(.+)\.rb\z}) { |m| Dir["spec/unit/#{m[1]}"] }
|
|
12
|
+
watch(%r{\Alib/(.+)/support/(.+)\.rb\z}) { |m| Dir["spec/unit/#{m[1]}/#{m[2]}"] }
|
|
13
|
+
watch("lib/#{File.basename(File.expand_path('../', __FILE__))}.rb") { 'spec' }
|
|
14
|
+
|
|
15
|
+
# run a spec if it is modified
|
|
16
|
+
watch(%r{\Aspec/(?:unit|integration)/.+_spec\.rb\z})
|
|
17
|
+
|
|
18
|
+
notification :tmux, :display_message => true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
guard :rubocop, cli: %w[--config config/rubocop.yml] do
|
|
22
|
+
watch(%r{.+\.(?:rb|rake)\z})
|
|
23
|
+
watch(%r{\Aconfig/rubocop\.yml\z}) { |m| File.dirname(m[0]) }
|
|
24
|
+
watch(%r{(?:.+/)?\.rubocop\.yml\z}) { |m| File.dirname(m[0]) }
|
|
25
|
+
end
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2013 Ruby Object Mapper Team
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# rom-relation
|
|
2
|
+
|
|
3
|
+
[][gem]
|
|
4
|
+
[][travis]
|
|
5
|
+
[][gemnasium]
|
|
6
|
+
[][codeclimate]
|
|
7
|
+
[][coveralls]
|
|
8
|
+
|
|
9
|
+
[gem]: https://rubygems.org/gems/rom-relation
|
|
10
|
+
[travis]: https://travis-ci.org/rom-rb/rom-relation
|
|
11
|
+
[gemnasium]: https://gemnasium.com/rom-rb/rom-relation
|
|
12
|
+
[codeclimate]: https://codeclimate.com/github/rom-rb/rom-relation
|
|
13
|
+
[coveralls]: https://coveralls.io/r/rom-rb/rom-relation
|
|
14
|
+
|
|
15
|
+
Relations with mappers for [Ruby Object Mapper](http://rom-rb.org).
|
|
16
|
+
|
|
17
|
+
See ROM's [README](https://github.com/rom-rb/rom) for more information.
|
|
18
|
+
|
|
19
|
+
## License
|
|
20
|
+
|
|
21
|
+
See LICENSE file.
|
data/Rakefile
ADDED
data/TODO.md
ADDED
data/config/devtools.yml
ADDED
data/config/flay.yml
ADDED
data/config/flog.yml
ADDED
data/config/mutant.yml
ADDED
data/config/reek.yml
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
---
|
|
2
|
+
Attribute:
|
|
3
|
+
enabled: false
|
|
4
|
+
exclude: []
|
|
5
|
+
BooleanParameter:
|
|
6
|
+
enabled: true
|
|
7
|
+
ClassVariable:
|
|
8
|
+
enabled: true
|
|
9
|
+
exclude: []
|
|
10
|
+
ControlParameter:
|
|
11
|
+
enabled: true
|
|
12
|
+
DataClump:
|
|
13
|
+
enabled: true
|
|
14
|
+
exclude: []
|
|
15
|
+
max_copies: 2
|
|
16
|
+
min_clump_size: 2
|
|
17
|
+
DuplicateMethodCall:
|
|
18
|
+
enabled: false
|
|
19
|
+
exclude: []
|
|
20
|
+
max_calls: 1
|
|
21
|
+
FeatureEnvy:
|
|
22
|
+
enabled: true
|
|
23
|
+
exclude:
|
|
24
|
+
- 'ROM::Mapping#build_relation'
|
|
25
|
+
IrresponsibleModule:
|
|
26
|
+
enabled: true
|
|
27
|
+
exclude: []
|
|
28
|
+
LongParameterList:
|
|
29
|
+
enabled: true
|
|
30
|
+
max_params: 3
|
|
31
|
+
LongYieldList:
|
|
32
|
+
enabled: true
|
|
33
|
+
max_params: 2
|
|
34
|
+
NestedIterators:
|
|
35
|
+
enabled: true
|
|
36
|
+
max_allowed_nesting: 1
|
|
37
|
+
ignore_iterators: []
|
|
38
|
+
NilCheck:
|
|
39
|
+
enabled: true
|
|
40
|
+
RepeatedConditional:
|
|
41
|
+
enabled: true
|
|
42
|
+
exclude: []
|
|
43
|
+
max_ifs: 1
|
|
44
|
+
TooManyInstanceVariables:
|
|
45
|
+
enabled: true
|
|
46
|
+
max_instance_variables: 3
|
|
47
|
+
exclude:
|
|
48
|
+
- ROM::Mapping::Definition
|
|
49
|
+
TooManyMethods:
|
|
50
|
+
enabled: true
|
|
51
|
+
exclude:
|
|
52
|
+
- ROM::Environment
|
|
53
|
+
- ROM::Relation
|
|
54
|
+
max_methods: 10
|
|
55
|
+
TooManyStatements:
|
|
56
|
+
enabled: true
|
|
57
|
+
max_statements: 4
|
|
58
|
+
UncommunicativeMethodName:
|
|
59
|
+
enabled: true
|
|
60
|
+
exclude:
|
|
61
|
+
- 'n' # typically used to denote infinity in ROM code
|
|
62
|
+
reject:
|
|
63
|
+
- !ruby/regexp /^[a-z]$/
|
|
64
|
+
- !ruby/regexp /[0-9]$/
|
|
65
|
+
- !ruby/regexp /[A-Z]/
|
|
66
|
+
accept: []
|
|
67
|
+
UncommunicativeModuleName:
|
|
68
|
+
enabled: true
|
|
69
|
+
exclude:
|
|
70
|
+
- Axiom::Adapter::Sqlite3
|
|
71
|
+
reject:
|
|
72
|
+
- !ruby/regexp /^.$/
|
|
73
|
+
- !ruby/regexp /[0-9]$/
|
|
74
|
+
accept: []
|
|
75
|
+
UncommunicativeParameterName:
|
|
76
|
+
enabled: true
|
|
77
|
+
exclude: []
|
|
78
|
+
reject:
|
|
79
|
+
- !ruby/regexp /^.$/
|
|
80
|
+
- !ruby/regexp /[0-9]$/
|
|
81
|
+
- !ruby/regexp /[A-Z]/
|
|
82
|
+
accept: []
|
|
83
|
+
UncommunicativeVariableName:
|
|
84
|
+
enabled: true
|
|
85
|
+
exclude:
|
|
86
|
+
- 'r' # typically used for 'relation' in blocks
|
|
87
|
+
reject:
|
|
88
|
+
- !ruby/regexp /^.$/
|
|
89
|
+
- !ruby/regexp /[0-9]$/
|
|
90
|
+
- !ruby/regexp /[A-Z]/
|
|
91
|
+
accept: []
|
|
92
|
+
UnusedParameters:
|
|
93
|
+
enabled: true
|
|
94
|
+
exclude: []
|
|
95
|
+
UtilityFunction:
|
|
96
|
+
enabled: true
|
|
97
|
+
max_helper_calls: 0
|
data/config/rubocop.yml
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
Includes:
|
|
3
|
+
- '**/*.rake'
|
|
4
|
+
Excludes:
|
|
5
|
+
- '**/vendor/**'
|
|
6
|
+
- '**/spec/unit/rom/relation/to_a_spec.rb'
|
|
7
|
+
|
|
8
|
+
# Avoid parameter lists longer than five parameters.
|
|
9
|
+
ParameterLists:
|
|
10
|
+
Max: 3
|
|
11
|
+
CountKeywordArgs: true
|
|
12
|
+
|
|
13
|
+
# Avoid more than `Max` levels of nesting.
|
|
14
|
+
BlockNesting:
|
|
15
|
+
Max: 3
|
|
16
|
+
|
|
17
|
+
# Align with the style guide.
|
|
18
|
+
CollectionMethods:
|
|
19
|
+
PreferredMethods:
|
|
20
|
+
collect: 'map'
|
|
21
|
+
inject: 'reduce'
|
|
22
|
+
find: 'detect'
|
|
23
|
+
find_all: 'select'
|
|
24
|
+
|
|
25
|
+
# Do not force public/protected/private keyword to be indented at the same
|
|
26
|
+
# level as the def keyword. My personal preference is to outdent these keywords
|
|
27
|
+
# because I think when scanning code it makes it easier to identify the
|
|
28
|
+
# sections of code and visually separate them. When the keyword is at the same
|
|
29
|
+
# level I think it sort of blends in with the def keywords and makes it harder
|
|
30
|
+
# to scan the code and see where the sections are.
|
|
31
|
+
AccessControl:
|
|
32
|
+
Enabled: false
|
|
33
|
+
|
|
34
|
+
LineLength:
|
|
35
|
+
Max: 94
|
|
36
|
+
|
|
37
|
+
Blocks:
|
|
38
|
+
Enabled: false
|
|
39
|
+
|
|
40
|
+
Documentation:
|
|
41
|
+
Enabled: false
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module ROM
|
|
4
|
+
|
|
5
|
+
# The environment configures repositories and loads schema with relations
|
|
6
|
+
#
|
|
7
|
+
class Environment
|
|
8
|
+
include Concord.new(:repositories, :registry)
|
|
9
|
+
|
|
10
|
+
# Build an environment instance from a repository config hash
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
#
|
|
14
|
+
# config = { 'test' => 'memory://test' }
|
|
15
|
+
# env = ROM::Environment.setup(config)
|
|
16
|
+
#
|
|
17
|
+
# @param [Environment, Hash<#to_sym, String>] config
|
|
18
|
+
# an environment or a hash of adapter uri strings,
|
|
19
|
+
# keyed by repository name
|
|
20
|
+
#
|
|
21
|
+
# @return [Environment]
|
|
22
|
+
#
|
|
23
|
+
# @api public
|
|
24
|
+
def self.setup(config)
|
|
25
|
+
return config if config.kind_of?(self)
|
|
26
|
+
|
|
27
|
+
repositories = config.each_with_object({}) { |(name, uri), hash|
|
|
28
|
+
hash[name.to_sym] = Repository.build(name, Addressable::URI.parse(uri))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
build(repositories)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Build a new environment
|
|
35
|
+
#
|
|
36
|
+
# @param [Hash] repositories
|
|
37
|
+
#
|
|
38
|
+
# @param [Hash] registry for relations
|
|
39
|
+
#
|
|
40
|
+
# @return [Environment]
|
|
41
|
+
#
|
|
42
|
+
# @api private
|
|
43
|
+
def self.build(repositories, registry = {})
|
|
44
|
+
new(repositories, registry)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Build a relation schema for this environment
|
|
48
|
+
#
|
|
49
|
+
# @example
|
|
50
|
+
# env = Environment.coerce(test: 'memory://test')
|
|
51
|
+
#
|
|
52
|
+
# env.schema do
|
|
53
|
+
# base_relation :users do
|
|
54
|
+
# repository :test
|
|
55
|
+
#
|
|
56
|
+
# attribute :id, Integer
|
|
57
|
+
# attribute :name, String
|
|
58
|
+
# end
|
|
59
|
+
# end
|
|
60
|
+
#
|
|
61
|
+
# @return [Schema]
|
|
62
|
+
#
|
|
63
|
+
# @api public
|
|
64
|
+
def schema(&block)
|
|
65
|
+
@schema ||= Schema.build(repositories)
|
|
66
|
+
@schema.call(&block) if block
|
|
67
|
+
@schema
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Define mapping for relations
|
|
71
|
+
#
|
|
72
|
+
# @example
|
|
73
|
+
#
|
|
74
|
+
# env.schema do
|
|
75
|
+
# base_relation :users do
|
|
76
|
+
# repository :test
|
|
77
|
+
#
|
|
78
|
+
# attribute :id, Integer
|
|
79
|
+
# attribtue :user_name, String
|
|
80
|
+
# end
|
|
81
|
+
# end
|
|
82
|
+
#
|
|
83
|
+
# env.mapping do
|
|
84
|
+
# users do
|
|
85
|
+
# model User
|
|
86
|
+
#
|
|
87
|
+
# map :id
|
|
88
|
+
# map :user_name, :to => :name
|
|
89
|
+
# end
|
|
90
|
+
# end
|
|
91
|
+
#
|
|
92
|
+
# @return [Mapping]
|
|
93
|
+
#
|
|
94
|
+
# @api public
|
|
95
|
+
def mapping(&block)
|
|
96
|
+
Mapping.build(self, schema, &block)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Return registered relation
|
|
100
|
+
#
|
|
101
|
+
# @example
|
|
102
|
+
#
|
|
103
|
+
# env[:users]
|
|
104
|
+
#
|
|
105
|
+
# @param [Symbol] relation name
|
|
106
|
+
#
|
|
107
|
+
# @return [Relation]
|
|
108
|
+
#
|
|
109
|
+
# @api public
|
|
110
|
+
def [](name)
|
|
111
|
+
registry[name]
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Register a rom relation
|
|
115
|
+
#
|
|
116
|
+
# @return [Environment]
|
|
117
|
+
#
|
|
118
|
+
# @api private
|
|
119
|
+
def []=(name, relation)
|
|
120
|
+
registry[name] = relation
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The repository with the given +name+
|
|
124
|
+
#
|
|
125
|
+
# @return [Repository]
|
|
126
|
+
#
|
|
127
|
+
# @api public
|
|
128
|
+
def repository(name)
|
|
129
|
+
repositories[name]
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
end # Environment
|
|
133
|
+
end # ROM
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module ROM
|
|
4
|
+
class Mapping
|
|
5
|
+
|
|
6
|
+
# Mapping definition DSL
|
|
7
|
+
#
|
|
8
|
+
# @private
|
|
9
|
+
class Definition
|
|
10
|
+
include Adamantium::Flat
|
|
11
|
+
|
|
12
|
+
attr_reader :mapping, :attributes
|
|
13
|
+
private :mapping, :attributes
|
|
14
|
+
|
|
15
|
+
# Build new mapping definition
|
|
16
|
+
#
|
|
17
|
+
# @api private
|
|
18
|
+
def self.build(header, &block)
|
|
19
|
+
new(header, &block)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Initialize a new Definition instance
|
|
23
|
+
#
|
|
24
|
+
# @return [undefined]
|
|
25
|
+
#
|
|
26
|
+
# @api private
|
|
27
|
+
def initialize(header, &block)
|
|
28
|
+
@header = header
|
|
29
|
+
@mapping = {}
|
|
30
|
+
@attributes = Set.new
|
|
31
|
+
|
|
32
|
+
instance_eval(&block)
|
|
33
|
+
|
|
34
|
+
build_mapper unless mapper
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @api private
|
|
38
|
+
def header
|
|
39
|
+
@header.project(attributes + Set[*mapping.keys])
|
|
40
|
+
end
|
|
41
|
+
memoize :header
|
|
42
|
+
|
|
43
|
+
# Get or set mapper
|
|
44
|
+
#
|
|
45
|
+
# @example
|
|
46
|
+
#
|
|
47
|
+
# Mapping.build do
|
|
48
|
+
# users do
|
|
49
|
+
# mapper my_custom_mapper
|
|
50
|
+
# end
|
|
51
|
+
# end
|
|
52
|
+
#
|
|
53
|
+
# @param [Object]
|
|
54
|
+
#
|
|
55
|
+
# @return [Object]
|
|
56
|
+
#
|
|
57
|
+
# @api public
|
|
58
|
+
def mapper(mapper = Undefined)
|
|
59
|
+
if mapper == Undefined
|
|
60
|
+
@mapper
|
|
61
|
+
else
|
|
62
|
+
@mapper = mapper
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Get or set model for the mapper
|
|
67
|
+
#
|
|
68
|
+
# @example
|
|
69
|
+
#
|
|
70
|
+
# Mapping.build do
|
|
71
|
+
# users do
|
|
72
|
+
# model User
|
|
73
|
+
# end
|
|
74
|
+
# end
|
|
75
|
+
#
|
|
76
|
+
# @param [Class]
|
|
77
|
+
#
|
|
78
|
+
# @return [Class]
|
|
79
|
+
#
|
|
80
|
+
# @api public
|
|
81
|
+
def model(model = Undefined)
|
|
82
|
+
if model == Undefined
|
|
83
|
+
@model
|
|
84
|
+
else
|
|
85
|
+
@model = model
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Configure attribute mappings
|
|
90
|
+
#
|
|
91
|
+
# @example
|
|
92
|
+
#
|
|
93
|
+
# Mapping.build do
|
|
94
|
+
# users do
|
|
95
|
+
# map :id, :email
|
|
96
|
+
# map :user_name, to: :name
|
|
97
|
+
# end
|
|
98
|
+
# end
|
|
99
|
+
#
|
|
100
|
+
# @params [Array<Symbol>,Symbol,Hash]
|
|
101
|
+
#
|
|
102
|
+
# @return [Definition]
|
|
103
|
+
#
|
|
104
|
+
# @api public
|
|
105
|
+
def map(*args)
|
|
106
|
+
options = args.last
|
|
107
|
+
|
|
108
|
+
if options.kind_of?(Hash)
|
|
109
|
+
mapping.update(args.first => options[:to])
|
|
110
|
+
else
|
|
111
|
+
@attributes += Set[*args]
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
# Build default rom mapper
|
|
118
|
+
#
|
|
119
|
+
# @api private
|
|
120
|
+
def build_mapper
|
|
121
|
+
@mapper = Mapper.build(header, model, map: mapping)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
end # Definition
|
|
125
|
+
|
|
126
|
+
end # Mapping
|
|
127
|
+
end # ROM
|