lib/ruby-dictionary/word_path.rb in ruby-dictionary-1.0.1 vs lib/ruby-dictionary/word_path.rb in ruby-dictionary-1.1.0
- old
+ new
@@ -1,28 +1,35 @@
class Dictionary
class WordPath
- def initialize
+ def initialize(case_sensitive)
+ @case_sensitive = !!case_sensitive
@is_leaf = false
@word_paths = {}
end
+ def case_sensitive?
+ @case_sensitive
+ end
+
def leaf?
@is_leaf
end
def leaf=(is_leaf)
@is_leaf = !!is_leaf
end
def <<(word)
- raise ArgumentError, "must be a string" unless word.kind_of?(String)
- _append(word.strip.downcase)
+ raise ArgumentError, 'must be a string' unless word.kind_of?(String)
+ word = word.downcase unless @case_sensitive
+ _append(word.strip)
end
def find(word)
- raise ArgumentError, "must be a string" unless word.kind_of?(String)
- _find(word.strip.downcase)
+ raise ArgumentError, 'must be a string' unless word.kind_of?(String)
+ word = word.downcase unless @case_sensitive
+ _find(word.strip)
end
def suffixes
[].tap do |suffixes|
@word_paths.each do |letter, path|
@@ -31,11 +38,11 @@
end
end
end
def hash
- self.class.hash ^ @is_leaf.hash ^ @word_paths.hash
+ self.class.hash ^ @is_leaf.hash ^ @word_paths.hash ^ @case_sensitive.hash
end
def ==(obj)
obj.class == self.class && obj.hash == self.hash
end
@@ -63,10 +70,10 @@
def _append(word)
return if word.empty?
char = word[0]
- word_path = @word_paths[char] ||= self.class.new
+ word_path = @word_paths[char] ||= self.class.new(@case_sensitive)
if word.size == 1
word_path.leaf = true
else
word_path._append(word[1, word.size])