lib/rambling/trie/root.rb in rambling-trie-0.5.2 vs lib/rambling/trie/root.rb in rambling-trie-0.6.0

- old
+ new

@@ -8,10 +8,28 @@ super self.compressed = false yield self if block_given? end + # Adds a branch to the trie based on the word, without changing the passed word. + # @param [String] word the word to add the branch from. + # @return [Node] the just added branch's root node. + # @raise [InvalidOperation] if the trie is already compressed. + # @see Branches#add + # @note Avoids clearing the contents of the word variable. + def add(word) + super word.clone + end + + alias_method :<<, :add + + # @deprecated Use `#partial_word?` instead. + def branch?(word = '') + warn 'The `#branch?` method will be deprecated, please use `#partial_word?` instead.' + partial_word? word + end + # Compresses the existing tree using redundant node elimination. Flags the trie as compressed. # @return [Root] self def compress! self.compressed = (compressed? || !!compress_tree!) self @@ -24,33 +42,29 @@ end # 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. - def branch?(word = '') - is? :branch, word + def partial_word?(word = '') + is? :partial_word, word end + alias_method :match?, :partial_word? + + # If the current node is the root node. + # @return [Boolean] `true` + def root? + true + end + # 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. def word?(word = '') is? :word, word end alias_method :include?, :word? - - # Adds a branch to the trie based on the word, without changing the passed word. - # @param [String] word the word to add the branch from. - # @return [Node] the just added branch's root node. - # @raise [InvalidOperation] if the trie is already compressed. - # @see Branches#add - # @note Avoids clearing the contents of the word variable. - def add(word) - super word.clone - end - - alias_method :<<, :add private attr_accessor :compressed