rom 0.5.0 → 0.6.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +19 -15
  3. data/.rubocop_todo.yml +28 -0
  4. data/.travis.yml +8 -1
  5. data/CHANGELOG.md +40 -0
  6. data/Gemfile +10 -2
  7. data/Guardfile +12 -10
  8. data/README.md +42 -43
  9. data/Rakefile +13 -23
  10. data/lib/rom.rb +19 -27
  11. data/lib/rom/command.rb +118 -0
  12. data/lib/rom/command_registry.rb +13 -27
  13. data/lib/rom/commands.rb +1 -59
  14. data/lib/rom/commands/abstract.rb +147 -0
  15. data/lib/rom/commands/composite.rb +47 -0
  16. data/lib/rom/commands/create.rb +2 -17
  17. data/lib/rom/commands/delete.rb +5 -25
  18. data/lib/rom/commands/result.rb +5 -5
  19. data/lib/rom/commands/update.rb +3 -27
  20. data/lib/rom/constants.rb +19 -0
  21. data/lib/rom/env.rb +85 -35
  22. data/lib/rom/global.rb +173 -42
  23. data/lib/rom/header.rb +5 -5
  24. data/lib/rom/header/attribute.rb +2 -2
  25. data/lib/rom/lint/enumerable_dataset.rb +52 -0
  26. data/lib/rom/lint/linter.rb +64 -0
  27. data/lib/rom/lint/repository.rb +78 -0
  28. data/lib/rom/lint/spec.rb +20 -0
  29. data/lib/rom/lint/test.rb +98 -0
  30. data/lib/rom/mapper.rb +32 -5
  31. data/lib/rom/mapper/attribute_dsl.rb +240 -0
  32. data/lib/rom/mapper/dsl.rb +100 -0
  33. data/lib/rom/mapper/model_dsl.rb +55 -0
  34. data/lib/rom/mapper_registry.rb +8 -1
  35. data/lib/rom/memory.rb +4 -0
  36. data/lib/rom/memory/commands.rb +46 -0
  37. data/lib/rom/memory/dataset.rb +72 -0
  38. data/lib/rom/memory/relation.rb +44 -0
  39. data/lib/rom/memory/repository.rb +62 -0
  40. data/lib/rom/memory/storage.rb +57 -0
  41. data/lib/rom/model_builder.rb +44 -5
  42. data/lib/rom/processor.rb +1 -1
  43. data/lib/rom/processor/transproc.rb +109 -16
  44. data/lib/rom/reader.rb +91 -39
  45. data/lib/rom/relation.rb +165 -26
  46. data/lib/rom/relation/composite.rb +132 -0
  47. data/lib/rom/relation/curried.rb +48 -0
  48. data/lib/rom/relation/lazy.rb +173 -0
  49. data/lib/rom/relation/loaded.rb +75 -0
  50. data/lib/rom/relation/registry_reader.rb +23 -0
  51. data/lib/rom/repository.rb +93 -34
  52. data/lib/rom/setup.rb +54 -98
  53. data/lib/rom/setup/finalize.rb +85 -76
  54. data/lib/rom/setup_dsl/command.rb +36 -0
  55. data/lib/rom/setup_dsl/command_dsl.rb +34 -0
  56. data/lib/rom/setup_dsl/mapper.rb +32 -0
  57. data/lib/rom/setup_dsl/mapper_dsl.rb +30 -0
  58. data/lib/rom/setup_dsl/relation.rb +21 -0
  59. data/lib/rom/setup_dsl/setup.rb +75 -0
  60. data/lib/rom/support/array_dataset.rb +38 -0
  61. data/lib/rom/support/class_builder.rb +44 -0
  62. data/lib/rom/support/class_macros.rb +56 -0
  63. data/lib/rom/support/data_proxy.rb +102 -0
  64. data/lib/rom/support/enumerable_dataset.rb +58 -0
  65. data/lib/rom/support/inflector.rb +73 -0
  66. data/lib/rom/support/options.rb +188 -0
  67. data/lib/rom/support/registry.rb +4 -8
  68. data/lib/rom/version.rb +1 -1
  69. data/rakelib/benchmark.rake +13 -0
  70. data/rakelib/mutant.rake +16 -0
  71. data/rakelib/rubocop.rake +18 -0
  72. data/rom.gemspec +4 -7
  73. data/spec/integration/commands/create_spec.rb +32 -24
  74. data/spec/integration/commands/delete_spec.rb +15 -7
  75. data/spec/integration/commands/update_spec.rb +13 -11
  76. data/spec/integration/mappers/deep_embedded_spec.rb +4 -11
  77. data/spec/integration/mappers/definition_dsl_spec.rb +31 -44
  78. data/spec/integration/mappers/embedded_spec.rb +9 -24
  79. data/spec/integration/mappers/group_spec.rb +22 -30
  80. data/spec/integration/mappers/prefixing_attributes_spec.rb +18 -23
  81. data/spec/integration/mappers/renaming_attributes_spec.rb +23 -38
  82. data/spec/integration/mappers/symbolizing_attributes_spec.rb +18 -24
  83. data/spec/integration/mappers/wrap_spec.rb +22 -30
  84. data/spec/integration/multi_repo_spec.rb +15 -37
  85. data/spec/integration/relations/reading_spec.rb +82 -14
  86. data/spec/integration/repositories/extending_relations_spec.rb +50 -0
  87. data/spec/integration/{adapters → repositories}/setting_logger_spec.rb +6 -5
  88. data/spec/integration/setup_spec.rb +59 -62
  89. data/spec/shared/enumerable_dataset.rb +49 -0
  90. data/spec/shared/one_behavior.rb +26 -0
  91. data/spec/shared/users_and_tasks.rb +11 -23
  92. data/spec/spec_helper.rb +16 -7
  93. data/spec/support/constant_leak_finder.rb +14 -0
  94. data/spec/test/memory_repository_lint_test.rb +27 -0
  95. data/spec/unit/rom/command_registry_spec.rb +44 -0
  96. data/spec/unit/rom/commands/result_spec.rb +14 -0
  97. data/spec/unit/rom/commands_spec.rb +174 -0
  98. data/spec/unit/rom/env_spec.rb +40 -7
  99. data/spec/unit/rom/global_spec.rb +14 -0
  100. data/spec/unit/rom/{mapper_builder_spec.rb → mapper/dsl_spec.rb} +52 -38
  101. data/spec/unit/rom/mapper_spec.rb +51 -10
  102. data/spec/unit/rom/{adapter/memory → memory}/dataset_spec.rb +6 -4
  103. data/spec/unit/rom/memory/repository_spec.rb +12 -0
  104. data/spec/unit/rom/memory/storage_spec.rb +45 -0
  105. data/spec/unit/rom/model_builder_spec.rb +4 -3
  106. data/spec/unit/rom/processor/transproc_spec.rb +1 -0
  107. data/spec/unit/rom/reader_spec.rb +97 -24
  108. data/spec/unit/rom/relation/composite_spec.rb +65 -0
  109. data/spec/unit/rom/relation/lazy_spec.rb +145 -0
  110. data/spec/unit/rom/relation/loaded_spec.rb +28 -0
  111. data/spec/unit/rom/relation_spec.rb +111 -6
  112. data/spec/unit/rom/repository_spec.rb +59 -9
  113. data/spec/unit/rom/setup_spec.rb +99 -11
  114. data/spec/unit/rom/support/array_dataset_spec.rb +59 -0
  115. data/spec/unit/rom/support/class_builder_spec.rb +42 -0
  116. data/spec/unit/rom/support/enumerable_dataset_spec.rb +17 -0
  117. data/spec/unit/rom/support/inflector_spec.rb +89 -0
  118. data/spec/unit/rom/support/options_spec.rb +119 -0
  119. metadata +74 -112
  120. data/lib/rom/adapter.rb +0 -191
  121. data/lib/rom/adapter/memory.rb +0 -32
  122. data/lib/rom/adapter/memory/commands.rb +0 -31
  123. data/lib/rom/adapter/memory/dataset.rb +0 -67
  124. data/lib/rom/adapter/memory/storage.rb +0 -26
  125. data/lib/rom/commands/with_options.rb +0 -18
  126. data/lib/rom/config.rb +0 -70
  127. data/lib/rom/mapper_builder.rb +0 -52
  128. data/lib/rom/mapper_builder/mapper_dsl.rb +0 -114
  129. data/lib/rom/mapper_builder/model_dsl.rb +0 -29
  130. data/lib/rom/reader_builder.rb +0 -48
  131. data/lib/rom/relation_builder.rb +0 -62
  132. data/lib/rom/setup/base_relation_dsl.rb +0 -46
  133. data/lib/rom/setup/command_dsl.rb +0 -46
  134. data/lib/rom/setup/mapper_dsl.rb +0 -19
  135. data/lib/rom/setup/relation_dsl.rb +0 -20
  136. data/lib/rom/setup/schema_dsl.rb +0 -33
  137. data/spec/integration/adapters/extending_relations_spec.rb +0 -41
  138. data/spec/integration/commands/try_spec.rb +0 -27
  139. data/spec/integration/schema_spec.rb +0 -77
  140. data/spec/unit/config_spec.rb +0 -60
  141. data/spec/unit/rom/adapter_spec.rb +0 -79
  142. data/spec/unit/rom_spec.rb +0 -14
