Skip to content

Commit cd5c226

Browse files
committed
Apply base rubocop ssettings to the repo
1 parent 209b91e commit cd5c226

19 files changed

Lines changed: 446 additions & 369 deletions

.rubocop.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,32 @@
1010
# See https://docs.rubocop.org/rubocop/configuration
1111
Layout/LineLength:
1212
Max: 120
13+
14+
Metrics/BlockLength:
15+
Exclude:
16+
- "spec/**/*.rb"
17+
- "*.gemspec"
18+
19+
Metrics/MethodLength:
20+
Enabled: false
21+
22+
Naming/FileName:
23+
Exclude:
24+
# This file is deliberately named with dashes, to match the package name
25+
- 'lib/recursive-open-struct.rb'
26+
27+
Style/CommentedKeyword:
28+
Exclude:
29+
- "spec/**/*.rb"
30+
31+
Style/Documentation:
32+
Enabled: false
33+
34+
# Can't really disable this in the gemspec file (it is reported on line 1, but
35+
# line 1 has the frozen string literal comment)
36+
Gemspec/RequiredRubyVersion:
37+
Enabled: false
38+
# TODO: should I set a required ruby version? At the moment, the project policy
39+
# is to officially support supported Ruby versions (and JRuby), but to not
40+
# officially support no-longer-maintained Ruby versions.
41+

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24
gemspec

Rakefile

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# encoding: utf-8
1+
# frozen_string_literal: true
22

33
require 'rubygems'
44
require 'bundler/gem_tasks'
@@ -9,16 +9,16 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
99
end
1010
namespace :spec do
1111
if RUBY_VERSION =~ /^1\.8/
12-
desc "Rspec code coverage (1.8.7)"
12+
desc 'Rspec code coverage (1.8.7)'
1313
RSpec::Core::RakeTask.new(:coverage) do |spec|
1414
spec.pattern = 'spec/**/*_spec.rb'
1515
spec.rcov = true
1616
end
1717
else
18-
desc "Rspec code coverage (1.9+)"
18+
desc 'Rspec code coverage (1.9+)'
1919
task :coverage do
2020
ENV['COVERAGE'] = 'true'
21-
Rake::Task["spec"].execute
21+
Rake::Task['spec'].execute
2222
end
2323
end
2424
end
@@ -28,36 +28,36 @@ RuboCop::RakeTask.new
2828

2929
require 'rdoc/task'
3030
Rake::RDocTask.new do |rdoc|
31-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
31+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
3232

3333
rdoc.rdoc_dir = 'rdoc'
3434
rdoc.title = "recursive-open-struct #{version}"
3535
rdoc.rdoc_files.include('README*')
3636
rdoc.rdoc_files.include('lib/**/*.rb')
3737
end
3838

39-
task :default => [:spec, :rubocop]
39+
task default: %i[spec rubocop]
4040

4141
task :fix_permissions do
42-
File.umask 0022
42+
File.umask 0o022
4343
filelist = `git ls-files`.split("\n")
44-
FileUtils.chmod 0644, filelist, :verbose => true
45-
FileUtils.chmod 0755, ['lib','spec'], :verbose => true
44+
FileUtils.chmod 0o644, filelist, verbose: true
45+
FileUtils.chmod 0o755, %w[lib spec], verbose: true
4646
end
4747

48-
desc "Update the AUTHORS.txt file"
48+
desc 'Update the AUTHORS.txt file'
4949
task :update_authors do
5050
authors = `git log --format="%aN <%aE>"|sort -f|uniq`
5151
File.open('AUTHORS.txt', 'w') do |f|
5252
f.write("Recursive-open-struct was written by these fine people:\n\n")
53-
f.write(authors.split("\n").map { |a| "* #{a}" }.join( "\n" ))
53+
f.write(authors.split("\n").map { |a| "* #{a}" }.join("\n"))
5454
f.write("\n")
5555
end
5656
end
5757

58-
task :build => [:update_authors, :fix_permissions]
58+
task build: %i[update_authors fix_permissions]
5959

60-
desc "Run an interactive pry shell with ros required"
60+
desc 'Run an interactive pry shell with ros required'
6161
task :pry do
62-
sh "pry -I lib -r recursive-open-struct"
62+
sh 'pry -I lib -r recursive-open-struct'
6363
end

lib/recursive-open-struct.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
# frozen_string_literal: true
2+
13
require 'recursive_open_struct'

lib/recursive_open_struct.rb

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'ostruct'
24
require 'recursive_open_struct/version'
35

@@ -12,6 +14,7 @@
1214
# TODO: `#*_as_a_hash` deprecated. Nested hashes can be referenced using
1315
# `#to_h`.
1416

17+
# rubocop:disable Metrics/ClassLength
1518
class RecursiveOpenStruct < OpenStruct
1619
include Dig if OpenStruct.public_instance_methods.include? :dig
1720

