Skip to content

Commit

Permalink
Add CodeClimate plugin: rubocop (#53)
Browse files Browse the repository at this point in the history
- Allow up to 5 params to be optional (same as max total params)
- Change max line length to 120.
  • Loading branch information
gonzedge committed May 16, 2023
1 parent 7741c13 commit ff8aecd
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 221 deletions.
4 changes: 4 additions & 0 deletions .codeclimate.yml
Expand Up @@ -9,3 +9,7 @@ plugins:
# Markdown lint with rules from https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md
markdownlint:
enabled: true
# Ruby lint
rubocop:
enabled: true
channel: rubocop-1-50-2
98 changes: 50 additions & 48 deletions .rubocop.yml
Expand Up @@ -29,6 +29,37 @@ Layout/AccessModifierIndentation:
Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation

# checks whether the end keywords are aligned properly for `do` `end` blocks.
Layout/BlockAlignment:
# The value `start_of_block` means that the `end` should be aligned with line
# where the `do` keyword appears.
# The value `start_of_line` means it should be aligned with the whole
# expression's starting line.
# The value `either` means both are allowed.
EnforcedStyleAlignWith: start_of_line

Layout/DefEndAlignment:
# The value `def` means that `end` should be aligned with the def keyword.
# The value `start_of_line` means that `end` should be aligned with method
# calls like `private`, `public`, etc, if present in front of the `def`
# keyword on the same line.
EnforcedStyleAlignWith: start_of_line
# AutoCorrect: false
Severity: warning

# Align ends correctly.
Layout/EndAlignment:
# The value `keyword` means that `end` should be aligned with the matching
# keyword (`if`, `while`, etc.).
# The value `variable` means that in assignments, `end` should be aligned
# with the start of the variable on the left hand side of `=`. In all other
# situations, `end` should still be aligned with the keyword.
# The value `start_of_line` means that `end` should be aligned with the start
# of the line which the matching keyword appears on.
EnforcedStyleAlignWith: start_of_line
# AutoCorrect: false
Severity: warning

# Align the elements of a hash literal if they span more than one line.
Layout/HashAlignment:
# Alignment of entries using colon as separator. Valid values are:
Expand Down Expand Up @@ -76,6 +107,23 @@ Layout/HashAlignment:
# b: 2)
EnforcedLastArgumentHashStyle: ignore_implicit

Layout/LineLength:
Max: 120
# To make it possible to copy or click on URIs in the code, we allow lines
# containing a URI to be longer than Max.
AllowHeredoc: true
AllowURI: true
URISchemes:
- http
- https
# The IgnoreCopDirectives option causes the LineLength rule to ignore cop
# directives like '# rubocop: enable ...' when calculating a line's length.
IgnoreCopDirectives: false
# The IgnoredPatterns option is a list of !ruby/regexp and/or string
# elements. Strings will be converted to Regexp objects. A line that matches
# any regular expression listed in this option will be ignored by LineLength.
IgnoredPatterns: []

Layout/ParameterAlignment:
# Alignment of parameters in multi-line method calls.
#
Expand Down Expand Up @@ -1288,6 +1336,7 @@ Metrics/BlockLength:
Exclude:
- 'test/**/*'
- 'spec/**/*'
- 'tasks/ips.rb'

Metrics/BlockNesting:
CountBlocks: false
Expand All @@ -1304,23 +1353,6 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 6

Layout/LineLength:
Max: 80
# To make it possible to copy or click on URIs in the code, we allow lines
# containing a URI to be longer than Max.
AllowHeredoc: true
AllowURI: true
URISchemes:
- http
- https
# The IgnoreCopDirectives option causes the LineLength rule to ignore cop
# directives like '# rubocop: enable ...' when calculating a line's length.
IgnoreCopDirectives: false
# The IgnoredPatterns option is a list of !ruby/regexp and/or string
# elements. Strings will be converted to Regexp objects. A line that matches
# any regular expression listed in this option will be ignored by LineLength.
IgnoredPatterns: []

Metrics/MethodLength:
CountComments: false # count full line comments?
Max: 20
Expand All @@ -1331,6 +1363,7 @@ Metrics/ModuleLength:

Metrics/ParameterLists:
Max: 5
MaxOptionalParameters: 5
CountKeywordArgs: true

Metrics/PerceivedComplexity:
Expand All @@ -1342,37 +1375,6 @@ Metrics/PerceivedComplexity:
Lint/AssignmentInCondition:
AllowSafeAssignment: true

# checks whether the end keywords are aligned properly for `do` `end` blocks.
Layout/BlockAlignment:
# The value `start_of_block` means that the `end` should be aligned with line
# where the `do` keyword appears.
# The value `start_of_line` means it should be aligned with the whole
# expression's starting line.
# The value `either` means both are allowed.
EnforcedStyleAlignWith: start_of_line

Layout/DefEndAlignment:
# The value `def` means that `end` should be aligned with the def keyword.
# The value `start_of_line` means that `end` should be aligned with method
# calls like `private`, `public`, etc, if present in front of the `def`
# keyword on the same line.
EnforcedStyleAlignWith: start_of_line
# AutoCorrect: false
Severity: warning

# Align ends correctly.
Layout/EndAlignment:
# The value `keyword` means that `end` should be aligned with the matching
# keyword (`if`, `while`, etc.).
# The value `variable` means that in assignments, `end` should be aligned
# with the start of the variable on the left hand side of `=`. In all other
# situations, `end` should still be aligned with the keyword.
# The value `start_of_line` means that `end` should be aligned with the start
# of the line which the matching keyword appears on.
EnforcedStyleAlignWith: start_of_line
# AutoCorrect: false
Severity: warning

Lint/SuppressedException:
Exclude:
- 'spec/**/*'
Expand Down
24 changes: 8 additions & 16 deletions lib/rambling/trie.rb
@@ -1,9 +1,8 @@
# frozen_string_literal: true

%w(
comparable compressible compressor configuration container enumerable
inspectable invalid_operation readers serializers stringifyable nodes
version
comparable compressible compressor configuration container enumerable inspectable invalid_operation
readers serializers stringifyable nodes version
).each do |file|
require File.join('rambling', 'trie', file)
end
Expand Down Expand Up @@ -41,15 +40,12 @@ def create filepath = nil, reader = nil
# Available formats are +yml+, +marshal+, and +zip+ versions of all the
# previous formats. You can also define your own.
# @param [String] filepath the file to load the words from.
# @param [Serializer, nil] serializer the object responsible of loading
# the trie from disk
# @param [Serializer, nil] serializer the object responsible of loading the trie from disk.
# @return [Container] the trie just loaded.
# @yield [Container] the trie just loaded.
# @see Rambling::Trie::Serializers Serializers.
# @note Use of
# {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load
# Marshal.load} is generally discouraged. Only use the +.marshal+
# format with trusted input.
# @note Use of # {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load Marshal.load} is generally
# discouraged. Only use the +.marshal+ format with trusted input.
def load filepath, serializer = nil
serializer ||= serializers.resolve filepath
root = serializer.load filepath
Expand All @@ -64,10 +60,8 @@ def load filepath, serializer = nil
# previous formats. You can also define your own.
# @param [Container] trie the trie to dump into disk.
# @param [String] filepath the file to dump to serialized trie into.
# @param [Serializers::Serializer, nil] serializer the object responsible
# for trie serialization.
# @param [Serializers::Serializer, nil] serializer the object responsible for trie serialization.
# @return [void]
# serializing and dumping the trie into disk.
# @see Serializers Serializers.
def dump trie, filepath, serializer = nil
serializer ||= serializers.resolve filepath
Expand All @@ -76,10 +70,8 @@ def dump trie, filepath, serializer = nil
end

# Provides configuration properties for the +Rambling::Trie+ gem.
# @return [Configuration::Properties] the configured properties of the
# gem.
# @yield [Configuration::Properties] the configured properties of the
# gem.
# @return [Configuration::Properties] the configured properties of the gem.
# @yield [Configuration::Properties] the configured properties of the gem.
def config
yield properties if block_given?
properties
Expand Down
3 changes: 1 addition & 2 deletions lib/rambling/trie/comparable.rb
Expand Up @@ -7,8 +7,7 @@ module Comparable
# Compares two nodes.
# @param [Nodes::Node] other the node to compare against.
# @return [Boolean] +true+ if the nodes' {Nodes::Node#letter #letter} and
# {Nodes::Node#children_tree #children_tree} are equal, +false+
# otherwise.
# {Nodes::Node#children_tree #children_tree} are equal, +false+ otherwise.
def == other
letter == other.letter &&
terminal? == other.terminal? &&
Expand Down
6 changes: 2 additions & 4 deletions lib/rambling/trie/compressible.rb
Expand Up @@ -4,10 +4,8 @@ module Rambling
module Trie
# Provides the compressible behavior for the trie data structure.
module Compressible
# Indicates if the current {Rambling::Trie::Nodes::Node Node} can be
# compressed or not.
# @return [Boolean] +true+ for non-{Nodes::Node#terminal? terminal} nodes
# with one child, +false+ otherwise.
# Indicates if the current {Rambling::Trie::Nodes::Node Node} can be compressed or not.
# @return [Boolean] +true+ for non-{Nodes::Node#terminal? terminal} nodes with one child, +false+ otherwise.
def compressible?
!(root? || terminal?) && 1 == children_tree.size
end
Expand Down
14 changes: 2 additions & 12 deletions lib/rambling/trie/compressor.rb
Expand Up @@ -24,21 +24,11 @@ def compress_child_and_merge node
def merge node, other
letter = node.letter.to_s << other.letter.to_s

new_compressed_node(
letter.to_sym,
node.parent,
other.children_tree,
other.terminal?,
)
new_compressed_node letter.to_sym, node.parent, other.children_tree, other.terminal?
end

def compress_children_and_copy node
new_compressed_node(
node.letter,
node.parent,
compress_children(node.children_tree),
node.terminal?,
)
new_compressed_node node.letter, node.parent, compress_children(node.children_tree), node.terminal?
end

def compress_children tree
Expand Down
15 changes: 4 additions & 11 deletions lib/rambling/trie/configuration/properties.rb
Expand Up @@ -6,21 +6,18 @@ module Configuration
# Provides configurable properties for Rambling::Trie.
class Properties
# The configured {Readers Readers}.
# @return [ProviderCollection<Readers::Reader>] the mapping of
# configured {Readers Readers}.
# @return [ProviderCollection<Readers::Reader>] the mapping of configured {Readers Readers}.
attr_reader :readers

# The configured {Serializers Serializers}.
# @return [ProviderCollection<Serializers::Serializer>] the mapping of
# configured {Serializers Serializers}.
# @return [ProviderCollection<Serializers::Serializer>] the mapping of configured {Serializers Serializers}.
attr_reader :serializers

# The configured {Compressor Compressor}.
# @return [Compressor] the configured compressor.
attr_accessor :compressor

# The configured +root_builder+, which returns a {Nodes::Node Node}
# when called.
# The configured +root_builder+, which returns a {Nodes::Node Node} when called.
# @return [Proc<Nodes::Node>] the configured +root_builder+.
attr_accessor :root_builder

Expand Down Expand Up @@ -50,11 +47,7 @@ def reset

def reset_readers
plain_text_reader = Rambling::Trie::Readers::PlainText.new

@readers = Rambling::Trie::Configuration::ProviderCollection.new(
:reader,
txt: plain_text_reader,
)
@readers = Rambling::Trie::Configuration::ProviderCollection.new :reader, txt: plain_text_reader
end

def reset_serializers
Expand Down
30 changes: 10 additions & 20 deletions lib/rambling/trie/configuration/provider_collection.rb
Expand Up @@ -16,11 +16,10 @@ class ProviderCollection
# Sets the default provider. Needs to be one of the configured
# providers.
# @param [TProvider] provider the provider to use as default.
# @raise [ArgumentError] when the given provider is not in the
# provider collection.
# @raise [ArgumentError] when the given provider is not in the provider collection.
# @note If no providers have been configured, +nil+ will be assigned.
# @return [TProvider, nil] the default provider to use when a provider
# cannot be resolved in {ProviderCollection#resolve #resolve}.
# @return [TProvider, nil] the default provider to use when a provider cannot be resolved in
# {ProviderCollection#resolve #resolve}.
attr_reader :default

# Creates a new provider collection.
Expand All @@ -36,35 +35,28 @@ def initialize name, providers = {}, default = nil
end

# Adds a new provider to the provider collection.
# @param [Symbol] extension the extension that the provider will
# correspond to.
# @param [TProvider] provider the provider to add to the provider
# collection.
# @param [Symbol] extension the extension that the provider will correspond to.
# @param [TProvider] provider the provider to add to the provider collection.
# @return [TProvider] the provider just added.
def add extension, provider
providers[extension] = provider
end

def default= provider
unless contains? provider
raise ArgumentError,
"default #{name} should be part of configured #{name}s"
end
raise ArgumentError, "default #{name} should be part of configured #{name}s" unless contains? provider

@default = provider
end

# List of configured providers.
# @return [Hash<Symbol, TProvider>] the mapping of extensions to their
# corresponding providers.
# @return [Hash<Symbol, TProvider>] the mapping of extensions to their corresponding providers.
def providers
@providers ||= {}
end

# Resolves the provider from a filepath based on the file extension.
# @param [String] filepath the filepath to resolve into a provider.
# @return [TProvider, nil] the provider for the given file's extension.
# {#default} if not found.
# @return [TProvider, nil] the provider for the given file's extension. {#default} if not found.
def resolve filepath
providers[file_format filepath] || default
end
Expand All @@ -88,8 +80,7 @@ def formats
# Get provider corresponding to a given format.
# @param [Symbol] format the format to search for in the collection.
# @return [TProvider] the provider corresponding to that format.
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D
# Hash#[]
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D Hash#[]
def [] format
providers[format]
end
Expand All @@ -113,8 +104,7 @@ def file_format filepath
end

def contains? provider
provider.nil? ||
(providers.any? && provider_instances.include?(provider))
provider.nil? || (providers.any? && provider_instances.include?(provider))
end

alias_method :provider_instances, :values
Expand Down

0 comments on commit ff8aecd

Please sign in to comment.