Class: Faker::Markdown

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/markdown.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.block_codeString

Produces a random code block formatted in Ruby.

Examples:

Faker::Markdown.block_code #=> "```ruby\nEos quasi qui.\n```"

Returns:



97
98
99
# File 'lib/faker/default/markdown.rb', line 97

def block_code
  "```ruby\n#{Lorem.sentence(word_count: 1)}\n```"
end

.emphasisString

Produces a random emphasis formatting on a random word in two sentences.

Examples:

Faker::Markdown.emphasis #=> "_Incidunt atque quis repellat id impedit.  Quas numquam quod incidunt dicta non. Blanditiis delectus laudantium atque reiciendis qui._"

Returns:



28
29
30
31
32
33
34
35
# File 'lib/faker/default/markdown.rb', line 28

def emphasis
  paragraph = Faker::Lorem.paragraph(sentence_count: 3)
  words = paragraph.split
  position = rand(0..words.length - 1)
  formatting = fetch('markdown.emphasis')
  words[position] = "#{formatting}#{words[position]}#{formatting}"
  words.join(' ')
end

.headersString

Produces a random header format.

Examples:

Faker::Markdown.headers #=> "##### Autem"

Returns:



15
16
17
# File 'lib/faker/default/markdown.rb', line 15

def headers
  "#{fetch('markdown.headers')} #{Lorem.word.capitalize}"
end

.inline_codeString

Produces a random inline code snippet between two sentences.

Examples:

Faker::Markdown.inline_code #=> "Aut eos quis suscipit. `Dignissimos voluptatem expedita qui.` Quo doloremque veritatis tempora aut."

Returns:



84
85
86
# File 'lib/faker/default/markdown.rb', line 84

def inline_code
  "`#{Faker::Lorem.sentence(word_count: 1)}`"
end

.ordered_listString

Produces a random ordered list of items between 1 and 10 randomly.

Examples:

Faker::Markdown.ordered_list #=> "1. Qui reiciendis non consequatur atque.\n2. Quo doloremque veritatis tempora aut.\n3. Aspernatur.\n4. Ea ab.\n5. Qui.\n6. Sit pariatur nemo eveniet.\n7. Molestiae aut.\n8. Nihil molestias iure placeat.\n9. Dolore autem quisquam."

Returns:



46
47
48
49
50
51
52
53
54
# File 'lib/faker/default/markdown.rb', line 46

def ordered_list
  number = rand(1..10)

  result = []
  number.times do |i|
    result << "#{i}. #{Faker::Lorem.sentence(word_count: 1)} \n"
  end
  result.join
end

.random(methods) ⇒ String+

Produces a random method from the methods above, excluding the methods listed in the arguments.

Examples:

Faker::Markdown.random #=> returns output from a single method outlined above
Faker::Markdown.random("table") #=> returns output from any single method outlined above except for "table"
Faker::Markdown.random("ordered_list", "unordered_list") #=> returns output from any single method outlined above except for either ordered_list and unordered_list

Parameters:

  • methods (Symbol)

    Specify which methods to exclude.

Returns:



133
134
135
136
137
# File 'lib/faker/default/markdown.rb', line 133

def random(*args)
  method_list = available_methods
  args&.each { |ex| method_list.delete_if { |meth| meth == ex.to_sym } }
  send(method_list[Faker::Config.random.rand(0..method_list.length - 1)])
end

.sandwich(sentences: 3, repeat: 1) ⇒ String

Produces a simulated blog-esque text-heavy block in markdown

Keyword arguments: sentences, repeat

Examples:

Faker::Markdown.sandwich #=> returns newline separated content of 1 header, 1 default lorem paragraph, and 1 random markdown element
Faker::Markdown.sandwich(sentences: 5) #=> returns newline separated content of 1 header, 1 5-sentence lorem paragraph, and 1 random markdown element
Faker::Markdown.sandwich(sentences: 6, repeat: 3) #=> returns newline separated content of 1 header, and then 3 sections consisting of, here, 1 6-sentence lorem paragraph and 1 random markdown element. The random markdown element is chosen at random in each iteration of the paragraph-markdown pairing.

Parameters:

  • sentences (Integer) (defaults to: 3)

    Specifies how many sentences make a text block.

  • repeat (Integer) (defaults to: 1)

    Specifies how many times the text block repeats.

Returns:



153
154
155
156
157
158
159
160
161
# File 'lib/faker/default/markdown.rb', line 153

def sandwich(sentences: 3, repeat: 1)
  text_block = []
  text_block << headers
  repeat.times do
    text_block << Faker::Lorem.paragraph(sentence_count: sentences)
    text_block << random
  end
  text_block.join("\n")
end

.tableString

Produces a random 3x4 table with a row of headings, a row of hyphens and two rows of data

Examples:

Faker::Markdown.table #=> "ad | similique | voluptatem\n---- | ---- | ----\ncorrupti | est | rerum\nmolestiae | quidem | et"

Returns:



110
111
112
113
114
115
116
117
# File 'lib/faker/default/markdown.rb', line 110

def table
  table = []
  3.times do
    table << "#{Lorem.word} | #{Lorem.word} | #{Lorem.word}"
  end
  table.insert(1, '---- | ---- | ----')
  table.join("\n")
end

.unordered_listString

Produces a random unordered list of items between 1 and 10 randomly.

Examples:

Faker::Markdown.unordered_list #=> "* Voluptatum aliquid tempora molestiae facilis non sed.\n* Nostrum omnis iste impedit voluptatum dolor.\n* Esse quidem et facere."

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/faker/default/markdown.rb', line 65

def unordered_list
  number = rand(1..10)

  result = []
  number.times do |_i|
    result << "* #{Faker::Lorem.sentence(word_count: 1)} \n"
  end
  result.join
end