Class: YaLoremJa::Lorem

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • resource_name (Symbol) (defaults to: DEFAULT_RESOURCE_NAME)

    name of word resource

  • char_count_range (Range) (defaults to: DEFAULT_CHAR_COUNT_RANGE_IN_WORD)

    range of character count in a word

  • word_count_range (Range) (defaults to: DEFAULT_WORD_COUNT_RANGE_IN_SENTENCE)

    range of word count in sentence

  • sentence_count_range (Range) (defaults to: DEFAULT_SENTENCE_COUNT_RANGE_IN_PARAGRAPH)

    renage of sentence count in paragraph



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_nameObject (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_rangeRange

Returns range of character count in a word.

Returns:

  • (Range)

    range of character count in a word



40
41
42
# File 'lib/ya_lorem_ja.rb', line 40

def char_count_range
  return @resource.char_count_range_in_a_word
end

#date(fmt = '%Y年%m月%d日') ⇒ Object



126
127
128
129
130
131
# File 'lib/ya_lorem_ja.rb', line 126

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

Parameters:

  • size (String)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ya_lorem_ja.rb', line 138

def image(size, options={})
  domain           = options[: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 = options[:background_color]
  color            = options[:color]

  if options[: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(options[:text])}" if options[:text]

  src
end

#line_breakString

Returns line break character.

Returns:

  • (String)

    line break character



71
72
73
# File 'lib/ya_lorem_ja.rb', line 71

def line_break
  @resource.line_break
end

#line_break=(char) ⇒ Object

set line break character

Parameters:

  • char (String)

    line break character



65
66
67
# File 'lib/ya_lorem_ja.rb', line 65

def line_break=(char)
  @resource.line_break = char
end

#paragraph(opts = { }) ⇒ String

return a random paragraph from word resource

Returns:

  • (String)

    sentence



111
112
113
# File 'lib/ya_lorem_ja.rb', line 111

def paragraph(opts={  })
  @resource.paragraph(opts)
end

#paragraphs(total, opts = { }) ⇒ String

return random paragraphs from word resource

Parameters:

  • total (Fixnum)

    count of paragraph

Returns:

  • (String)

    paragraph



119
120
121
122
123
# File 'lib/ya_lorem_ja.rb', line 119

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

#sentenceString

return a random sentence from word resource

Returns:

  • (String)

    sentence



95
96
97
# File 'lib/ya_lorem_ja.rb', line 95

def sentence
  @resource.sentence
end

#sentence_count_rangeRange

Returns range of sentence count in a paragraph.

Returns:

  • (Range)

    range of sentence count in a paragraph



52
53
54
# File 'lib/ya_lorem_ja.rb', line 52

def sentence_count_range
  return @resource.sentence_count_range_in_a_paragraph
end

#sentence_end_charsArray

Return sentence end chars

Returns:

  • (Array)

    return sentence end chars



58
59
60
# File 'lib/ya_lorem_ja.rb', line 58

def sentence_end_chars
  return @resource.sentence_end_chars
end

#sentences(total) ⇒ String

return rondom sentences from word resource

Parameters:

  • total (Fixnum)

    count of sentence

Returns:

  • (String)

    words



104
105
106
# File 'lib/ya_lorem_ja.rb', line 104

def sentences(total)
  @resource.sentences(total)
end

#wordString

return a random word from word resource

Returns:

  • (String)

    word



78
79
80
81
# File 'lib/ya_lorem_ja.rb', line 78

def word
  # 1単語返却
  @resource.word
end

#word_count_rangeRange

Returns range of word count in a sentence.

Returns:

  • (Range)

    range of word count in a sentence



46
47
48
# File 'lib/ya_lorem_ja.rb', line 46

def word_count_range
  return @resource.word_count_range_in_a_sentence
end

#words(total) ⇒ String

return rondom words from word resource

Parameters:

  • total (Fixnum)

    count of word

Returns:

  • (String)

    words



88
89
90
# File 'lib/ya_lorem_ja.rb', line 88

def words(total)
  @resource.words(total)
end