class Faker::Commerce

Public Class Methods

brand() click to toggle source

Produces a randomized string of a brand name @example

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

@return [string]

@faker.version next

# File lib/faker/default/commerce.rb, line 125
def brand
  fetch('commerce.brand')
end
color() click to toggle source

Produces a random color.

@return [String]

@example

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

@faker.version 1.2.0

# File lib/faker/default/commerce.rb, line 15
def color
  fetch('color.name')
end
department(max: 3, fixed_amount: false) click to toggle source

Produces a random department.

@param max [Integer] Updates the maximum number of names used to generate the department name. @param fixed_amount [Boolean] Fixes the amount of departments to use instead of using a range. @return [String]

@example

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"

@faker.version 1.2.0

# 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
material() click to toggle source

Produces a random material.

@return [String]

@example

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

@faker.version 1.5.0

# File lib/faker/default/commerce.rb, line 90
def material
  fetch('commerce.product_name.material')
end
price(range: 0..100.0, as_string: false) click to toggle source

Produces a random product price.

@param range [Range] A range to generate the random number within. @param as_string [Boolean] Changes the return value to [String]. @return [Float]

@example

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

@faker.version 1.2.0

# 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_name() click to toggle source

Produces a random product name.

@return [String]

@example

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

@faker.version 1.2.0

# 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) click to toggle source

Produces a random promotion code.

@param digits [Integer] Updates the number of numerical digits used to generate the promotion code. @return [String]

@example

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

@faker.version 1.7.0

# 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
vendor() click to toggle source

Produces a randomized string of a vendor name @example

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

@return [string]

@faker.version next

# File lib/faker/default/commerce.rb, line 139
def vendor
  fetch('commerce.vendor')
end

Private Class Methods

categories(num) click to toggle source
# File lib/faker/default/commerce.rb, line 145
def categories(num)
  sample(fetch_all('commerce.department'), num)
end
merge_categories(categories) click to toggle source
# File lib/faker/default/commerce.rb, line 149
def merge_categories(categories)
  separator = fetch('separator')
  comma_separated = categories.slice!(0...-1).join(', ')

  [comma_separated, categories[0]].join(separator)
end