Skip to content

Commit

Permalink
[rubocop] fix offences
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 16, 2022
1 parent 6f7ef07 commit f32f141
Show file tree
Hide file tree
Showing 32 changed files with 101 additions and 38 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

eval_gemfile "Gemfile.devtools"
Expand Down
2 changes: 2 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

guard :rspec, cmd: "bundle exec rspec" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^spec/(spec_helper|support)}) { "spec" }
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/setup"
Bundler::GemHelper.install_tasks

Expand Down
2 changes: 2 additions & 0 deletions lib/dry-initializer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require_relative "dry/initializer"
2 changes: 2 additions & 0 deletions lib/dry/initializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "set"

# Namespace for gems in a dry-rb community
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/builders.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
# @private
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/builders/attribute.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
module Builders
Expand Down
10 changes: 4 additions & 6 deletions lib/dry/initializer/builders/initializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
module Builders
Expand Down Expand Up @@ -41,15 +43,11 @@ def define_line
end

def params_lines
@definitions.reject(&:option)
.flat_map { |item| Attribute[item] }
.map { |line| " " << line }
@definitions.reject(&:option).flat_map { Attribute[_1] }.map { " #{_1}" }
end

def options_lines
@definitions.select(&:option)
.flat_map { |item| Attribute[item] }
.map { |line| " " << line }
@definitions.select(&:option).flat_map { Attribute[_1] }.map { " #{_1}" }
end

def end_line
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/builders/reader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
module Builders
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/builders/signature.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
module Builders
Expand Down
8 changes: 5 additions & 3 deletions lib/dry/initializer/config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
#
Expand All @@ -23,10 +25,10 @@ class Config
# @return [Module] reference to the module to be included into class
def mixin
@mixin ||= Module.new.tap do |mod|
__dry_initializer__ = self
initializer = self
mod.extend(Mixin::Local)
mod.send :define_method, :__dry_initializer_config__ do
__dry_initializer__
mod.define_method(:__dry_initializer_config__) do
initializer
end
mod.send :private, :__dry_initializer_config__
end
Expand Down
6 changes: 4 additions & 2 deletions lib/dry/initializer/definition.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
#
Expand All @@ -21,11 +23,11 @@ def options
default: default,
reader: reader,
desc: desc
}.reject { |_, value| value.nil? }
}.compact
end

def name
@name ||= (option ? "option" : "parameter") << " '#{source}'"
@name ||= "#{option ? "option" : "parameter"} '#{source}'"
end
alias_method :to_s, :name
alias_method :to_str, :name
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# The module is responsible for __normalizing__ arguments
# of `.param` and `.option`.
#
Expand Down
14 changes: 11 additions & 3 deletions lib/dry/initializer/dispatchers/build_nested_type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Prepare nested data type from a block
#
# @example
Expand Down Expand Up @@ -56,8 +57,15 @@ def full_name(parent, name)
end

def build_struct(klass_name, block)
eval "class #{klass_name} < Dry::Initializer::Struct; end"
const_get(klass_name).tap { |klass| klass.class_eval(&block) }
# rubocop: disable Security/Eval
# rubocop: disable Style/DocumentDynamicEvalDefinition
eval <<~RUBY, TOPLEVEL_BINDING, __FILE__, __LINE__ + 1
class #{klass_name} < Dry::Initializer::Struct
end
RUBY
# rubocop: enable Style/DocumentDynamicEvalDefinition
# rubocop: enable Security/Eval
const_get(klass_name).tap { _1.class_eval(&block) }
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers/check_type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Checks whether an unwrapped type is valid
#
module Dry
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers/prepare_default.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Prepares the `:default` option
#
# It must respond to `.call` without arguments
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers/prepare_ivar.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Prepares the variable name of a parameter or an option.
#
module Dry
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers/prepare_optional.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Defines whether an argument is optional
#
module Dry
Expand Down
13 changes: 7 additions & 6 deletions lib/dry/initializer/dispatchers/prepare_reader.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Checks the reader privacy
#
module Dry
Expand All @@ -9,11 +10,11 @@ module PrepareReader

def call(target: nil, reader: :public, **options)
reader = case reader.to_s
when "false", "" then nil
when "true" then :public
when "public", "private", "protected" then reader.to_sym
else invalid_reader!(target, reader)
end
when "false", "" then nil
when "true" then :public
when "public", "private", "protected" then reader.to_sym
else invalid_reader!(target, reader)
end

{target: target, reader: reader, **options}
end
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers/prepare_source.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# The dispatcher verifies a correctness of the source name
# of param or option, taken as a `:source` option.
#
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers/prepare_target.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Prepares the target name of a parameter or an option.
#
# Unlike source, the target must satisfy requirements for Ruby variable names.
Expand Down
13 changes: 9 additions & 4 deletions lib/dry/initializer/dispatchers/unwrap_type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Looks at the `:type` option and counts how many nested arrays
# it contains around either nil or a callable value.
#
Expand All @@ -12,15 +13,19 @@ module UnwrapType
extend self

def call(type: nil, wrap: 0, **options)
type, wrap = unwrap(type, 0)
type, count = unwrap(type, wrap)

{type: type, wrap: wrap, **options}
{type: type, wrap: count, **options}
end

private

def unwrap(type, count)
type.is_a?(Array) ? unwrap(type.first, count + 1) : [type, count]
if type.is_a?(::Array)
unwrap(type.first, count + 1)
else
[type, count]
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/initializer/dispatchers/wrap_type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# frozen_string_literal: true

# Takes `:type` and `:wrap` to construct the final value coercer
#
module Dry
Expand Down
10 changes: 8 additions & 2 deletions lib/dry/initializer/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
# Module-level DSL
Expand Down Expand Up @@ -37,8 +39,12 @@ def extended(klass)
klass.include Mixin::Root
end

def self.extended(mod)
mod.instance_variable_set :@null, UNDEFINED
class << self
private

def extended(mod)
mod.instance_variable_set :@null, UNDEFINED
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/mixin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
# @private
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/mixin/local.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
module Mixin
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/mixin/root.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
module Mixin
Expand Down
7 changes: 4 additions & 3 deletions lib/dry/initializer/struct.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#
# frozen_string_literal: true

# The nested structure that takes nested hashes with indifferent access
#
class Dry
module Dry
module Initializer
module Struct
class Struct
extend ::Dry::Initializer

class << self
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/initializer/undefined.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dry
module Initializer
module UNDEFINED
Expand Down
4 changes: 3 additions & 1 deletion lib/dry/initializer/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Dry
module Initializer
VERSION = "3.1.0".freeze
VERSION = "3.1.0"
end
end
2 changes: 2 additions & 0 deletions lib/tasks/benchmark.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

namespace :benchmark do
desc "Runs benchmarks for plain params"
task :plain_params do
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/profile.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# rubocop: disable Lint/ConstantDefinitionInBlock
namespace :profile do
def profile(name, execution, &definition)
Expand Down

0 comments on commit f32f141

Please sign in to comment.