@@ -1,114 +0,0 @@
1
- require 'rom/mapper_builder/model_dsl'
2
-
3
- module ROM
4
- class MapperBuilder
5
- # @api private
6
- class MapperDSL
7
- include ModelDSL
8
-
9
- attr_reader :attributes, :options, :symbolize_keys, :prefix
10
-
11
- def initialize(attributes, options)
12
- @attributes = attributes
13
- @options = options
14
- @symbolize_keys = options[:symbolize_keys]
15
- @prefix = options[:prefix]
16
- end
17
-
18
- def attribute(name, options = EMPTY_HASH)
19
- with_attr_options(name, options) do |attr_options|
20
- add_attribute(name, attr_options)
21
- end
22
- end
23
-
24
- def exclude(*names)
25
- names.each { |name| attributes.delete([name]) }
26
- end
27
-
28
- def embedded(name, options, &block)
29
- with_attr_options(name) do |attr_options|
30
- dsl = new(options, &block)
31
-
32
- attr_options.update(options)
33
-
34
- add_attribute(
35
- name, { header: dsl.header, type: :array }.update(attr_options)
36
- )
37
- end
38
- end
39
-
40
- def wrap(*args, &block)
41
- with_name_or_options(*args) do |name, options|
42
- dsl(name, { type: :hash, wrap: true }.update(options), &block)
43
- end
44
- end
45
-
46
- def group(*args, &block)
47
- with_name_or_options(*args) do |name, options|
48
- dsl(name, { type: :array, group: true }.update(options), &block)
49
- end
50
- end
51
-
52
- def header
53
- Header.coerce(attributes, model)
54
- end
55
-
56
- private
57
-
58
- def with_attr_options(name, options = EMPTY_HASH)
59
- attr_options = options.dup
60
-
61
- attr_options[:from] ||= :"#{prefix}_#{name}" if prefix
62
-
63
- if symbolize_keys
64
- attr_options.update(from: attr_options.fetch(:from) { name }.to_s)
65
- end
66
-
67
- yield(attr_options)
68
- end
69
-
70
- def with_name_or_options(*args)
71
- name, options =
72
- if args.size > 1
73
- args
74
- else
75
- [args.first, {}]
76
- end
77
-
78
- yield(name, options)
79
- end
80
-
81
- def dsl(name_or_attrs, options, &block)
82
- if block
83
- attributes_from_block(name_or_attrs, options, &block)
84
- else
85
- attributes_from_hash(name_or_attrs, options)
86
- end
87
- end
88
-
89
- def attributes_from_block(name, options, &block)
90
- dsl = new(options, &block)
91
- add_attribute(name, options.update(header: dsl.header))
92
- end
93
-
94
- def attributes_from_hash(hash, options)
95
- hash.each do |name, header|
96
- with_attr_options(name, options) do |attr_options|
97
- add_attribute(name, attr_options.update(header: header.zip))
98
- end
99
- end
100
- end
101
-
102
- def add_attribute(name, options)
103
- exclude(name, name.to_s)
104
- attributes << [name, options]
105
- end
106
-
107
- def new(options, &block)
108
- dsl = self.class.new([], @options.merge(options))
109
- dsl.instance_exec(&block)
110
- dsl
111
- end
112
- end
113
- end
114
- end
@@ -1,29 +0,0 @@
1
- require 'rom/model_builder'
2
-
3
- module ROM
4
- class MapperBuilder
5
- module ModelDSL
6
- attr_reader :attributes, :builder, :klass
7
-
8
- DEFAULT_TYPE = :poro
9
-
10
- def model(options = nil)
11
- if options.is_a?(Class)
12
- @klass = options
13
- elsif options
14
- type = options.fetch(:type) { DEFAULT_TYPE }
15
- @builder = ModelBuilder[type].new(options)
16
- end
17
-
18
- build_class unless options
19
- end
20
-
21
- private
22
-
23
- def build_class
24
- return klass if klass
25
- return builder.call(attributes.map(&:first)) if builder
26
- end
27
- end
28
- end
29
- end
@@ -1,48 +0,0 @@
1
- require 'rom/mapper_registry'
2
-
3
- module ROM
4
- # @api private
5
- class ReaderBuilder
6
- DEFAULT_OPTIONS = { inherit_header: true }.freeze
7
-
8
- attr_reader :relations, :readers
9
-
10
- # @api private
11
- def initialize(relations)
12
- @relations = relations
13
- @readers = {}
14
- end
15
-
16
- # @api private
17
- def call(name, input_options = {}, &block)
18
- with_options(input_options) do |options|
19
- parent = relations[options.fetch(:parent) { name }]
20
-
21
- builder = MapperBuilder.new(name, parent, options)
22
- builder.instance_exec(&block) if block
23
- mapper = builder.call
24
-
25
- mappers =
26
- if options[:parent]
27
- readers.fetch(parent.name).mappers
28
- else
29
- MapperRegistry.new
30
- end
31
-
32
- mappers[name] = mapper
33
-
34
- unless options[:parent]
35
- readers[name] = Reader.build(
36
- name, parent, mappers, parent.class.relation_methods
37
- )
38
- end
39
- end
40
- end
41
-
42
- private
43
-
44
- def with_options(options)
45
- yield(DEFAULT_OPTIONS.merge(options))
46
- end
47
- end
48
- end
@@ -1,62 +0,0 @@
1
- module ROM
2
- # @api private
3
- class RelationBuilder
4
- attr_reader :schema, :mod
5
-
6
- # @api private
7
- def initialize(schema, relations)
8
- @schema = schema
9
-
10
- @mod = Module.new
11
-
12
- @mod.module_exec do
13
- define_method(:__relations__) { relations }
14
- end
15
- end
16
-
17
- # @api private
18
- def call(name)
19
- schema_relation = schema[name]
20
-
21
- klass = Class.new(Relation)
22
-
23
- klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
24
- def self.name
25
- "#{Relation.name}[#{Inflecto.camelize(name)}]"
26
- end
27
-
28
- def self.inspect
29
- name
30
- end
31
-
32
- def self.to_s
33
- name
34
- end
35
-
36
- def name
37
- #{name.inspect}
38
- end
39
-
40
- def respond_to_missing?(name, _include_private = false)
41
- __relations__.key?(name) || super
42
- end
43
-
44
- private
45
-
46
- def method_missing(name, *args, &block)
47
- if __relations__.key?(name)
48
- __relations__[name]
49
- else
50
- super
51
- end
52
- end
53
- RUBY
54
-
55
- klass.send(:include, mod)
56
-
57
- yield(klass)
58
-
59
- klass.new(schema_relation.dataset, schema_relation.header)
60
- end
61
- end
62
- end
@@ -1,46 +0,0 @@
1
- module ROM
2
- class Setup
3
- class BaseRelationDSL
4
- attr_reader :env, :name, :header
5
-
6
- def initialize(env, name, &block)
7
- @env = env
8
- @name = name
9
- @header = []
10
- @repository = nil
11
- instance_exec(&block)
12
- end
13
-
14
- def repository(name = nil)
15
- if @repository
16
- @repository
17
- else
18
- @repository = env[name]
19
- end
20
- end
21
-
22
- def attribute(name)
23
- header << name
24
- end
25
-
26
- def call
27
- dataset =
28
- if adapter.respond_to?(:dataset)
29
- adapter.dataset(name, header)
30
- else
31
- adapter[name]
32
- end
33
-
34
- base_header = dataset.respond_to?(:header) ? dataset.header : header
35
-
36
- [name, dataset, base_header]
37
- end
38
-
39
- private
40
-
41
- def adapter
42
- repository.adapter
43
- end
44
- end
45
- end
46
- end
@@ -1,46 +0,0 @@
1
- require 'rom/mapper_builder'
2
-
3
- module ROM
4
- class Setup
5
- class CommandDSL
6
- attr_reader :commands
7
-
8
- class CommandDefinition
9
- attr_reader :options
10
-
11
- def initialize(options, &block)
12
- @options = options
13
- instance_exec(&block) if block
14
- end
15
-
16
- def to_h
17
- options
18
- end
19
-
20
- def type
21
- options[:type]
22
- end
23
-
24
- private
25
-
26
- def method_missing(name, *args, &block)
27
- if args.size == 1
28
- options[name] = args.first
29
- else
30
- super
31
- end
32
- end
33
- end
34
-
35
- def initialize(&block)
36
- @commands = {}
37
- instance_exec(&block)
38
- end
39
-
40
- def define(name, options = {}, &block)
41
- commands[name] = CommandDefinition.new(options, &block)
42
- self
43
- end
44
- end
45
- end
46
- end
@@ -1,19 +0,0 @@
1
- require 'rom/mapper_builder'
2
-
3
- module ROM
4
- class Setup
5
- class MapperDSL
6
- attr_reader :mappers
7
-
8
- def initialize(&block)
9
- @mappers = []
10
- instance_exec(&block)
11
- end
12
-
13
- def define(name, options = {}, &block)
14
- mappers << [name, options, block]
15
- self
16
- end
17
- end
18
- end
19
- end
@@ -1,20 +0,0 @@
1
- module ROM
2
- class Setup
3
- class RelationDSL
4
- attr_reader :schema, :relations
5
-
6
- def initialize(schema, relations)
7
- @schema = schema
8
- @relations = relations
9
- end
10
-
11
- def register(name, &block)
12
- relations[name] = [block]
13
- end
14
-
15
- def call
16
- relations
17
- end
18
- end
19
- end
20
- end
@@ -1,33 +0,0 @@
1
- require 'rom/setup/base_relation_dsl'
2
-
3
- module ROM
4
- class Setup
5
- # @private
6
- class SchemaDSL
7
- attr_reader :env, :schema
8
-
9
- # @api private
10
- def initialize(env, schema, &block)
11
- @env = env
12
- @schema = schema
13
- initialize_schema
14
- instance_exec(&block)
15
- end
16
-
17
- # @api public
18
- def base_relation(name, &block)
19
- dsl = BaseRelationDSL.new(env, name, &block)
20
- schema[dsl.repository] << dsl.call
21
- end
22
-
23
- private
24
-
25
- # @api private
26
- def initialize_schema
27
- env.repositories.each_value do |repository|
28
- schema[repository] ||= []
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,41 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Adapter" do
4
- include_context "users and tasks" do
5
- before(:all) do
6
- Class.new(ROM::Adapter::Memory) do
7
- def self.schemes
8
- [:memory]
9
- end
10
-
11
- def extend_relation_class(klass)
12
- klass.class_eval do
13
- def self.freaking_awesome?
14
- true
15
- end
16
- end
17
- end
18
-
19
- def extend_relation_instance(relation)
20
- relation.instance_eval do
21
- def freaking_cool?
22
- true
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
29
-
30
- before do
31
- setup.relation(:users)
32
- end
33
-
34
- it "can extend relation class" do
35
- expect(rom.relations.users.class).to be_freaking_awesome
36
- end
37
-
38
- it "can extend relation instance" do
39
- expect(rom.relations.users).to be_freaking_cool
40
- end
41
- end