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 ⇒ String
return a random paragraph from word resource.
-
#paragraphs(total) ⇒ 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.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 58 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.
51 52 53 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 51 def char_count_range_in_a_word @char_count_range_in_a_word end |
#line_break ⇒ Object
Returns the value of attribute line_break.
52 53 54 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 52 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.
51 52 53 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 51 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.
51 52 53 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 51 def sentence_end_chars @sentence_end_chars end |
#sentence_map ⇒ Object (readonly)
Returns the value of attribute sentence_map.
51 52 53 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 51 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.
51 52 53 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 51 def word_count_range_in_a_sentence @word_count_range_in_a_sentence end |
Class Method Details
.register(resource_name) ⇒ Object
register word resource
154 155 156 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 154 def register(resource_name) ::YaLoremJa::WordResources.register(resource_name, self) end |
Instance Method Details
#paragraph ⇒ String
return a random paragraph from word resource
124 125 126 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 124 def paragraph paragraphs(1) end |
#paragraphs(total) ⇒ String
return random paragraphs from word resource
132 133 134 135 136 137 138 139 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 132 def paragraphs(total) list = [] total.times do sentence_count = rand(sentence_count_range_in_a_paragraph) list << self.sentences(sentence_count) end return list.join(@line_break * 2) end |
#sentence ⇒ String
return a random sentence from word resource
98 99 100 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 98 def sentence sentences(1) end |
#sentence_map_keys ⇒ Object
69 70 71 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 69 def sentence_map_keys @sentence_map_keys ||= sentence_map.keys.sort.reverse end |
#sentences(total) ⇒ String
return rondom sentences from word resource
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 107 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
76 77 78 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 76 def word words(1) end |
#words(num_of_word) ⇒ String
return rondom words from word resource
85 86 87 88 89 90 91 92 93 |
# File 'lib/ya_lorem_ja/word_resource.rb', line 85 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 |