Skip to content

Commit

Permalink
Add (x || raise) for nil unwrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzedge committed Feb 20, 2024
1 parent 8588bf9 commit 68c2aa9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/rambling/trie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create filepath = nil, reader = nil
if filepath
reader ||= readers.resolve filepath
# noinspection RubyMismatchedArgumentType,RubyNilAnalysis
reader.each_word filepath do |word|
(reader || raise).each_word filepath do |word|
container << word
end
end
Expand All @@ -48,7 +48,7 @@ def create filepath = nil, reader = nil
# discouraged. Only use the +.marshal+ format with trusted input.
def load filepath, serializer = nil
serializer ||= serializers.resolve filepath
root = serializer.load filepath
root = (serializer || raise).load filepath
Rambling::Trie::Container.new root, compressor do |container|
yield container if block_given?
end
Expand All @@ -66,7 +66,7 @@ def load filepath, serializer = nil
def dump trie, filepath, serializer = nil
serializer ||= serializers.resolve filepath
# noinspection RubyNilAnalysis
serializer.dump trie.root, filepath
(serializer || raise).dump trie.root, filepath
end

# Provides configuration properties for the +Rambling::Trie+ gem.
Expand Down
4 changes: 3 additions & 1 deletion lib/rambling/trie/configuration/provider_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def file_format filepath
end

def contains? provider
provider.nil? || (providers.any? && provider_instances.include?(provider))
return true if provider.nil?
p = (provider || raise)
providers.any? && provider_instances.include?(p)
end

alias_method :provider_instances, :values
Expand Down
16 changes: 8 additions & 8 deletions lib/rambling/trie/nodes/compressed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def compressed?
private

def partial_word_chars? chars
child = children_tree[chars.first.to_sym]
child = children_tree[(chars.first || raise).to_sym]
return false unless child

child_letter = child.letter.to_s

if chars.size >= child_letter.size
letter = chars.slice!(0, child_letter.size).join
letter = (chars.slice!(0, child_letter.size) || raise).join
return child.partial_word? chars if child_letter == letter
end

Expand All @@ -39,7 +39,7 @@ def partial_word_chars? chars
end

def word_chars? chars
letter = chars.slice! 0
letter = chars.slice!(0) || raise
letter_sym = letter.to_sym

child = children_tree[letter_sym]
Expand All @@ -50,21 +50,21 @@ def word_chars? chars

break if chars.empty?

letter << chars.slice!(0)
letter << (chars.slice!(0) || raise)
letter_sym = letter.to_sym
end

false
end

def closest_node chars
child = children_tree[chars.first.to_sym]
child = children_tree[(chars.first || raise).to_sym]
return missing unless child

child_letter = child.letter.to_s

if chars.size >= child_letter.size
letter = chars.slice!(0, child_letter.size).join
letter = (chars.slice!(0, child_letter.size) || raise).join
return child.scan chars if child_letter == letter
end

Expand All @@ -79,11 +79,11 @@ def children_match_prefix chars

return EMPTY_ENUMERATOR if chars.empty?

child = children_tree[chars.first.to_sym]
child = children_tree[(chars.first || raise).to_sym]
return EMPTY_ENUMERATOR unless child

child_letter = child.letter.to_s
letter = chars.slice!(0, child_letter.size).join
letter = (chars.slice!(0, child_letter.size) || raise).join

return EMPTY_ENUMERATOR unless child_letter == letter

Expand Down
10 changes: 5 additions & 5 deletions lib/rambling/trie/nodes/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def compressed?
private

def add_to_children_tree chars
letter = chars.pop
letter = chars.pop || raise
child = children_tree[letter] || new_node(letter)
child.add chars
child
Expand All @@ -40,23 +40,23 @@ def new_node letter
end

def partial_word_chars? chars = []
letter = chars.shift.to_sym
letter = (chars.shift || raise).to_sym
child = children_tree[letter]
return false unless child

child.partial_word? chars
end

def word_chars? chars = []
letter = chars.shift.to_sym
letter = (chars.shift || raise).to_sym
child = children_tree[letter]
return false unless child

child.word? chars
end

def closest_node chars
letter = chars.shift.to_sym
letter = (chars.shift || raise).to_sym
child = children_tree[letter]
return missing unless child

Expand All @@ -68,7 +68,7 @@ def children_match_prefix chars

return EMPTY_ENUMERATOR if chars.empty?

letter = chars.shift.to_sym
letter = (chars.shift || raise).to_sym
child = children_tree[letter]

return EMPTY_ENUMERATOR unless child
Expand Down
6 changes: 5 additions & 1 deletion lib/rambling/trie/serializers/zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def load filepath

::Zip::File.open filepath do |zip|
entry = zip.entries.first
return nil if entry.nil?
raise if entry.nil?

entry_path = path entry.name
entry.extract entry_path

serializer = serializers.resolve entry.name
raise if serializer.nil?

serializer.load entry_path
end
end
Expand All @@ -50,6 +52,8 @@ def dump contents, filepath

entry_path = path filename
serializer = serializers.resolve filename

raise if serializer.nil?
serializer.dump contents, entry_path

zip.add filename, entry_path
Expand Down

0 comments on commit 68c2aa9

Please sign in to comment.