@@ -28,7 +31,9 @@ def self.default_options
2831
}
2932
end
3033

31-
def initialize(hash=nil, passed_options={})
34+
# rubocop:disable Lint/MissingSuper
35+
# Intentionally doesn't call super and initializes +@table+ itself.
36+
def initialize(hash = nil, passed_options = {})
3237
hash = hash.to_h if [hash.is_a?(RecursiveOpenStruct), hash.is_a?(OpenStruct)].any?
3338
hash ||= {}
3439

@@ -40,6 +45,7 @@ def initialize(hash=nil, passed_options={})
4045

4146
@sub_elements = {}
4247
end
48+
# rubocop:enable Lint/MissingSuper
4349

4450
def marshal_load(attributes)
4551
hash, @options = attributes
@@ -69,17 +75,17 @@ def to_h
6975

7076
# TODO: deprecated, unsupported by OpenStruct. OpenStruct does not consider
7177
# itself to be a "kind of" Hash.
72-
alias_method :to_hash, :to_h
78+
alias to_hash to_h
7379

7480
# Continue supporting older rubies -- JRuby 9.1.x.x is still considered
7581
# stable, but is based on Ruby
7682
# 2.3.x and so uses :modifiable instead of :modifiable?. Furthermore, if
7783
# :modifiable is private, then make :modifiable? private too.
78-
if !OpenStruct.private_instance_methods.include?(:modifiable?)
84+
unless OpenStruct.private_instance_methods.include?(:modifiable?)
7985
if OpenStruct.private_instance_methods.include?(:modifiable)
80-
alias_method :modifiable?, :modifiable
86+
alias modifiable? modifiable
8187
elsif OpenStruct.public_instance_methods.include?(:modifiable)
82-
alias_method :modifiable?, :modifiable
88+
alias modifiable? modifiable
8389
private :modifiable?
8490
end
8591
end
@@ -89,7 +95,7 @@ def [](name)
8995
v = @table[key_name]
9096
if v.is_a?(Hash)
9197
@sub_elements[key_name] ||= _create_sub_element_(v, mutate_input_hash: true)
92-
elsif v.is_a?(Array) and @options[:recurse_over_arrays]
98+
elsif v.is_a?(Array) && @options[:recurse_over_arrays]
9399
@sub_elements[key_name] ||= recurse_over_array(v)
94100
@sub_elements[key_name] = recurse_over_array(@sub_elements[key_name])
95101
else
@@ -100,7 +106,7 @@ def [](name)
100106
if private_instance_methods.include?(:modifiable?) || public_instance_methods.include?(:modifiable?)
101107
def []=(name, value)
102108
key_name = _get_key_from_table_(name)
103-
tbl = modifiable? # Ensure we are modifiable
109+
tbl = modifiable? # Ensure we are modifiable
104110
@sub_elements.delete(key_name)
105111
tbl[key_name] = value
106112
end
@@ -120,19 +126,20 @@ def respond_to_missing?(mid, include_private = false)
120126

121127
# Adapted implementation of method_missing to accommodate the differences
122128
# between ROS and OS.
129+
# rubocop:disable Metrics/AbcSize
130+
# rubocop:disable Metrics/PerceivedComplexity
123131
def method_missing(mid, *args)
124132
len = args.length
125133
if mid =~ /^(.*)=$/
126-
if len != 1
127-
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
128-
end
134+
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1) if len != 1
135+
129136
# self[$1.to_sym] = args[0]
130137
# modifiable?[new_ostruct_member!($1.to_sym)] = args[0]
131-
new_ostruct_member!($1.to_sym)
138+
new_ostruct_member!(::Regexp.last_match(1).to_sym)
132139
public_send(mid, args[0])
133-
elsif len == 0
140+
elsif len.zero?
134141
key = mid
135-
key = $1 if key =~ /^(.*)_as_a_hash$/
142+
key = ::Regexp.last_match(1) if key =~ /^(.*)_as_a_hash$/
136143
if @table.key?(_get_key_from_table_(key))
137144
new_ostruct_member!(key)
138145
public_send(mid)
@@ -147,6 +154,8 @@ def method_missing(mid, *args)
147154
raise err
148155
end
149156
end
157+
# rubocop:enable Metrics/PerceivedComplexity
158+
# rubocop:enable Metrics/AbcSize
150159

151160
def freeze
152161
@table.each_key do |key|
@@ -160,7 +169,7 @@ def freeze
160169
# 2.4.0.
161170
def new_ostruct_member(name)
162171
key_name = _get_key_from_table_(name)
163-
unless self.singleton_class.method_defined?(name.to_sym)
172+
unless singleton_class.method_defined?(name.to_sym)
164173
class << self; self; end.class_eval do
165174
define_method(name) do
166175
self[key_name]
@@ -185,7 +194,12 @@ class << self; self; end.class_eval do
185194

