Class: Storage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



5
6
7
8
# File 'lib/storage.rb', line 5

def initialize
  @storage_array = {}
  @search_words = []
end

Instance Attribute Details

#search_wordsObject

Returns the value of attribute search_words.



3
4
5
# File 'lib/storage.rb', line 3

def search_words
  @search_words
end

#storage_arrayObject

Returns the value of attribute storage_array.



3
4
5
# File 'lib/storage.rb', line 3

def storage_array
  @storage_array
end

Instance Method Details

#add(string) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/storage.rb', line 57

def add(string)
  if contains(string)
    p  "'#{string}' is already in the vocabulary"
    return
  end
  prev_node = @storage_array
  i = 0
  string_size = string.size
  string.each_char do |c|
    current_node = prev_node
    if  !current_node.has_key?(c)
      current_node[c] = {}
    end
    prev_node = current_node[c]
    i+=1
    if string_size == i
      current_node[c]['value'] = string
      p "New word '#{string}' added"
    end
  end
end

#contains(string) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/storage.rb', line 79

def contains(string)
  prev_node = @storage_array
  i = 0
  string_size = string.size
  string.each_char do |c|
    current_node = prev_node
    if  !current_node.has_key?(c)
      return false
    end
    prev_node = current_node[c]
    i+=1
    if string_size == i
      if current_node[c]['value'] == string
        return true
      end
    end
  end
end

#find(string) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/storage.rb', line 98

def find(string)
  string_size = string.size
  if string_size > 2
    prev_node = @storage_array
    i = 0
    string.each_char do |c|
      current_node = prev_node
      if  !current_node.has_key?(c)
        return false
      end
      prev_node = current_node[c]
      i+=1
      if string_size == i
        return output_words(current_node[c])
      end
    end
  end
end

#load_from_file(filename) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/storage.rb', line 17

def load_from_file(filename)
  file_str = ''
  File.open(filename, 'r') {|f|
    file_str = f.read
  }
  load_string(file_str)
end

#load_from_zip(zip_name) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/storage.rb', line 48

def load_from_zip(zip_name)
  Zip::ZipFile.open(zip_name) do |zipfile|
    zipfile.each do |entry|
      file_string = entry.get_input_stream.read
      load_string(file_string)
    end
  end
end

#load_string(init_string) ⇒ Object



10
11
12
13
14
15
# File 'lib/storage.rb', line 10

def load_string(init_string)
  input_array = init_string.split(',')
  input_array.each do |store|
    add(store)
  end
end

#output_words(node) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/storage.rb', line 117

def output_words(node)
  node.keys.each do |child|
    if child == 'value'
      @search_words << node[child]
    else
      output_words(node[child])
    end
  end
  return @search_words
end

#save_to_file(filename) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/storage.rb', line 25

def save_to_file(filename)
  input_array = output_words(@storage_array)
  input_string = ''
  input_array.each do |word|
    input_string += "#{word},"
  end
  File.open(filename, 'w') {|f|
    f.print input_string
  }
end

#save_to_zip(zip_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/storage.rb', line 36

def save_to_zip(zip_name)
  input_array = output_words(@storage_array)
  input_string = ''
  input_array.each do |word|
    input_string += "#{word},"
  end
  Zip::ZipFile.open(zip_name, Zip::ZipFile::CREATE) {
      |zipfile|
    zipfile.get_output_stream("storage") { |f| f.print input_string }
  }
end