Module: Decidim::Faker::Localized

Defined in:
lib/decidim/faker/localized.rb

Overview

A Custom Faker wrapper so we can easily generate fake data for each locale in localized attributes.

Class Method Summary collapse

Class Method Details

.characterObject

Builds a Lorem Ipsum character.

Returns a Hash with a value for each locale.



58
59
60
61
62
# File 'lib/decidim/faker/localized.rb', line 58

def self.character
  localized do
    ::Faker::Lorem.character
  end
end

.characters(*args) ⇒ Object

Builds many Lorem Ipsum characters. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



67
68
69
70
71
# File 'lib/decidim/faker/localized.rb', line 67

def self.characters(*args)
  localized do
    ::Faker::Lorem.characters(*args)
  end
end

.companyObject

Fakes a company name.

Returns a Hash with a value for each locale.



22
23
24
25
26
# File 'lib/decidim/faker/localized.rb', line 22

def self.company
  localized do
    ::Faker::Company.name
  end
end

.literal(text) ⇒ Object

Sets the given text as the value for each locale.

text - The String text to set for each locale.

Returns a Hash with a value for each locale.



132
133
134
135
136
# File 'lib/decidim/faker/localized.rb', line 132

def self.literal(text)
  Decidim.available_locales.inject({}) do |result, locale|
    result.update(locale => text)
  end.with_indifferent_access
end

.localizedObject

Runs the given block for each of the available locales in Decidim, momentarilly setting the locale to the current one.

Returns a Hash with a value for each locale.



162
163
164
165
166
167
168
169
170
# File 'lib/decidim/faker/localized.rb', line 162

def self.localized
  Decidim.available_locales.inject({}) do |result, locale|
    text = ::Faker::Base.with_locale(locale) do
      yield
    end

    result.update(locale => text)
  end.with_indifferent_access
end

.nameObject

Fakes a person name.

Returns a Hash with a value for each locale.



31
32
33
34
35
# File 'lib/decidim/faker/localized.rb', line 31

def self.name
  localized do
    ::Faker::Name.name
  end
end

.paragraph(*args) ⇒ Object

Builds a paragraph with Lorem Ipsum words. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



94
95
96
97
98
# File 'lib/decidim/faker/localized.rb', line 94

def self.paragraph(*args)
  localized do
    ::Faker::Lorem.paragraph(*args)
  end
end

.paragraphs(*args) ⇒ Object

Builds many paragraphs with Lorem Ipsum words. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



103
104
105
106
107
# File 'lib/decidim/faker/localized.rb', line 103

def self.paragraphs(*args)
  localized do
    ::Faker::Lorem.paragraphs(*args)
  end
end

.prefixed(msg, locales = Decidim.available_locales) ⇒ Object

Prefixes the msg for each available locale and returns as a Hash of the form ‘locale => prefixed_msg`.

Return a Hash with a value for each locale.



176
177
178
179
180
# File 'lib/decidim/faker/localized.rb', line 176

def self.prefixed(msg, locales = Decidim.available_locales)
  locales.inject({}) do |result, locale|
    result.update(locale => "#{locale.to_s.upcase}: #{msg}")
  end.with_indifferent_access
end

.question(*args) ⇒ Object

Builds a question with Lorem Ipsum words. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



112
113
114
115
116
# File 'lib/decidim/faker/localized.rb', line 112

def self.question(*args)
  localized do
    ::Faker::Lorem.question(*args)
  end
end

.questions(*args) ⇒ Object

Builds many questions with Lorem Ipsum words. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



121
122
123
124
125
# File 'lib/decidim/faker/localized.rb', line 121

def self.questions(*args)
  localized do
    ::Faker::Lorem.questions(*args)
  end
end

.sentence(*args) ⇒ Object

Builds a sentence with Lorem Ipsum words. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



76
77
78
79
80
# File 'lib/decidim/faker/localized.rb', line 76

def self.sentence(*args)
  localized do
    ::Faker::Lorem.sentence(*args)
  end
end

.sentences(*args) ⇒ Object

Builds many sentences with Lorem Ipsum words. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



85
86
87
88
89
# File 'lib/decidim/faker/localized.rb', line 85

def self.sentences(*args)
  localized do
    ::Faker::Lorem.sentences(*args)
  end
end

.wordObject

Builds a Lorem Ipsum word.

Returns a Hash with a value for each locale.



40
41
42
43
44
# File 'lib/decidim/faker/localized.rb', line 40

def self.word
  localized do
    ::Faker::Lorem.word
  end
end

.words(*args) ⇒ Object

Builds many Lorem Ipsum words. See Faker::Lorem for options.

Returns a Hash with a value for each locale.



49
50
51
52
53
# File 'lib/decidim/faker/localized.rb', line 49

def self.words(*args)
  localized do
    ::Faker::Lorem.words(*args)
  end
end

.wrapped(before, after) ⇒ Object

Wrapps a text build by the block with some other text.

before - The String text to inject at the begining of each value. after - The String text to inject at the end of each value. block - A Block that generates a Hash with a text for each locale.

Example:

Decidim::Faker::Localized.wrapped("<p>", "</p>") do
  Decidim::Faker::Localized.sentence(5)
end

Returns a Hash with a value for each locale.



151
152
153
154
155
156
# File 'lib/decidim/faker/localized.rb', line 151

def self.wrapped(before, after)
  result = yield
  result.inject({}) do |wrapped, (locale, text)|
    wrapped.update(locale => [before, text, after].join)
  end
end