Class: Dictation::Serializer

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

Class Method Summary collapse

Class Method Details

.deserialize(file) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/dictation/serializer.rb', line 13

def deserialize(file)
  words = []
  File.open(file).each do |line|
    words.push(JSON.parse(line))
  end
  words
end

.deserialize_a_portion_by_given_line(file, start, stop = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dictation/serializer.rb', line 31

def deserialize_a_portion_by_given_line(file, start, stop = nil)
  all_words = deserialize(file)
  unless stop.nil?
    if stop >= start && stop <= all_words.size
      return all_words[start-1..stop-1]
    end
  else
    if start <= all_words.size
      return all_words[start-1..-1]
    end
  end
end

.deserialize_a_portion_by_given_word(file, start, stop = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/dictation/serializer.rb', line 21

def deserialize_a_portion_by_given_word(file, start, stop = nil)
  all_words = deserialize(file)
  idx_start = all_words.index { |x| x.value == start }
  idx_stop = ( stop.nil? ? nil : all_words.index { |x| x.value == stop } )
  idx_stop = all_words.size if idx_stop.nil?
  if !idx_start.nil? && idx_start <= idx_stop
    return all_words[idx_start..idx_stop]
  end
end

.serialize(data, file) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/dictation/serializer.rb', line 4

def serialize(data, file)
  if Array === data
    data = data.inject('') do |chunk, word|
      chunk += word.to_json + "\n"
    end
  end
  File.open(file, 'a') { |f| f.write(data) }
end