Class: MyStorageGem::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/Storage.rb

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



7
8
9
10
# File 'lib/Storage.rb', line 7

def initialize
  @root = Node.new ''
  @last_node = @root
end

Instance Method Details

#add(word) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

#cleanObject



144
145
146
147
# File 'lib/Storage.rb', line 144

def clean
  @root = Node.new ''
  @last_node = @root
end

#contains?(word) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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_allObject



114
115
116
# File 'lib/Storage.rb', line 114

def find_all
  assemble_like_list([@root], [])[1..-1]
end

#load_from_file(file_path) ⇒ Object

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



119
120
121
# 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) ⇒ Object

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



124
125
126
127
128
129
130
# 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) ⇒ Object

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



133
134
135
# File 'lib/Storage.rb', line 133

def save_to_file(filename)
  File.write(filename, find_all.join(','))
end

#save_to_zip(filename) ⇒ Object

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



138
139
140
141
142
# 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