Sha256: 196afb07d0929954509a603a61aec4a6204a0dbc6f0bce5f4ea8a40a9e7b3360

Contents?: true

Size: 760 Bytes

Versions: 1

Compression:

Stored size: 760 Bytes

Contents

require "yaml"
require "fileutils"
module MarkovUuid
  class Storage
    include KeySelector
    attr_accessor :data

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

    def add words
      words.add_to @data
    end

    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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
markov_uuid-0.0.3 lib/markov_uuid/storage.rb