From 888221511b0cdc049bb814f1c4717adb28661350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Gonz=C3=A1lez?= Date: Mon, 15 May 2023 13:26:32 -0400 Subject: [PATCH 1/4] Add rubocop plugin --- .codeclimate.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.codeclimate.yml b/.codeclimate.yml index b4bd17f..6e63d78 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -9,3 +9,6 @@ plugins: # Markdown lint with rules from https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md markdownlint: enabled: true + # Ruby lint + rubocop: + enabled: true From 1a56082c812b133f140089f6008ef263e883f381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Gonz=C3=A1lez?= Date: Sat, 13 May 2023 14:20:47 -0400 Subject: [PATCH 2/4] use latest rubocop channel --- .codeclimate.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.codeclimate.yml b/.codeclimate.yml index 6e63d78..95efb8a 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -12,3 +12,4 @@ plugins: # Ruby lint rubocop: enabled: true + channel: rubocop-1-50-2 From 8574e79fe60721c590740627a447e6dddf39a734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Gonz=C3=A1lez?= Date: Mon, 15 May 2023 12:11:41 -0400 Subject: [PATCH 3/4] rubocop: allow all params to be optional --- .rubocop.yml | 98 ++++++++++---------- spec/support/shared_examples/a_serializer.rb | 4 +- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c07e7f6..293c7a0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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: @@ -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. # @@ -1288,6 +1336,7 @@ Metrics/BlockLength: Exclude: - 'test/**/*' - 'spec/**/*' + - 'tasks/ips.rb' Metrics/BlockNesting: CountBlocks: false @@ -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 @@ -1331,6 +1363,7 @@ Metrics/ModuleLength: Metrics/ParameterLists: Max: 5 + MaxOptionalParameters: 5 CountKeywordArgs: true Metrics/PerceivedComplexity: @@ -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/**/*' diff --git a/spec/support/shared_examples/a_serializer.rb b/spec/support/shared_examples/a_serializer.rb index 4b7882f..b5029d7 100644 --- a/spec/support/shared_examples/a_serializer.rb +++ b/spec/support/shared_examples/a_serializer.rb @@ -52,9 +52,7 @@ it "loads a compressed=#{compress_value} object" do loaded = serializer.load filepath - if :file != file_format - expect(loaded.compressed?).to be compress_value - end + expect(loaded.compressed?).to be compress_value unless :file == file_format end end end From 641c18df065d751031ea42e8e29f962e92335c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Gonz=C3=A1lez?= Date: Mon, 15 May 2023 21:10:51 -0400 Subject: [PATCH 4/4] Change char max to 100 --- lib/rambling/trie.rb | 24 +++------ lib/rambling/trie/comparable.rb | 3 +- lib/rambling/trie/compressible.rb | 6 +-- lib/rambling/trie/compressor.rb | 14 +---- lib/rambling/trie/configuration/properties.rb | 15 ++---- .../trie/configuration/provider_collection.rb | 30 ++++------- lib/rambling/trie/container.rb | 52 +++++++------------ lib/rambling/trie/enumerable.rb | 3 +- lib/rambling/trie/nodes/compressed.rb | 3 +- lib/rambling/trie/nodes/missing.rb | 3 +- lib/rambling/trie/nodes/node.rb | 39 +++++--------- lib/rambling/trie/readers/plain_text.rb | 3 +- lib/rambling/trie/readers/reader.rb | 6 +-- lib/rambling/trie/serializers/marshal.rb | 29 ++++------- lib/rambling/trie/serializers/yaml.rb | 23 +++----- lib/rambling/trie/serializers/zip.rb | 2 +- 16 files changed, 85 insertions(+), 170 deletions(-) diff --git a/lib/rambling/trie.rb b/lib/rambling/trie.rb index ddef627..04c8fe2 100644 --- a/lib/rambling/trie.rb +++ b/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 @@ -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 @@ -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 @@ -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 diff --git a/lib/rambling/trie/comparable.rb b/lib/rambling/trie/comparable.rb index e337a8a..a5678b8 100644 --- a/lib/rambling/trie/comparable.rb +++ b/lib/rambling/trie/comparable.rb @@ -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? && diff --git a/lib/rambling/trie/compressible.rb b/lib/rambling/trie/compressible.rb index 463578a..40324ef 100644 --- a/lib/rambling/trie/compressible.rb +++ b/lib/rambling/trie/compressible.rb @@ -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 diff --git a/lib/rambling/trie/compressor.rb b/lib/rambling/trie/compressor.rb index 810442a..9bced88 100644 --- a/lib/rambling/trie/compressor.rb +++ b/lib/rambling/trie/compressor.rb @@ -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 diff --git a/lib/rambling/trie/configuration/properties.rb b/lib/rambling/trie/configuration/properties.rb index 1de51c9..a40e45d 100644 --- a/lib/rambling/trie/configuration/properties.rb +++ b/lib/rambling/trie/configuration/properties.rb @@ -6,21 +6,18 @@ module Configuration # Provides configurable properties for Rambling::Trie. class Properties # The configured {Readers Readers}. - # @return [ProviderCollection] the mapping of - # configured {Readers Readers}. + # @return [ProviderCollection] the mapping of configured {Readers Readers}. attr_reader :readers # The configured {Serializers Serializers}. - # @return [ProviderCollection] the mapping of - # configured {Serializers Serializers}. + # @return [ProviderCollection] 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] the configured +root_builder+. attr_accessor :root_builder @@ -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 diff --git a/lib/rambling/trie/configuration/provider_collection.rb b/lib/rambling/trie/configuration/provider_collection.rb index c0fcd93..7e82743 100644 --- a/lib/rambling/trie/configuration/provider_collection.rb +++ b/lib/rambling/trie/configuration/provider_collection.rb @@ -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. @@ -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] the mapping of extensions to their - # corresponding providers. + # @return [Hash] 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 @@ -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 @@ -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 diff --git a/lib/rambling/trie/container.rb b/lib/rambling/trie/container.rb index da6c90e..8570049 100644 --- a/lib/rambling/trie/container.rb +++ b/lib/rambling/trie/container.rb @@ -41,22 +41,19 @@ def concat words words.map { |word| add word } end - # Compresses the existing trie using redundant node elimination. Marks - # the trie as compressed. Does nothing if the trie has already been - # compressed. + # Compresses the existing trie using redundant node elimination. + # Marks the trie as compressed. + # Does nothing if the trie has already been compressed. # @return [self] - # @note This method replaces the root {Nodes::Raw Raw} node with a - # {Nodes::Compressed Compressed} version of it. + # @note This method replaces the root {Nodes::Raw Raw} node with a {Nodes::Compressed Compressed} version of it. def compress! self.root = compress_root unless root.compressed? self end - # Compresses the existing trie using redundant node elimination. Returns - # a new trie with the compressed root. - # @return [Container] A new {Container} with the {Nodes::Compressed - # Compressed} root node or self if the trie has already been - # compressed. + # Compresses the existing trie using redundant node elimination. Returns a new trie with the compressed root. + # @return [Container] A new {Container} with the {Nodes::Compressed Compressed} root node + # or self if the trie has already been compressed. def compress return self if root.compressed? @@ -65,8 +62,7 @@ def compress # Checks if a path for a word or partial word exists in the trie. # @param [String] word the word or partial word to look for in the trie. - # @return [Boolean] +true+ if the word or partial word is found, +false+ - # otherwise. + # @return [Boolean] +true+ if the word or partial word is found, +false+ otherwise. # @see Nodes::Node#partial_word? def partial_word? word = '' root.partial_word? word.chars @@ -74,8 +70,8 @@ def partial_word? word = '' # Checks if a whole word exists in the trie. # @param [String] word the word to look for in the trie. - # @return [Boolean] +true+ only if the word is found and the last - # character corresponds to a terminal node, +false+ otherwise. + # @return [Boolean] +true+ only if the word is found and the last character corresponds to a terminal node, + # +false+ otherwise. # @see Nodes::Node#word? def word? word = '' root.word? word.chars @@ -83,18 +79,15 @@ def word? word = '' # Returns all words that start with the specified characters. # @param [String] word the word to look for in the trie. - # @return [Array] all the words contained in the trie that start - # with the specified characters. + # @return [Array] all the words contained in the trie that start with the specified characters. # @see Nodes::Node#scan def scan word = '' root.scan(word.chars).to_a end - # Returns all words within a string that match a word contained in the - # trie. + # Returns all words within a string that match a word contained in the trie. # @param [String] phrase the string to look for matching words in. - # @return [Enumerator] all the words in the given string that - # match a word in the trie. + # @return [Enumerator] all the words in the given string that match a word in the trie. # @yield [String] each word found in phrase. def words_within phrase words_within_root(phrase).to_a @@ -102,8 +95,7 @@ def words_within phrase # Checks if there are any valid words in a given string. # @param [String] phrase the string to look for matching words in. - # @return [Boolean] +true+ if any word within phrase is contained in the - # trie, +false+ otherwise. + # @return [Boolean] +true+ if any word within phrase is contained in the trie, +false+ otherwise. # @see Container#words_within def words_within? phrase words_within_root(phrase).any? @@ -141,33 +133,29 @@ def [] letter end # Root node's child nodes. - # @return [Array] the array of children nodes contained in - # the root node. + # @return [Array] the array of children nodes contained in the root node. # @see Nodes::Node#children def children root.children end # Root node's children tree. - # @return [Hash] the children tree hash contained in - # the root node, consisting of +:letter => node+. + # @return [Hash] the children tree hash contained in the root node, consisting of + # +:letter => node+. # @see Nodes::Node#children_tree def children_tree root.children_tree end - # Indicates if the root {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 root {Nodes::Node Node} can be compressed or not. + # @return [Boolean] +true+ for non-{Nodes::Node#terminal? terminal} nodes with one child, +false+ otherwise. def compressed? root.compressed? end # Array of words contained in the root {Nodes::Node Node}. # @return [Array] all words contained in this trie. - # @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-to_a - # Enumerable#to_a + # @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-to_a Enumerable#to_a def to_a root.to_a end diff --git a/lib/rambling/trie/enumerable.rb b/lib/rambling/trie/enumerable.rb index 514a2ba..b3ca64f 100644 --- a/lib/rambling/trie/enumerable.rb +++ b/lib/rambling/trie/enumerable.rb @@ -7,8 +7,7 @@ module Enumerable include ::Enumerable # Returns number of words contained in the trie - # @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-count - # Enumerable#count + # @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-count Enumerable#count alias_method :size, :count # Iterates over the words contained in the trie. diff --git a/lib/rambling/trie/nodes/compressed.rb b/lib/rambling/trie/nodes/compressed.rb index 859dc72..5c901c2 100644 --- a/lib/rambling/trie/nodes/compressed.rb +++ b/lib/rambling/trie/nodes/compressed.rb @@ -11,8 +11,7 @@ class Compressed < Rambling::Trie::Nodes::Node # @raise [InvalidOperation] if the trie is already compressed. # @return [void] def add _ - raise Rambling::Trie::InvalidOperation, - 'Cannot add word to compressed trie' + raise Rambling::Trie::InvalidOperation, 'Cannot add word to compressed trie' end # Always return +true+ for a compressed node. diff --git a/lib/rambling/trie/nodes/missing.rb b/lib/rambling/trie/nodes/missing.rb index 17c3bb1..b0892e6 100644 --- a/lib/rambling/trie/nodes/missing.rb +++ b/lib/rambling/trie/nodes/missing.rb @@ -3,8 +3,7 @@ module Rambling module Trie module Nodes - # A representation of a missing node in the trie data structure. Returned - # when a node is not found. + # A representation of a missing node in the trie data structure. Returned when a node is not found. class Missing < Rambling::Trie::Nodes::Node end end diff --git a/lib/rambling/trie/nodes/node.rb b/lib/rambling/trie/nodes/node.rb index 46cfef5..4163e9d 100644 --- a/lib/rambling/trie/nodes/node.rb +++ b/lib/rambling/trie/nodes/node.rb @@ -22,8 +22,7 @@ class Node attr_reader :letter # Child nodes tree. - # @return [Hash] the children tree hash, consisting of - # +:letter => node+. + # @return [Hash] the children tree hash, consisting of +:letter => node+. attr_accessor :children_tree # Parent node. @@ -40,8 +39,7 @@ def initialize letter = nil, parent = nil, children_tree = {} end # Child nodes. - # @return [Array] the array of child nodes contained - # in the current node. + # @return [Array] the array of child nodes contained in the current node. def children children_tree.values end @@ -57,8 +55,7 @@ def first_child end # Indicates if the current node is the root node. - # @return [Boolean] +true+ if the node does not have a parent, +false+ - # otherwise. + # @return [Boolean] +true+ if the node does not have a parent, +false+ otherwise. def root? !parent end @@ -82,8 +79,7 @@ def letter= letter # Checks if a path for a set of characters exists in the trie. # @param [Array] chars the characters to look for in the trie. - # @return [Boolean] +true+ if the characters are found, +false+ - # otherwise. + # @return [Boolean] +true+ if the characters are found, +false+ otherwise. def partial_word? chars return true if chars.empty? @@ -92,8 +88,7 @@ def partial_word? chars # Checks if a path for set of characters represents a word in the trie. # @param [Array] chars the characters to look for in the trie. - # @return [Boolean] +true+ if the characters are found and form a word, - # +false+ otherwise. + # @return [Boolean] +true+ if the characters are found and form a word, +false+ otherwise. def word? chars = [] return terminal? if chars.empty? @@ -112,8 +107,7 @@ def scan chars # Returns all words that match a prefix of any length within chars. # @param [String] chars the chars to base the prefix on. - # @return [Enumerator] all the words that match a prefix given - # by chars. + # @return [Enumerator] all the words that match a prefix by chars. # @yield [String] each word found. def match_prefix chars return enum_for :match_prefix, chars unless block_given? @@ -128,8 +122,7 @@ def match_prefix chars # Get {Node Node} corresponding to a given letter. # @param [Symbol] letter the letter to search for in the node. # @return [Node] the node corresponding to that letter. - # @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 [] letter children_tree[letter] end @@ -137,31 +130,25 @@ def [] letter # Set the {Node Node} that corresponds to a given letter. # @param [Symbol] letter the letter to insert or update in the node's # @param [Node] node the {Node Node} to assign to that letter. - # @return [Node] the node corresponding to the inserted or - # updated letter. - # @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D - # Hash#[] + # @return [Node] the node corresponding to the inserted or updated letter. + # @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D Hash#[] def []= letter, node children_tree[letter] = node end - # Check if a {Node Node}'s children tree contains a given - # letter. + # Check if a {Node Node}'s children tree contains a given letter. # @param [Symbol] letter the letter to search for in the node. # @return [Boolean] +true+ if the letter is present, +false+ otherwise. - # @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-has_key-3F - # Hash#key? + # @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-has_key-3F Hash#key? def key? letter children_tree.key? letter end # Delete a given letter and its corresponding {Node Node} from # this {Node Node}'s children tree. - # @param [Symbol] letter the letter to delete from the node's children - # tree. + # @param [Symbol] letter the letter to delete from the node's children tree. # @return [Node] the node corresponding to the deleted letter. - # @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-delete - # Hash#delete + # @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-delete Hash#delete def delete letter children_tree.delete letter end diff --git a/lib/rambling/trie/readers/plain_text.rb b/lib/rambling/trie/readers/plain_text.rb index 0ce6296..2986976 100644 --- a/lib/rambling/trie/readers/plain_text.rb +++ b/lib/rambling/trie/readers/plain_text.rb @@ -6,8 +6,7 @@ module Readers # File reader for +.txt+ files. class PlainText < Reader # Yields each word read from a +.txt+ file. - # @param [String] filepath the full path of the file to load the words - # from. + # @param [String] filepath the full path of the file to load the words from. # @yield [String] Each line read from the file. # @return [self] def each_word filepath diff --git a/lib/rambling/trie/readers/reader.rb b/lib/rambling/trie/readers/reader.rb index b08f2ba..727d677 100644 --- a/lib/rambling/trie/readers/reader.rb +++ b/lib/rambling/trie/readers/reader.rb @@ -6,10 +6,8 @@ module Readers # Base class for all readers. class Reader # Yields each word read from given file. - # @abstract Subclass and override {#each_word} to fit to a particular - # file format. - # @param [String] filepath the full path of the file to load the words - # from. + # @abstract Subclass and override {#each_word} to fit to a particular file format. + # @param [String] filepath the full path of the file to load the words from. # @yield [String] Each line read from the file. # @return [self] def each_word filepath diff --git a/lib/rambling/trie/serializers/marshal.rb b/lib/rambling/trie/serializers/marshal.rb index e1fb5d5..758ad09 100644 --- a/lib/rambling/trie/serializers/marshal.rb +++ b/lib/rambling/trie/serializers/marshal.rb @@ -6,36 +6,27 @@ module Serializers # Serializer for Ruby marshal format (+.marshal+) files. class Marshal < Serializer # Creates a new Marshal serializer. - # @param [Serializer] serializer the serializer responsible to write to - # and read from disk. + # @param [Serializer] serializer the serializer responsible to write to and read from disk. def initialize serializer = nil - @serializer = serializer || Rambling::Trie::Serializers::File.new super() + @serializer = serializer || Rambling::Trie::Serializers::File.new end - # Loads marshaled object from contents in filepath and deserializes it - # into a {Nodes::Node Node}. - # @param [String] filepath the full path of the file to load the - # marshaled object from. + # Loads marshaled object from contents in filepath and deserializes it into a {Nodes::Node Node}. + # @param [String] filepath the full path of the file to load the marshaled object from. # @return [Nodes::Node] The deserialized {Nodes::Node Node}. - # @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load - # Marshal.load - # @note Use of - # {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load - # Marshal.load} is generally discouraged. Only use this with trusted - # input. + # @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load Marshal.load + # @note Use of {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load Marshal.load} is generally + # discouraged. Only use this with trusted input. def load filepath ::Marshal.load serializer.load filepath end - # Serializes a {Nodes::Node Node} and dumps it as a marshaled object - # into filepath. + # Serializes a {Nodes::Node Node} and dumps it as a marshaled object into filepath. # @param [Nodes::Node] node the node to serialize - # @param [String] filepath the full path of the file to dump the - # marshaled object into. + # @param [String] filepath the full path of the file to dump the marshaled object into. # @return [Numeric] number of bytes written to disk. - # @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-dump - # Marshal.dump + # @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-dump Marshal.dump def dump node, filepath serializer.dump ::Marshal.dump(node), filepath end diff --git a/lib/rambling/trie/serializers/yaml.rb b/lib/rambling/trie/serializers/yaml.rb index cadd4cb..fc0a886 100644 --- a/lib/rambling/trie/serializers/yaml.rb +++ b/lib/rambling/trie/serializers/yaml.rb @@ -6,20 +6,16 @@ module Serializers # Serializer for Ruby yaml format (+.yaml+, or +.yml+) files. class Yaml < Serializer # Creates a new Yaml serializer. - # @param [Serializer] serializer the serializer responsible to write to - # and read from disk. + # @param [Serializer] serializer the serializer responsible to write to and read from disk. def initialize serializer = nil - @serializer = serializer || Rambling::Trie::Serializers::File.new super() + @serializer = serializer || Rambling::Trie::Serializers::File.new end - # Loads serialized object from YAML file in filepath and deserializes - # it into a {Nodes::Node Node}. - # @param [String] filepath the full path of the file to load the - # serialized YAML object from. + # Loads serialized object from YAML file in filepath and deserializes it into a {Nodes::Node Node}. + # @param [String] filepath the full path of the file to load the serialized YAML object from. # @return [Nodes::Node] The deserialized {Nodes::Node Node}. - # @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-safe_load - # Psych.safe_load + # @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-safe_load Psych.safe_load def load filepath require 'yaml' ::YAML.safe_load( @@ -33,14 +29,11 @@ def load filepath ) end - # Serializes a {Nodes::Node Node} and dumps it as a YAML object into - # filepath. + # Serializes a {Nodes::Node Node} and dumps it as a YAML object into filepath. # @param [Nodes::Node] node the node to serialize - # @param [String] filepath the full path of the file to dump the YAML - # object into. + # @param [String] filepath the full path of the file to dump the YAML object into. # @return [Numeric] number of bytes written to disk. - # @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-dump - # Psych.dump + # @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-dump Psych.dump def dump node, filepath require 'yaml' serializer.dump ::YAML.dump(node), filepath diff --git a/lib/rambling/trie/serializers/zip.rb b/lib/rambling/trie/serializers/zip.rb index 8c01f96..2f01886 100644 --- a/lib/rambling/trie/serializers/zip.rb +++ b/lib/rambling/trie/serializers/zip.rb @@ -11,8 +11,8 @@ class Zip < Serializer # @param [Configuration::Properties] properties the configuration # properties set up so far. def initialize properties - @properties = properties super() + @properties = properties end # Unzip contents from specified filepath and load in contents from