markov_uuid 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/markov_uuid.rb CHANGED
@@ -2,5 +2,5 @@ require_relative './markov_uuid/chain'
2
2
  require_relative './markov_uuid/storage'
3
3
 
4
4
  module MarkovUuid
5
- VERSION = "0.0.5"
5
+ VERSION = "0.0.6"
6
6
  end
@@ -4,26 +4,24 @@ module MarkovUuid
4
4
  attr_reader :words
5
5
 
6
6
  class << self
7
- def from_file(f)
8
- new(strip_punctuation File.read(f))
7
+ def from_string string
8
+ from_word_list(strip_punctuation string)
9
9
  end
10
10
 
11
- def strip_punctuation l
12
- l.gsub(/[[:punct:]]/," ").gsub!(' ', ' ').split " "
11
+ def from_word_list words
12
+ new.tap { |i| i.add words }
13
13
  end
14
- end
15
14
 
16
- def initialize words
17
- @words = words
15
+ def strip_punctuation l
16
+ l.gsub(/[[:punct:]]/," ").gsub(' ', ' ').split " "
17
+ end
18
18
  end
19
19
 
20
- def new_key(key, word)
21
- return SEPARATOR if word == "\n"
22
-
23
- word or key
20
+ def initialize h={}
21
+ replace h
24
22
  end
25
23
 
26
- def add
24
+ def add words
27
25
  key = SEPARATOR
28
26
 
29
27
  words.each do |word|
@@ -33,7 +31,16 @@ module MarkovUuid
33
31
  end
34
32
  end
35
33
 
36
- def to_words length = 100
34
+ def new_key(key, word)
35
+ return SEPARATOR if word == "\n"
36
+
37
+ word or key
38
+ end
39
+
40
+ #note these are not by any means guaranteed to be unique
41
+ #dependent on uuid length and corpus size
42
+ #maybe maintain uniqueness elsewhere by regenerating unless unique
43
+ def uuid length = 100
37
44
  key = keys.sample
38
45
  word = ""
39
46
 
@@ -46,8 +53,11 @@ module MarkovUuid
46
53
  format result
47
54
  end
48
55
 
49
- def format words, i=32
50
- words.join("-")[0..i.to_i].gsub(/\A\w+-/,'').gsub(/-$/,"").gsub(/-\w+$/,"")
56
+ def format result, i=32
57
+ result.join("-")[0..i.to_i].
58
+ gsub(/\A\w+-/ , "").
59
+ gsub(/-$/ , "").
60
+ gsub(/-\w+$/ , "")
51
61
  end
52
62
  end
53
63
  end
@@ -2,21 +2,53 @@ require "yaml"
2
2
  require "fileutils"
3
3
  module MarkovUuid
4
4
  class Storage
5
- def initialize filename
6
- @filename = filename
5
+ attr_accessor :data, :cache_file, :input_file
6
+
7
+ #note these are not by any means guaranteed to be unique
8
+ #dependent on uuid length and corpus size
9
+ #maybe maintain uniqueness elsewhere by regenerating unless unique
10
+ def uuid
11
+ chain.uuid
12
+ end
13
+
14
+ def chain
15
+ @chain ||= MarkovUuid::Chain.new data
16
+ end
17
+
18
+ def initialize input_file, cache_file
19
+ @input_file, @cache_file = input_file, cache_file
20
+
21
+ preload_data
22
+ end
23
+
24
+ #for testing
25
+ def file_klass
26
+ File
7
27
  end
8
28
 
9
- attr_accessor :data, :filename
29
+ private
10
30
 
11
31
  def save
12
- File.open(filename, "w"){|f| YAML.dump(@data, f) }
32
+ file_klass.open(cache_file, "w"){|f| YAML.dump(@data, f) }
13
33
  end
14
34
 
15
- def load
16
- FileUtils.touch filename
17
- File.open(filename) do |f|
35
+ def open
36
+ file_klass.open(cache_file) do |f|
18
37
  @data = YAML.load f
19
38
  end
39
+
40
+ @data
41
+ end
42
+
43
+ def data
44
+ @data ||= MarkovUuid::Chain.from_string file_klass.read(input_file)
45
+ end
46
+
47
+ def preload_data
48
+ unless open
49
+ data
50
+ save
51
+ end
20
52
  end
