Class: YaLoremJa::Lorem
- Inherits:
-
Object
- Object
- YaLoremJa::Lorem
- Defined in:
- lib/ya_lorem_ja.rb
Overview
Japanese Lorem
Constant Summary collapse
- DEFAULT_RESOURCE_NAME =
:kazehakase
- DEFAULT_CHAR_COUNT_RANGE_IN_WORD =
2..6
- DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE =
6..15
- DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH =
2..5
Instance Attribute Summary collapse
-
#resource_name ⇒ Object
readonly
Returns the value of attribute resource_name.
Instance Method Summary collapse
-
#char_count_range ⇒ Range
Range of character count in a word.
- #date(fmt = '%Y年%m月%d日') ⇒ Object
-
#image(size, options = {}) ⇒ String
Get a placeholder image, using placehold.it by default.
-
#initialize(resource_name = DEFAULT_RESOURCE_NAME, char_count_range = DEFAULT_CHAR_COUNT_RANGE_IN_WORD, word_count_range = DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE, sentence_count_range = DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH) ⇒ Lorem
constructor
A new instance of Lorem.
-
#line_break ⇒ String
Line break character.
-
#line_break=(char) ⇒ Object
set line break character.
-
#paragraph(opts = { }) ⇒ String
return a random paragraph from word resource.
-
#paragraphs(total, opts = { }) ⇒ String
return random paragraphs from word resource.
- #reload_resource(resource_name = DEFAULT_RESOURCE_NAME, char_count_range = DEFAULT_CHAR_COUNT_RANGE_IN_WORD, word_count_range = DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE, sentence_count_range = DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH) ⇒ Object
-
#sentence ⇒ String
return a random sentence from word resource.
-
#sentence_count_range ⇒ Range
Range of sentence count in a paragraph.
-
#sentence_end_chars ⇒ Array
Return sentence end chars.
-
#sentences(total) ⇒ String
return rondom sentences from word resource.
-
#word ⇒ String
return a random word from word resource.
-
#word_count_range ⇒ Range
Range of word count in a sentence.
-
#words(total) ⇒ String
return rondom words from word resource.
Constructor Details
#initialize(resource_name = DEFAULT_RESOURCE_NAME, char_count_range = DEFAULT_CHAR_COUNT_RANGE_IN_WORD, word_count_range = DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE, sentence_count_range = DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH) ⇒ Lorem
Returns a new instance of Lorem.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ya_lorem_ja.rb', line 25 def initialize(resource_name=DEFAULT_RESOURCE_NAME, char_count_range=DEFAULT_CHAR_COUNT_RANGE_IN_WORD, word_count_range=DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE, sentence_count_range=DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH) @resource_name = resource_name # require begin require File.join('ya_lorem_ja/resources', resource_name.to_s) rescue LoadError end # 文章辞書の読み込み @resource = ::YaLoremJa::WordResources.load(resource_name, char_count_range, word_count_range, sentence_count_range) end |
Instance Attribute Details
#resource_name ⇒ Object (readonly)
Returns the value of attribute resource_name.
12 13 14 |
# File 'lib/ya_lorem_ja.rb', line 12 def resource_name @resource_name end |
Instance Method Details
#char_count_range ⇒ Range
Returns range of character count in a word.
44 45 46 |
# File 'lib/ya_lorem_ja.rb', line 44 def char_count_range return @resource.char_count_range_in_a_word end |
#date(fmt = '%Y年%m月%d日') ⇒ Object
130 131 132 133 134 135 |
# File 'lib/ya_lorem_ja.rb', line 130 def date(fmt='%Y年%m月%d日') y = rand(20) + 1990 m = rand(12) + 1 d = rand(31) + 1 Time.local(y, m, d).strftime(fmt) end |
#image(size, options = {}) ⇒ String
Get a placeholder image, using placehold.it by default
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ya_lorem_ja.rb', line 142 def image(size, ={}) domain = [:domain] || 'http://placehold.it' src = "#{domain}/#{size}" hex = %w(a b c d e f 0 1 2 3 4 5 6 7 8 9) background_color = [:background_color] color = [:color] if [:random_color] background_color = hex.shuffle[0...6].join color = hex.shuffle[0...6].join end src << "/#{background_color.sub(/^#/, '')}" if background_color src << '/ccc' if background_color.nil? && color src << "/#{color.sub(/^#/, '')}" if color src << "&text=#{Rack::Utils.escape([:text])}" if [:text] src end |
#line_break ⇒ String
Returns line break character.
75 76 77 |
# File 'lib/ya_lorem_ja.rb', line 75 def line_break @resource.line_break end |
#line_break=(char) ⇒ Object
set line break character
69 70 71 |
# File 'lib/ya_lorem_ja.rb', line 69 def line_break=(char) @resource.line_break = char end |
#paragraph(opts = { }) ⇒ String
return a random paragraph from word resource
115 116 117 |
# File 'lib/ya_lorem_ja.rb', line 115 def paragraph(opts={ }) @resource.paragraph(opts) end |
#paragraphs(total, opts = { }) ⇒ String
return random paragraphs from word resource
123 124 125 126 127 |
# File 'lib/ya_lorem_ja.rb', line 123 def paragraphs(total, opts={ }) default_opts = { start_sep: "", end_sep: line_break * 2 } merged_opts = default_opts.merge(opts) @resource.paragraphs(total, merged_opts) end |
#reload_resource(resource_name = DEFAULT_RESOURCE_NAME, char_count_range = DEFAULT_CHAR_COUNT_RANGE_IN_WORD, word_count_range = DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE, sentence_count_range = DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH) ⇒ Object
38 39 40 |
# File 'lib/ya_lorem_ja.rb', line 38 def reload_resource(resource_name=DEFAULT_RESOURCE_NAME, char_count_range=DEFAULT_CHAR_COUNT_RANGE_IN_WORD, word_count_range=DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE, sentence_count_range=DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH) initialize(resource_name, char_count_range, word_count_range, sentence_count_range) end |
#sentence ⇒ String
return a random sentence from word resource
99 100 101 |
# File 'lib/ya_lorem_ja.rb', line 99 def sentence @resource.sentence end |
#sentence_count_range ⇒ Range
Returns range of sentence count in a paragraph.
56 57 58 |
# File 'lib/ya_lorem_ja.rb', line 56 def sentence_count_range return @resource.sentence_count_range_in_a_paragraph end |
#sentence_end_chars ⇒ Array
Return sentence end chars
62 63 64 |
# File 'lib/ya_lorem_ja.rb', line 62 def sentence_end_chars return @resource.sentence_end_chars end |
#sentences(total) ⇒ String
return rondom sentences from word resource
108 109 110 |
# File 'lib/ya_lorem_ja.rb', line 108 def sentences(total) @resource.sentences(total) end |
#word ⇒ String
return a random word from word resource
82 83 84 85 |
# File 'lib/ya_lorem_ja.rb', line 82 def word # 1単語返却 @resource.word end |
#word_count_range ⇒ Range
Returns range of word count in a sentence.
50 51 52 |
# File 'lib/ya_lorem_ja.rb', line 50 def word_count_range return @resource.word_count_range_in_a_sentence end |
#words(total) ⇒ String
return rondom words from word resource
92 93 94 |
# File 'lib/ya_lorem_ja.rb', line 92 def words(total) @resource.words(total) end |