Class: YaLoremJa::WordResource
- Inherits:
-
Object
- Object
- YaLoremJa::WordResource
- Defined in:
- lib/ya_lorem_ja/word_resource.rb
Overview
base class of word resource
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_LINE_BREAK =
default line break character of sentence of paragraph
"\n"
Instance Attribute Summary collapse
-
#char_count_range_in_a_word ⇒ Object
readonly
Returns the value of attribute char_count_range_in_a_word.
-
#line_break ⇒ Object
Returns the value of attribute line_break.
-
#sentence_count_range_in_a_paragraph ⇒ Object
readonly
Returns the value of attribute sentence_count_range_in_a_paragraph.
-
#sentence_end_chars ⇒ Object
readonly
Returns the value of attribute sentence_end_chars.
-
#sentence_map ⇒ Object
readonly
Returns the value of attribute sentence_map.
-
#word_count_range_in_a_sentence ⇒ Object
readonly
Returns the value of attribute word_count_range_in_a_sentence.
Class Method Summary collapse
-
.register(resource_name) ⇒ Object
register word resource.
Instance Method Summary collapse
-
#initialize(char_count_range, word_count_range, sentence_count_range) ⇒ WordResource
constructor
A new instance of WordResource.
-
#paragraph(opts) ⇒ String
return a random paragraph from word resource.
-
#paragraphs(total, opts) ⇒ String
return random paragraphs from word resource.
-
#sentence ⇒ String
return a random sentence from word resource.
- #sentence_map_keys ⇒ Object
-
#sentences(total) ⇒ String
return rondom sentences from word resource.
-
#word ⇒ String
return a random word from word resource.
-
#words(num_of_word) ⇒ String
return rondom words from word resource.
Constructor Details
#initialize(char_count_range, word_count_range, sentence_count_range) ⇒ WordResource
Returns a new instance of WordResource.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 64 def initialize(char_count_range, word_count_range, sentence_count_range) # 単語の文字数範囲の設定 @char_count_range_in_a_word = char_count_range # 文の単語数範囲の設定 @word_count_range_in_a_sentence = word_count_range # 段落の文数範囲の設定 @sentence_count_range_in_a_paragraph = sentence_count_range # 改行文字 @line_break = DEFAULT_LINE_BREAK end |
Instance Attribute Details
#char_count_range_in_a_word ⇒ Object (readonly)
Returns the value of attribute char_count_range_in_a_word.
57 58 59 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 57 def char_count_range_in_a_word @char_count_range_in_a_word end |
#line_break ⇒ Object
Returns the value of attribute line_break.
58 59 60 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 58 def line_break @line_break end |
#sentence_count_range_in_a_paragraph ⇒ Object (readonly)
Returns the value of attribute sentence_count_range_in_a_paragraph.
57 58 59 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 57 def sentence_count_range_in_a_paragraph @sentence_count_range_in_a_paragraph end |
#sentence_end_chars ⇒ Object (readonly)
Returns the value of attribute sentence_end_chars.
57 58 59 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 57 def sentence_end_chars @sentence_end_chars end |
#sentence_map ⇒ Object (readonly)
Returns the value of attribute sentence_map.
57 58 59 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 57 def sentence_map @sentence_map end |
#word_count_range_in_a_sentence ⇒ Object (readonly)
Returns the value of attribute word_count_range_in_a_sentence.
57 58 59 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 57 def word_count_range_in_a_sentence @word_count_range_in_a_sentence end |
Class Method Details
.register(resource_name) ⇒ Object
register word resource
162 163 164 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 162 def register(resource_name) ::YaLoremJa::WordResources.register(resource_name, self) end |
Instance Method Details
#paragraph(opts) ⇒ String
return a random paragraph from word resource
130 131 132 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 130 def paragraph(opts) paragraphs(1, opts) end |
#paragraphs(total, opts) ⇒ String
return random paragraphs from word resource
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 138 def paragraphs(total, opts) list = [] total.times do sentence_count = rand(sentence_count_range_in_a_paragraph) start_sep = opts[:start_sep].to_s end_sep = opts[:end_sep].to_s list << start_sep + self.sentences(sentence_count) + end_sep end return list.join() end |
#sentence ⇒ String
return a random sentence from word resource
104 105 106 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 104 def sentence sentences(1) end |
#sentence_map_keys ⇒ Object
75 76 77 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 75 def sentence_map_keys @sentence_map_keys ||= sentence_map.keys.sort.reverse end |
#sentences(total) ⇒ String
return rondom sentences from word resource
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 113 def sentences(total) list = [] total.times do word_count = rand(word_count_range_in_a_sentence) sentence_len = word_count.times.inject(0){ |sum| sum + rand(char_count_range_in_a_word) } sentence_key = bsearch(sentence_map_keys, sentence_len) unless sentence_key sentence_key = sentence_map.keys.min end list << sentence_map[sentence_key].sample end return list.join(@line_break) end |
#word ⇒ String
return a random word from word resource
82 83 84 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 82 def word words(1) end |
#words(num_of_word) ⇒ String
return rondom words from word resource
91 92 93 94 95 96 97 98 99 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 91 def words(num_of_word) target_len = num_of_word.times.inject(0){ |sum| sum + rand(@char_count_range_in_a_word) } sentence_key = bsearch(sentence_map_keys, target_len) unless sentence_key sentence_key = sentence_map.keys.min end wk_word = sentence_map[sentence_key].sample return wk_word.sub(/[#{ sentence_end_chars.join() }]$/, "") end |