crazipsum 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crazipsum
4
+ # Dictionnary is a simple struct with a #words and #fillers read-only
5
+ # attributes.
6
+ # It is used to initialize the Generator instance.
7
+ class Dictionnary
8
+ attr_reader :words, :fillers
9
+
10
+ def initialize(words, fillers: [])
11
+ @words = words
12
+ @fillers = fillers
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crazipsum
4
+ # Generator generates lorem ipsum sentences, paragraphs and text based on a
5
+ # Dictionnary.
6
+ class Generator
7
+ DEFAULT_WORD_COUNT_RANGE = (7..15).freeze
8
+ DEFAULT_SENTENCE_COUNT_RANGE = (4..7).freeze
9
+ DEFAULT_PARAGRAPH_COUNT_RANGE = (3..5).freeze
10
+ DEFAULT_PARAGRAPH_SEPARATOR = "\n\n"
11
+
12
+ # Returns a new instance of Generator that will used words and fillers from
13
+ # the given Dictionnary to generate the lorem ipsum texts.
14
+ def initialize(dictionnary)
15
+ @dictionnary = dictionnary
16
+ end
17
+
18
+ # Generates a lorem ipsum sentence based on the generator's dictionnary.
19
+ #
20
+ # @param [integer] word_count the number of words expected in the sentence.
21
+ # @param [Array<String>] fillers a list of words used to fill in the lorem ipsum.
22
+ # @return [String] a lorem ipsum sentence.
23
+ def sentence(word_count: rand(DEFAULT_WORD_COUNT_RANGE), fillers: dictionnary.fillers)
24
+ word_count = 0 if word_count.negative?
25
+ dictionnary_words = dictionnary.words
26
+ words = (0...word_count).map do
27
+ next dictionnary_words.sample if fillers.nil? || fillers == false || fillers.empty?
28
+
29
+ rand(3).zero? ? dictionnary_words.sample : fillers.sample
30
+ end
31
+ return '' if words.empty?
32
+
33
+ words[0] = words[0].capitalize
34
+ words = words.join(' ')
35
+ "#{words}."
36
+ end
37
+
38
+ # Generates a lorem ipsum paragraph based on the generator's dictionnary.
39
+ #
40
+ # @param [integer] sentence_count the number of sentences expected in the paragraph.
41
+ # @param [integer] word_count the number of words expected in each sentence.
42
+ # @param [Array<String>] fillers a list of words used to fill in the lorem ipsum.
43
+ # @return [String] a lorem ipsum paragraph.
44
+ def paragraph(
45
+ word_count: rand(DEFAULT_WORD_COUNT_RANGE),
46
+ sentence_count: rand(DEFAULT_SENTENCE_COUNT_RANGE),
47
+ fillers: dictionnary.fillers
48
+ )
49
+ sentence_count = 0 if sentence_count.negative?
50
+ paragraph = []
51
+ sentence_count.times do
52
+ s = sentence(word_count: word_count, fillers: fillers)
53
+ paragraph << s unless s.empty?
54
+ end
55
+ paragraph.join(' ')
56
+ end
57
+ alias sentences paragraph
58
+
59
+ # Generates a lorem ipsum text with multiples paragraphs based on the
60
+ # generator's dictionnary.
61
+ #
62
+ # @param [integer] sentence_count the number of sentences expected in the paragraph.
63
+ # @param [integer] sentence_count the number of sentences expected in the paragraphs.
64
+ # @param [integer] word_count the number of words expected in each sentence.
65
+ # @param [Array<String>] fillers a list of words used to fill in the lorem ipsum.
66
+ # @return [String] a lorem ipsum text.
67
+ def paragraphs(
68
+ word_count: rand(DEFAULT_WORD_COUNT_RANGE),
69
+ sentence_count: rand(DEFAULT_SENTENCE_COUNT_RANGE),
70
+ paragraph_count: rand(DEFAULT_PARAGRAPH_COUNT_RANGE),
71
+ fillers: dictionnary.fillers,
72
+ seperator: DEFAULT_PARAGRAPH_SEPARATOR
73
+ )
74
+ paragraph_count = 0 if paragraph_count.negative?
75
+ paragraphs = []
76
+ paragraph_count.times do
77
+ p = paragraph(word_count: word_count, sentence_count: sentence_count, fillers: fillers)
78
+ paragraphs << p unless p.empty?
79
+ end
80
+ paragraphs.join(seperator)
81
+ end
82
+ alias text paragraphs
83
+
84
+ private
85
+
86
+ attr_reader :dictionnary
87
+ end
88
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ require 'crazipsum/dictionnary'
6
+ require 'crazipsum/generator'
7
+
8
+ module Crazipsum
9
+ # Registry is a singleton class that registers and stores different
10
+ # dictionnary types.
11
+ class Registry
12
+ include Singleton
13
+
14
+ def initialize
15
+ @registry = {}
16
+ end
17
+
18
+ # Registers a new type of Dictionnary that can later be retrieved with #[].
19
+ #
20
+ # @param [String,Symbol] type a name for that dictionnary. This type will be
21
+ # used later when calling `Crazipsum(type)`.
22
+ # @param [Array<String>] words a list of words used when generating the lorem ipsum.
23
+ # @param [Array<String>, false] fillers a list of words used to fill in the sentences when generating the lorem ipsum.
24
+ def register(type, words, fillers: [])
25
+ registry[type.to_s] = Dictionnary.new(words, fillers: fillers)
26
+ end
27
+
28
+ # Retrieves a dictionnary given its type.
29
+ #
30
+ # @param [String,Symbol] type the name of the dictionnary to retrieve
31
+ # @return [Dictionnary,nil] the dictionnary registered for this name. nil if no dictionnary was registered for this type.
32
+ def [](type)
33
+ registry[type.to_s]
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :registry
39
+ end
40
+ end
@@ -1,3 +1,5 @@
1
- class Crazipsum
2
- VERSION = '0.1.2'.freeze
1
+ # frozen_string_literal: true
2
+
3
+ module Crazipsum
4
+ VERSION = '0.2.0'
3
5
  end
metadata CHANGED
@@ -1,85 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crazipsum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yohan Robert
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-23 00:00:00.000000000 Z
11
+ date: 2019-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: ataru
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.12'
19
+ version: 0.2.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.12'
26
+ version: 0.2.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: bundler
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '10.0'
47
+ version: '0.8'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '10.0'
54
+ version: '0.8'
41
55
  - !ruby/object:Gem::Dependency
42
- name: rubocop
56
+ name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - '='
59
+ - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: 0.40.0
61
+ version: '10.0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - '='
66
+ - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: 0.40.0
68
+ version: '10.0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: pry-byebug
70
+ name: rubocop
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - '='
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: 3.4.0
75
+ version: '0.49'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - '='
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: 3.4.0
82
+ version: '0.49'
69
83
  - !ruby/object:Gem::Dependency
70
- name: coveralls
84
+ name: yard
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '0.8'
89
+ version: 0.9.20
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: '0.8'
96
+ version: 0.9.20
83
97
  description: Ever wanted some dumber, crazier (fancier?) lorem ipsum? Here you go!
84
98
  email:
85
99
  - yohan.robert@outlook.fr
@@ -96,9 +110,14 @@ files:
96
110
  - LICENSE.md
97
111
  - README.md
98
112
  - Rakefile
113
+ - ataru_setup.rb
99
114
  - bin/console
100
115
  - bin/setup
101
116
  - crazipsum.gemspec
117
+ - data/animal.txt
118
+ - data/car_make.txt
119
+ - data/constellation.txt
120
+ - data/country.txt
102
121
  - data/fillers.txt
103
122
  - data/fruit.txt
104
123
  - data/mineral.txt
@@ -106,11 +125,15 @@ files:
106
125
  - data/programming_language.txt
107
126
  - data/religion.txt
108
127
  - lib/crazipsum.rb
128
+ - lib/crazipsum/dictionnary.rb
129
+ - lib/crazipsum/generator.rb
130
+ - lib/crazipsum/registry.rb
109
131
  - lib/crazipsum/version.rb
110
132
  homepage: https://www.github.com/groyoh/crazipsum
111
133
  licenses:
112
134
  - MIT
113
- metadata: {}
135
+ metadata:
136
+ yard.run: yri
114
137
  post_install_message:
115
138
  rdoc_options: []
116
139
  require_paths:
@@ -127,9 +150,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
150
  version: '0'
128
151
  requirements: []
129
152
  rubyforge_project:
130
- rubygems_version: 2.5.1
153
+ rubygems_version: 2.5.2.3
131
154
  signing_key:
132
155
  specification_version: 4
133
156
  summary: Lorem ipsum remixed
134
157
  test_files: []
135
- has_rdoc: