Skip to content

Commit 92047a0

Browse files
committed
Address rubocop offenses
1 parent c9452c4 commit 92047a0

18 files changed

+78
-52
lines changed

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ Style/MapIntoArray:
138138
Exclude:
139139
- "core/spec/**/*.rb"
140140

141+
Style/NumberedParametersLimit:
142+
Max: 2
143+
141144
Style/OpenStructUse:
142145
Exclude:
143146
- "core/spec/**/*.rb"

core/lib/rom/command_registry.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ def map_with(mapper_name)
9191
end
9292

9393
# @api private
94-
def set_compiler(compiler)
94+
def set_compiler(compiler) # rubocop:disable Naming/AccessorMethodName
9595
options[:compiler] = @compiler = compiler
9696
end
9797

9898
# @api private
99-
def set_mappers(mappers)
99+
def set_mappers(mappers) # rubocop:disable Naming/AccessorMethodName
100100
options[:mappers] = @mappers = mappers
101101
end
102102

core/lib/rom/memory/dataset.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ def join(*args)
4141
# @return [Dataset]
4242
#
4343
# @api public
44-
def restrict(criteria = nil)
45-
return find_all { |tuple| yield(tuple) } unless criteria
44+
def restrict(criteria = nil, &)
45+
return find_all(&) unless criteria
4646

4747
find_all do |tuple|
4848
criteria.all? do |k, v|
4949
case v
50-
when Array then v.include?(tuple[k])
51-
when Regexp then tuple[k].match(v)
50+
when ::Array then v.include?(tuple[k])
51+
when ::Regexp then tuple[k].match(v)
5252
else tuple[k].eql?(v)
5353
end
5454
end

core/lib/rom/registry.rb

+11-6
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,23 @@ def to_hash
6262
end
6363

6464
# @api private
65-
def map
66-
new_elements = elements.each_with_object({}) do |(name, element), h|
67-
h[name] = yield(element)
68-
end
65+
def map(&)
66+
new_elements = elements.transform_values(&)
6967
self.class.new(new_elements, **options)
7068
end
7169

7270
# @api private
73-
def each
71+
def each(&)
7472
return to_enum unless block_given?
7573

76-
elements.each { |element| yield(element) }
74+
elements.each(&)
75+
end
76+
77+
# @api private
78+
def each_value(&)
79+
return to_enum(:each_value) unless block_given?
80+
81+
elements.each_value(&)
7782
end
7883

7984
# @api private

core/lib/rom/relation.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,13 @@ class Relation
161161
}
162162

163163
# @!attribute [r] auto_map
164-
# @return [TrueClass,FalseClass] Whether or not a relation and its compositions should be auto-mapped
164+
# @return [true, false] Whether or not a relation and its
165+
# compositions should be auto-mapped
165166
# @api private
166167
option :auto_map, default: -> { self.class.auto_map }
167168

168169
# @!attribute [r] auto_struct
169-
# @return [TrueClass,FalseClass] Whether or not tuples should be auto-mapped to structs
170+
# @return [true, false] Whether or not tuples should be auto-mapped to structs
170171
# @api private
171172
option :auto_struct, default: -> { self.class.auto_struct }
172173

@@ -424,7 +425,7 @@ def new(dataset, **new_opts)
424425
if new_opts.empty?
425426
options
426427
elsif new_opts.key?(:schema)
427-
options.merge(new_opts).reject { |k, _| %i[input_schema output_schema].include?(k) }
428+
options.merge(new_opts).except(:input_schema, :output_schema)
428429
else
429430
options.merge(new_opts)
430431
end