21
53
  end
22
54
  end
data/spec/markov_spec.rb CHANGED
@@ -3,7 +3,7 @@ require File.expand_path('spec/spec_helper')
3
3
  require './lib/markov_uuid'
4
4
 
5
5
  describe MarkovUuid::Chain do
6
- let(:file) do
6
+ let(:string) do
7
7
  <<-CONTENTS.gsub(/^\s*/, "")
8
8
  CHAPTER I. Down the Rabbit-Hole
9
9
 
@@ -18,7 +18,8 @@ describe MarkovUuid::Chain do
18
18
 
19
19
  specify do
20
20
  s = "これは Some text with, random -?! Punctuation in"
21
- MarkovUuid::Chain.strip_punctuation(s).should == %w(これは Some text with random Punctuation in)
21
+ MarkovUuid::Chain.strip_punctuation(s).should ==
22
+ %w(これは Some text with random Punctuation in)
22
23
  end
23
24
 
24
25
  let(:words) do
@@ -33,20 +34,19 @@ describe MarkovUuid::Chain do
33
34
  end
34
35
 
35
36
  specify do
36
- MarkovUuid::Chain.strip_punctuation(file).should == words
37
+ MarkovUuid::Chain.strip_punctuation(string).should == words
37
38
  end
38
39
 
39
40
  context 'from file' do
40
- before do
41
- File.stub(:read).and_return file
42
- end
43
-
44
41
  specify do
45
- chain = MarkovUuid::Chain.from_file ''
46
- chain.to_words.split("-").each do |word|
42
+ chain = MarkovUuid::Chain.from_string string
43
+ #not sure how to test the randomness right now
44
+ #so we can just check it's generating a list of words joined by '-'
45
+ chain.uuid.should =~ /\A(\w+\-)+\w+\z/
46
+
47
+ chain.uuid.split("-").each do |word|
47
48
  words.should include word
48
49
  end
49
-
50
50
  end
51
51
  end
52
52
  end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe MarkovUuid::Storage do
4
+ let(:file_klass) { mock 'File Class mock', read: file_contents }
5
+
6
+ let(:file_contents) { "Down the rabbit hole" }
7
+
8
+ before do
9
+ MarkovUuid::Storage.any_instance.stub(:file_klass).and_return file_klass
10
+ end
11
+
12
+ context "empty cache file" do
13
+ before do
14
+ file_klass.stub(:open).and_yield ''
15
+ YAML.stub(:dump).and_return true
16
+ end
17
+
18
+ specify do
19
+ storage = MarkovUuid::Storage.new 'input_filename.txt', 'cache_filename.txt'
20
+
21
+ storage.chain.should == {
22
+ MarkovUuid::Chain::SEPARATOR => ["Down"],
23
+ "Down" => ["the"],
24
+ "rabbit" => ["hole"],
25
+ "the" => ["rabbit"]
26
+ }
27
+ end
28
+
29
+ end
30
+
31
+ context "cached file" do
32
+ before do
33
+ yaml = <<-YAML.gsub(/^\s{6}/, "")
34
+ ---
35
+ a:
36
+ - b
37
+ - c
38
+ YAML
39
+
40
+ file_klass.stub(:open).and_yield yaml
41
+ end
42
+
43
+ specify do
44
+ storage = MarkovUuid::Storage.new 'input_filename', 'cache_filename'
45
+ storage.chain.should == {"a" => ["b", "c"]}
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markov_uuid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-02 00:00:00.000000000 Z
12
+ date: 2012-10-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -69,6 +69,7 @@ files:
69
69
  - markov_uuid.gemspec
70
70
  - spec/markov_spec.rb
71
71
  - spec/spec_helper.rb
72
+ - spec/storage_spec.rb
72
73
  - storage.rb
73
74
  homepage: http://github.com/markburns/markov_uuid
74
75
  licenses: []
@@ -98,4 +99,5 @@ summary: Easily generate random English-like UUIDs (or any other language) that
98
99
  test_files:
99
100
  - spec/markov_spec.rb
100
101
  - spec/spec_helper.rb
102
+ - spec/storage_spec.rb
101
103
  has_rdoc: