well_read_faker 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b242404457d34cf93e9e50e25854c0ee41129492
4
- data.tar.gz: a97599aa21ff730ec81c6d9fae29641925878d43
3
+ metadata.gz: 72f76968ddb4ab56cddb5fa3ddaf0ef3c296e5eb
4
+ data.tar.gz: 05a46f05bae05e1ff7baf49820b44cb1df6a7eae
5
5
  SHA512:
6
- metadata.gz: 3bc9419c52518e87897a9f644205de5fca4846d30a94a5eccbf341678ac742c09b0a91809d41ad1af16c0aa09c2ec62f39731d44bf023c99efb47bd0bd477e17
7
- data.tar.gz: 47daa33cc2b6b65ceb49016f644ecac7339c550a60c17d8528ce6710a8ff11ef36a06779648118236068f1ce6676b2edf97ee7d76fce98cd19176b72d52b610e
6
+ metadata.gz: 54bb4b982dd7a15bdc1c986a0b1713859f6e09503bf9ef867974db2f01d6eaf42d49884c6ca23d96a9deed880de14f8f28125fd9a93a0c6c8a8f2ce6126dc271
7
+ data.tar.gz: 535ef04cb097a9fe9311b0d0d65cd9099240a2456386a89a4febbdea5a7988b98e22472fdc5eb2796b71c28f230a5eb23b0cc17f1b9fb144d20636c8d1ffd597
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ /.ruby-gemset
2
+ /.ruby-version
1
3
  /.bundle/
2
4
  /.yardoc
3
5
  /Gemfile.lock
@@ -7,6 +9,7 @@
7
9
  /pkg/
8
10
  /spec/reports/
9
11
  /tmp/
12
+ *.gem
10
13
 
11
14
  # rspec failure tracking
12
15
  .rspec_status
@@ -2,7 +2,7 @@ sudo: false
2
2
  language: ruby
3
3
  before_install: gem install bundler -v 1.14.6
4
4
  rvm:
5
- - "2.4.0"
5
+ - "2.4.1"
6
6
  - "2.3.4"
7
7
  - "2.2.7"
8
8
  - "2.1.10"
@@ -23,7 +23,7 @@ following piece of code:
23
23
  puts WellReadFaker.paragraph
24
24
  --------------------------------------------------------------------------------
25
25
 
26
- Could print:
26
+ Could print (without newlines):
27
27
 
28
28
  --------------------------------------------------------------------------------
29
29
  Thus they prayed, but not as yet would Jove grant them their prayer.
@@ -33,6 +33,43 @@ dare not with my own eyes witness this fight between my son and
33
33
  Menelaus, for Jove and the other immortals alone know which shall fall."
34
34
  --------------------------------------------------------------------------------
35
35
 
