class MyStorageGem::Storage

Public Class Methods

new() click to toggle source
# File lib/Storage.rb, line 7
def initialize
  @root = Node.new ''
  @last_node = @root
end

Public Instance Methods

add(word) click to toggle source
# File lib/Storage.rb, line 12
def add(word)
  key_index = word.size
  i = 0
  # достигнут конец ключа
  while key_index !=0 do
    key_index-=1
    leaf_found = false

    # если такой узел уже существует, пометить его как текущий и пойти далее
    @last_node.leafs.each do |leaf|
      if leaf.value == word[i]
        @last_node = leaf
        leaf_found = true
      end
      break if leaf_found
    end

    # если такого узла нет, добавить его и сделать текущим
    unless leaf_found
      new_node = Node.new(word[i])
      @last_node.leafs.push new_node
      @last_node = new_node
    end
    i+=1
  end
  @last_node = @root # переключиться в корень
end
assemble_like_list(prefix, like) click to toggle source
# File lib/Storage.rb, line 63
def assemble_like_list(prefix, like)
  like.push prefix.map { |node| node.value }.join('')
  prefix[prefix.size - 1].leafs.each do |leaf|
    list = []
    list.concat(prefix.map { |node| node.value })
    list.push(leaf.value)

    if leaf.leafs.any?
      new_prefix = []
      new_prefix.concat prefix
      new_prefix.push leaf
      assemble_like_list(new_prefix, like)
    else
      like.push list.join('')
    end
  end
  like
end
clean() click to toggle source
# File lib/Storage.rb, line 144
def clean
  @root = Node.new ''
  @last_node = @root
end
contains?(word) click to toggle source
# File lib/Storage.rb, line 40
def contains?(word)
  key_index = word.size
  i = 0
  leaf_found = false
  # достигнут конец ключа
  while key_index != 0 do
    key_index-=1

    @last_node.leafs.each do |leaf|
      leaf_found = false
      if leaf.value == word[i]
        @last_node = leaf
        leaf_found = true
      end
      break if leaf_found
    end
    i+=1
  end

  @last_node = @root # переключиться в корень
  leaf_found
end
find(word) click to toggle source
# File lib/Storage.rb, line 83
def find(word)
  raise ArgumentError.new('wrong size of arguments, pass at least 3 character') if word.size < 3

  key_index = word.size
  i = 0
  leaf_found = false
  keys_start = []

  # достигнут конец ключа
  while key_index != 0 do
    key_index = key_index - 1

    @last_node.leafs.each do |leaf|
      leaf_found = false
      if leaf.value == word[i]
        @last_node = leaf
        leaf_found = true
        keys_start.push leaf
      end
      break if leaf_found
    end
    i+=1
  end
  like = []
  if leaf_found
    like = assemble_like_list(keys_start, like)
  end
  @last_node = @root
  like
end
find_all() click to toggle source
# File lib/Storage.rb, line 114
def find_all
  assemble_like_list([@root], [])[1..-1]
end
load_from_file(file_path) click to toggle source

загружает слова из файла.

# File lib/Storage.rb, line 119
def load_from_file(file_path)
  File.read(file_path).split(',').each { |word| add word }
end
load_from_zip(filename) click to toggle source

загружает слова из zip архива.

# File lib/Storage.rb, line 124
def load_from_zip(filename)
  Zip::ZipFile.open(filename) do |zip_file|
    zip_file.each do |entry|
      entry.get_input_stream.read.split(',').each { |word| add word }
    end
  end
end
save_to_file(filename) click to toggle source

сохраняет все хранимые слова в файл.

# File lib/Storage.rb, line 133
def save_to_file(filename)
  File.write(filename, find_all.join(','))
end
save_to_zip(filename) click to toggle source

сохраняет все хранимые слова в zip архив.

# File lib/Storage.rb, line 138
def save_to_zip(filename)
  Zip::ZipFile.open(filename, Zip::ZipFile::CREATE) { |zipfile|
    zipfile.get_output_stream('words.txt') { |f| f.puts find_all.join(',') }
  }
end