core/lib/rom/relation/class_interface.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ def view(*args, &block)
234234
# @api public
235235
def forward(*methods)
236236
methods.each do |method|
237-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
238-
def #{method}(*args, &block)
239-
new(dataset.__send__(:#{method}, *args, &block))
240-
end
237+
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
238+
def #{method}(...) # def super_query(...)
239+
new(dataset.__send__(:#{method}, ...)) # new(dataset.__send__(:super_query, ...))
240+
end # end
241241
RUBY
242242
end
243243
end

core/lib/rom/relation/curried.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
require 'rom/pipeline'
66
require 'rom/relation/name'
77
require 'rom/relation/materializable'
8+
require 'rom/relation/graph'
9+
require 'rom/relation/wrap'
10+
require 'rom/relation/composite'
811

912
module ROM
1013
class Relation
@@ -19,6 +22,8 @@ class Relation
1922
class Curried
2023
extend Initializer
2124

25+
WRAPS = [Relation, Graph, Wrap, Composite].freeze
26+
2227
include Dry::Equalizer(:relation, :options)
2328
include Materializable
2429
include Pipeline
@@ -112,7 +117,7 @@ def method_missing(meth, ...)
112117

113118
super if response.is_a?(self.class)
114119

115-
if response.is_a?(Relation) || response.is_a?(Graph) || response.is_a?(Wrap) || response.is_a?(Composite)
120+
if WRAPS.any? { |klass| response.is_a?(klass) }
116121
__new__(response)
117122
else
118123
response

core/lib/rom/relation/loaded.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def initialize(source, collection = source.to_a)
4343
# @yield [Hash]
4444
#
4545
# @api public
46-
def each
46+
def each(&)
4747
return to_enum unless block_given?
4848

49-
collection.each { |tuple| yield(tuple) }
49+
collection.each(&)
5050
end
5151

5252
# Returns a single tuple from the relation if there is one.

core/lib/rom/schema.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Schema
3939
subscribe('configuration.relations.registry.created') do |event|
4040
registry = event[:registry]
4141

42-
registry.each do |_, relation|
42+
registry.each_value do |relation|
4343
unless relation.schema.frozen?
4444
relation.schema.finalize_associations!(relations: registry)
4545
relation.schema.finalize!

core/lib/rom/schema/dsl.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Schema
99
# Schema DSL exposed as `schema { .. }` in relation classes
1010
#
1111
# @api public
12-
class DSL < BasicObject
12+
class DSL < ::BasicObject
1313
KERNEL_METHODS = %i[extend method].freeze
1414
KERNEL_METHODS.each { |m| define_method(m, ::Kernel.instance_method(m)) }
1515

@@ -185,7 +185,7 @@ def call(&block)
185185
instance_exec(&definition) if definition
186186

187187
schema_class.define(relation, **opts) do |schema|
188-
plugins.values.each do |plugin, options|
188+
plugins.each_value do |plugin, options|
189189
plugin.apply_to(schema, **options)
190190
end
191191
end

core/lib/rom/setup.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def initialize(notifications)
4141
#
4242
# @param [String, Pathname] directory The root path to components
4343
# @param [Hash] options
44-
# @option options [Boolean, String] :namespace Enable/disable namespace or provide a custom namespace name
44+
# @option options [Boolean, String] :namespace Enable/disable
45+
# namespace or provide a custom namespace name
4546
#
4647
# @return [Setup]
4748
#

core/lib/rom/setup/auto_registration.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ class AutoRegistration
4444
# @!attribute [r] globs
4545
# @return [Hash] File globbing functions for each component dir
4646
option :globs, default: lambda {
47-
Hash[
48-
component_dirs.map { |component, path|
49-
[component, directory.join("#{path}/**/*.rb")]
50-
}
51-
]
47+
component_dirs.transform_values { |path|
48+
directory.join("#{path}/**/*.rb")
49+
}
5250
}
5351

5452
# Load relation files

core/lib/rom/setup/finalize/finalize_mappers.rb

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
module ROM
66
class Finalize
77
class FinalizeMappers
8-
attr_reader :mapper_classes, :mapper_objects, :registry_hash
8+
attr_reader :mapper_classes
9+
attr_reader :mapper_objects
10+
attr_reader :registry_hash
911

1012
# @api private
1113
def initialize(mapper_classes, mapper_objects)
@@ -40,16 +42,16 @@ def run!
4042
private
4143

4244
def check_duplicate_registered_mappers
43-
mapper_relation_register = mapper_classes.map { |mapper_class| [mapper_class.relation, mapper_class.register_as].compact }
44-
return if mapper_relation_register.uniq.count == mapper_classes.count
45+
duplicates = mapper_classes.map { [_1.relation, _1.register_as] }.tally.select { _2 > 1 }
4546

46-
mapper_relation_register.select { |relation_register_as| mapper_relation_register.count(relation_register_as) > 1 }
47-
.uniq
48-
.each do |duplicated_mappers|
49-
raise MapperAlreadyDefinedError,
50-
"Mapper with `register_as #{duplicated_mappers.last.inspect}` registered more " \
51-
"than once for relation #{duplicated_mappers.first.inspect}"
52-
end
47+
case duplicates.first
48+
in [rel, as], _
49+
raise MapperAlreadyDefinedError,
50+
"Mapper with `register_as #{as.inspect}` registered more " \
51+
"than once for relation #{rel.inspect}"
52+
else
53+
nil
54+
end
5355
end
5456

5557
def build_mappers(relation_name)

core/lib/rom/setup/finalize/finalize_relations.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,22 @@ def run!
5656
"Relation with name #{key.inspect} registered more than once"
5757
end
5858

59-
klass.use(:registry_reader, klass: klass,
60-
relation_readers_module: relation_readers_module)
59+
klass.use(
60+
:registry_reader,
61+
klass: klass,
62+
relation_readers_module: relation_readers_module
63+
)
6164

62-
notifications.trigger('configuration.relations.class.ready', relation: klass,
63-
adapter: klass.adapter)
65+
notifications.trigger(
66+
'configuration.relations.class.ready',
67+
relation: klass,
68+
adapter: klass.adapter
69+
)
6470

6571
relations[key] = build_relation(klass, registry)
6672
end
6773

68-
registry.each do |_, relation|
74+
registry.each_value do |relation|
6975
notifications.trigger(
7076
'configuration.relations.object.registered',
7177
relation: relation, registry: registry

core/lib/rom/struct.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ module ROM
5151
# # => #<Dry::Types[id: Nominal<Integer meta={primary_key: true, source: :users}>]>
5252
#
5353
# model.schema[:name]
54-
# # => #<Dry::Types[name: Sum<Nominal<NilClass> | Nominal<String meta={source: :users}> meta={source: :users}>]>
54+
# # => #<Dry::Types[name: Sum<Nominal<NilClass> |
55+
# # Nominal<String meta={source: :users}> meta={source: :users}>]>
5556
#
5657
# @example passing a namespace with an existing parent class
5758
# module Entities

core/spec/unit/rom/plugin_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def plugged_in
1616
end
1717
Test::SchemaPlugin = Module.new do
1818
def self.apply(schema, **)
19-
schema.attributes.concat(
20-
[ROM::Attribute.new(ROM::Types::Date.meta(source: schema.name), name: :created_at),
21-
ROM::Attribute.new(ROM::Types::Date.meta(source: schema.name), name: :updated_at)]
19+
schema.attributes.push(
20+
ROM::Attribute.new(ROM::Types::Date.meta(source: schema.name), name: :created_at),
21+
ROM::Attribute.new(ROM::Types::Date.meta(source: schema.name), name: :updated_at)
2222
)
2323
end
2424
end

core/spec/unit/rom/relation/class_interface/view_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@
136136
}
137137

138138
Class.new(ROM::Memory::Relation) do
139-
schema_inferrer ROM::Schema::DEFAULT_INFERRER.with(attributes_inferrer: attributes_inferrer)
139+
schema_inferrer ROM::Schema::DEFAULT_INFERRER.with(
140+
attributes_inferrer: attributes_inferrer
141+
)
140142

141143
schema(:users, infer: true)
142144

core/spec/unit/rom/relation/map_to_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
let(:mappers) { {} }
2424

2525
it 'instantiates custom model when auto_struct is enabled' do
26-
expect(relation.with(auto_struct: true).map_to(OpenStruct).first).to be_instance_of(OpenStruct)
26+
expect(
27+
relation.with(auto_struct: true).map_to(OpenStruct).first
28+
).to be_instance_of(OpenStruct)
2729
end
2830

2931
it 'instantiates custom model when auto_struct is disabled' do

0 commit comments

Comments
 (0)