36
+ == Custom sources
37
+
38
+ Well Read Faker comes with "Iliad" bundled as a default source. However,
39
+ custom sources can be used:
40
+
41
+ [source,ruby]
42
+ --------------------------------------------------------------------------------
43
+ WellReadFaker.add_source :book_in_my_language, "path/to/book", {}
44
+ WellReadFaker[:book_in_my_language].paragraph
45
+ WellReadFaker.default_source = :book_in_my_language
46
+ WellReadFaker.paragraph
47
+ WellReadFaker[:iliad].paragraph # Iliad can be still accessed
48
+ --------------------------------------------------------------------------------
49
+
50
+ The second argument passed to +WellReadFaker::add_source+ can be a string
51
+ containing a file path, or any object which responds to +#read+, like +File+
52
+ instance.
53
+
54
+ The third argument is optional but can be used to supply additional options,
55
+ for example to skip book descriptor or legal notes. See
56
+ +lib/well_read_faker/load_bundled_sources.rb+ for examples.
57
+
58
+ In the source text, paragraphs must be separated with blank lines.
59
+
60
+ == Note about randomness quality
61
+
62
+ For given text source, the paragraphs are returned in a random order, but will
63
+ not be repeated (in the sense of +String#==+ equality) until all unique
64
+ paragraphs from that text are returned exactly once. After that, they are
65
+ returned once again in the same order.
66
+
67
+ Therefore it is guaranteed that if given source text contains +n+ unique
68
+ paragraphs, then +n+ subsequent calls of +#paragraph+ method will return
69
+ unique values.
70
+
71
+ For your information, The Iliad contains over 1000 unique paragraphs.
72
+
36
73
  == License
37
74
 
38
75
  The gem is available as open source under the terms of the
@@ -3,6 +3,7 @@ WellReadFaker.add_source(
3
3
  File.expand_path("../../books/homer_butler_iliad.txt", __FILE__),
4
4
  begin: /The quarrel between Agamemnon/,
5
5
  end: /End of the Project Gutenberg EBook of The Iliad, by Homer/,
6
+ min_words: 5,
6
7
  )
7
8
 
8
9
  WellReadFaker.default_source = :iliad
@@ -4,45 +4,83 @@ module WellReadFaker
4
4
  class Source
5
5
  include Mutex_m
6
6
 
7
- attr_reader :path_to_book, :options
7
+ attr_reader :path_to_book_or_io, :options
8
8
 
9
- def initialize path_to_book, options = {}
9
+ def initialize path_to_book_or_io, options = {}
10
10
  super()
11
- @path_to_book = path_to_book
11
+ @path_to_book_or_io = path_to_book_or_io
12
12
  @options = options
13
13
  end
14
14
 
15
- def text
16
- mu_synchronize{ @text ||= load }
15
+ def paragraph
16
+ ensure_loaded
17
+ @paragraphs_arr[inc_paragraphs]
17
18
  end
18
19
 
19
- def paragraph
20
- text.sample
20
+ def ensure_loaded
21
+ @loaded or mu_synchronize{ @loaded or load }
21
22
  end
22
23
 
23
24
  private
24
25
 
25
26
  def load
26
- raw = File.read path_to_book
27
- @text = process_raw_text raw
27
+ if path_to_book_or_io.respond_to? :read
28
+ raw = path_to_book_or_io.read
29
+ else
30
+ raw = File.read path_to_book_or_io
31
+ end
32
+
33
+ process_raw_text raw
34
+ @loaded = true
28
35
  end
29
36
 
30
37
  def process_raw_text raw
31
- paragraphs = raw.strip.split(/\n\s*\n/)
32
- paragraphs.map!{ |m| m.gsub /\s*\n\s*/, " " }
33
- while (options.key? :begin) && (paragraphs[0] !~ options[:begin])
34
- paragraphs.shift or raise(
38
+ trimmed = trim_text_by_regexps raw, options[:begin], options[:end]
39
+
40
+ @paragraphs_arr = trimmed.split(/\n\s*\n/)
41
+ @paragraphs_arr.map!{ |m| m.gsub /\s*\n\s*/, " " }
42
+ @paragraphs_arr.uniq!
43
+ if options[:min_words]
44
+ @paragraphs_arr.select!{ |m| /(\w+\b\W*){#{options[:min_words]}}/ =~ m }
45
+ end
46
+
47
+ shuffle_array @paragraphs_arr
48
+ @paragraphs_idx = -1
49
+ end
50
+
51
+ def trim_text_by_regexps source_text, begin_rx, end_rx
52
+ retval = source_text.dup
53
+
54
+ if begin_rx
55
+ match_data = begin_rx.match(retval) or raise(
35
56
  ArgumentError,
36
- "Regular expression #{options[:begin].inspect} not found in text."
57
+ "Regular expression #{begin_rx.inspect} not found in text."
37
58
  )
59
+ retval[0..match_data.begin(0)-1] = ""
38
60
  end
39
- while (options.key? :end) && (paragraphs.pop !~ options[:end])
40
- paragraphs.empty? and raise(
61
+
62
+ if end_rx
63
+ match_data = end_rx.match(retval) or raise(
41
64
  ArgumentError,
42
- "Regular expression #{options[:end].inspect} not found in text."
65
+ "Regular expression #{end_rx.inspect} not found in text."
43
66
  )
67
+ retval[match_data.begin(0)..-1] = ""
68
+ end
69
+
70
+ retval.strip!
71
+ retval
72
+ end
73
+
74
+ def inc_paragraphs
75
+ mu_synchronize do
76
+ return @paragraphs_idx = (@paragraphs_idx + 1) % @paragraphs_arr.size
44
77
  end
45
- paragraphs
78
+ end
79
+
80
+ # Extracted to a separate method to make stubbing easy (sometimes we want
81
+ # to preserve natural order in specs)
82
+ def shuffle_array array
83
+ array.sort_by!{ rand }
46
84
  end
47
85
 
48
86
  end
@@ -1,3 +1,3 @@
1
1
  module WellReadFaker
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "pry"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
25
26
  spec.add_development_dependency "rspec", "~> 3.5"
26
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: well_read_faker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Skałacki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-01 00:00:00.000000000 Z
11
+ date: 2017-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
111
  version: '0'
98
112
  requirements: []
99
113
  rubyforge_project:
100
- rubygems_version: 2.6.8
114
+ rubygems_version: 2.6.11
101
115
  signing_key:
102
116
  specification_version: 4
103
117
  summary: Returns random fragments of Iliad or other book.