class Faker::Commerce

Public Class Methods

color() click to toggle source
# File lib/faker/commerce.rb, line 5
def color
  fetch('color.name')
end
department(max = 3, fixed_amount = false) click to toggle source
# File lib/faker/commerce.rb, line 17
def department(max = 3, fixed_amount = false)
  num = max if fixed_amount
  num ||= 1 + rand(max)

  categories = categories(num)

  return merge_categories(categories) if num > 1
  categories[0]
end
material() click to toggle source
# File lib/faker/commerce.rb, line 31
def material
  fetch('commerce.product_name.material')
end
price(range=0..100.0, as_string=false) click to toggle source
# File lib/faker/commerce.rb, line 35
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
# File lib/faker/commerce.rb, line 27
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
# File lib/faker/commerce.rb, line 9
def promotion_code(digits = 6)
  [
    fetch('commerce.promotion_code.adjective'),
    fetch('commerce.promotion_code.noun'),
    Faker::Number.number(digits)
  ].join
end

Private Class Methods

categories(num) click to toggle source
# File lib/faker/commerce.rb, line 46
def categories(num)
  categories = []
  while categories.length < num
    category = fetch('commerce.department')
    categories << category unless categories.include?(category)
  end

  categories
end
merge_categories(categories) click to toggle source
# File lib/faker/commerce.rb, line 56
def merge_categories(categories)
  separator = fetch('separator')
  comma_separated = categories.slice!(0...-1).join(', ')

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