Class: MarkovUuid::Storage

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

Constant Summary

Constants included from KeySelector

KeySelector::SEPARATOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from KeySelector

#new_key

Constructor Details

#initialize(data = nil) ⇒ Storage

Returns a new instance of Storage.



8
9
10
11
# File 'lib/markov_uuid/storage.rb', line 8

def initialize(data = nil )
  @data = data if data.class == Hash
  @data ||= Hash.new
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Instance Method Details

#add(words) ⇒ Object



13
14
15
# File 'lib/markov_uuid/storage.rb', line 13

def add words
  words.add_to @data
end

#load(filename) ⇒ Object



34
35
36
37
38
39
# File 'lib/markov_uuid/storage.rb', line 34

def load filename
  FileUtils.touch filename
  File.open(filename) do |f|
    @data = YAML.load f
  end
end

#save(filename) ⇒ Object



30
31
32
# File 'lib/markov_uuid/storage.rb', line 30

def save filename
  File.open(filename, "w"){|f| YAML.dump(@data, f) }
end

#to_words(length = 100) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/markov_uuid/storage.rb', line 17

def to_words length = 100
  key = Markov::SEPARATOR
  word = ""

  result = length.times.map do
    word = @data[key].sample rescue nil
    key = new_key key, word
    word
  end.compact

  Markov.new result
end