Class: Faker::Commerce

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/commerce.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, translate, unique, with_locale

Class Method Details

.brandstring

Produces a randomized string of a brand name

Examples:

Faker::Commerce.brand #=> 'Apple'

Returns:

  • (string)

Available since:

  • next



125
126
127
# File 'lib/faker/default/commerce.rb', line 125

def brand
  fetch('commerce.brand')
end

.colorString

Produces a random color.

Examples:

Faker::Commerce.color #=> "lavender"

Returns:

  • (String)

Available since:

  • 1.2.0



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

def color
  fetch('color.name')
end

.department(max: 3, fixed_amount: false) ⇒ String

Produces a random department.

Examples:

Faker::Commerce.department #=> "Grocery, Health & Beauty"
Faker::Commerce.department(max: 5) #=> "Grocery, Books, Health & Beauty"
Faker::Commerce.department(max: 2, fixed_amount: true) #=> "Books & Tools"

Parameters:

  • max (Integer) (defaults to: 3)

    Updates the maximum number of names used to generate the department name.

  • fixed_amount (Boolean) (defaults to: false)

    Fixes the amount of departments to use instead of using a range.

Returns:

  • (String)

Available since:

  • 1.2.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/faker/default/commerce.rb', line 51

def department(max: 3, fixed_amount: false)
  num = max if fixed_amount
  num ||= 1 + rand(max)

  categories = categories(num)

  if categories.is_a?(Array)
    if categories.length > 1
      merge_categories(categories)
    else
      categories[0]
    end
  else
    categories
  end
end

.materialString

Produces a random material.

Examples:

Faker::Commerce.material #=> "Plastic"

Returns:

  • (String)

Available since:

  • 1.5.0



90
91
92
# File 'lib/faker/default/commerce.rb', line 90

def material
  fetch('commerce.product_name.material')
end

.price(range: 0..100.0, as_string: false) ⇒ Float

Produces a random product price.

Examples:

Faker::Commerce.price #=> 44.6
Faker::Commerce.price(range: 0..10.0, as_string: true) #=> "2.18"

Parameters:

  • range (Range) (defaults to: 0..100.0)

    A range to generate the random number within.

  • as_string (Boolean) (defaults to: false)

    Changes the return value to [String].

Returns:

  • (Float)

Available since:

  • 1.2.0



106
107
108
109
110
111
112
113
# File 'lib/faker/default/commerce.rb', line 106

def price(range: 0..100.0, as_string: false)
  price = (rand(range) * 100).floor / 100.0
  if as_string
    price_parts = price.to_s.split('.')
    price = "#{price_parts[0]}.#{price_parts[-1].ljust(2, '0')}"
  end
  price
end

.product_nameString

Produces a random product name.

Examples:

Faker::Commerce.product_name #=> "Practical Granite Shirt"

Returns:

  • (String)

Available since:

  • 1.2.0



77
78
79
# File 'lib/faker/default/commerce.rb', line 77

def product_name
  "#{fetch('commerce.product_name.adjective')} #{fetch('commerce.product_name.material')} #{fetch('commerce.product_name.product')}"
end

.promotion_code(digits: 6) ⇒ String

Produces a random promotion code.

Examples:

Faker::Commerce.promotion_code #=> "AmazingDeal829102"
Faker::Commerce.promotion_code(digits: 2) #=> "AmazingPrice57"

Parameters:

  • digits (Integer) (defaults to: 6)

    Updates the number of numerical digits used to generate the promotion code.

Returns:

  • (String)

Available since:

  • 1.7.0



30
31
32
33
34
35
36
# File 'lib/faker/default/commerce.rb', line 30

def promotion_code(digits: 6)
  [
    fetch('commerce.promotion_code.adjective'),
    fetch('commerce.promotion_code.noun'),
    Faker::Number.number(digits: digits)
  ].join
end

.vendorstring

Produces a randomized string of a vendor name

Examples:

Faker::Commerce.vendor #=> 'Dollar General'

Returns:

  • (string)

Available since:

  • next



139
140
141
# File 'lib/faker/default/commerce.rb', line 139

def vendor
  fetch('commerce.vendor')
end