186195
def delete_field(name)
187196
sym = _get_key_from_table_(name)
188-
singleton_class.__send__(:remove_method, sym, "#{sym}=") rescue NoMethodError # ignore if methods not yet generated.
197+
begin
198+
singleton_class.__send__(:remove_method, sym, "#{sym}=")
199+
rescue StandardError
200+
# ignore if methods not yet generated.
201+
NoMethodError
202+
end
189203
@sub_elements.delete(sym)
190204
@table.delete(sym)
191205
end
@@ -203,8 +217,9 @@ def initialize_dup(orig)
203217
end
204218

205219
def _get_key_from_table_(name)
206-
return name.to_s if @table.has_key?(name.to_s)
207-
return name.to_sym if @table.has_key?(name.to_sym)
220+
return name.to_s if @table.key?(name.to_s)
221+
return name.to_sym if @table.key?(name.to_sym)
222+
208223
name
209224
end
210225

@@ -222,5 +237,5 @@ def recurse_over_array(array)
222237
end
223238
array
224239
end
225-
226240
end
241+
# rubocop:enable Metrics/ClassLength
Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
1-
module RecursiveOpenStruct::DebugInspect
2-
def debug_inspect(io = STDOUT, indent_level = 0, recursion_limit = 12)
3-
display_recursive_open_struct(io, @table, indent_level, recursion_limit)
4-
end
1+
# frozen_string_literal: true
52

6-
def display_recursive_open_struct(io, ostrct_or_hash, indent_level, recursion_limit)
3+
# rubocop:disable Metrics/AbcSize
4+
# rubocop:disable Metrics/CyclomaticComplexity
5+
# rubocop:disable Metrics/PerceivedComplexity
6+
# rubocop:disable Style/StringConcatenation
7+
class RecursiveOpenStruct < OpenStruct
8+
module DebugInspect
9+
def debug_inspect(io = $stdout, indent_level = 0, recursion_limit = 12)
10+
display_recursive_open_struct(io, @table, indent_level, recursion_limit)
11+
end
712

8-
if recursion_limit <= 0 then
9-
# protection against recursive structure (like in the tests)
10-
io.puts ' '*indent_level + '(recursion limit reached)'
11-
else
12-
#puts ostrct_or_hash.inspect
13-
if ostrct_or_hash.is_a?(self.class) then
14-
ostrct_or_hash = ostrct_or_hash.marshal_dump
15-
end
13+
def display_recursive_open_struct(io, ostrct_or_hash, indent_level, recursion_limit)
14+
if recursion_limit <= 0
15+
# protection against recursive structure (like in the tests)
16+
io.puts ' ' * indent_level + '(recursion limit reached)'
17+
else
18+
# puts ostrct_or_hash.inspect
19+
ostrct_or_hash = ostrct_or_hash.marshal_dump if ostrct_or_hash.is_a?(self.class)
1620

17-
# We'll display the key values like this : key = value
18-
# to align display, we look for the maximum key length of the data that will be displayed
19-
# (everything except hashes)
20-
data_indent = ostrct_or_hash \
21-
.reject { |k, v| v.is_a?(self.class) || v.is_a?(Hash) } \
22-
.max {|a,b| a[0].to_s.length <=> b[0].to_s.length}[0].to_s.length
23-
# puts "max length = #{data_indent}"
21+
# We'll display the key values like this : key = value
22+
# to align display, we look for the maximum key length of the data that will be displayed
23+
# (everything except hashes)
24+
data_indent = ostrct_or_hash \
25+
.reject { |_k, v| v.is_a?(self.class) || v.is_a?(Hash) } \
26+
.max { |a, b| a[0].to_s.length <=> b[0].to_s.length }[0].to_s.length
27+
# puts "max length = #{data_indent}"
2428

25-
ostrct_or_hash.each do |key, value|
26-
if (value.is_a?(self.class) || value.is_a?(Hash)) then
27-
io.puts ' '*indent_level + key.to_s + '.'
28-
display_recursive_open_struct(io, value, indent_level + 1, recursion_limit - 1)
29-
else
30-
io.puts ' '*indent_level + key.to_s + ' '*(data_indent - key.to_s.length) + ' = ' + value.inspect
29+
ostrct_or_hash.each do |key, value|
30+
if value.is_a?(self.class) || value.is_a?(Hash)
31+
io.puts ' ' * indent_level + key.to_s + '.'
32+
display_recursive_open_struct(io, value, indent_level + 1, recursion_limit - 1)
33+
else
34+
io.puts ' ' * indent_level + key.to_s + ' ' * (data_indent - key.to_s.length) + ' = ' + value.inspect
35+
end
3136
end
3237
end
33-
end
3438

35-
true
39+
true
40+
end
3641
end
37-
3842
end
43+
# rubocop:enable Style/StringConcatenation
44+
# rubocop:enable Metrics/PerceivedComplexity
45+
# rubocop:enable Metrics/CyclomaticComplexity
46+
# rubocop:enable Metrics/AbcSize

0 commit comments

Comments
 (0)