Sha256: fcecff85d56fc6a9955270a9dd834bb58b4d810faa4bcedd381bb5606a360b22

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.2 lib/markov_uuid/storage.rb