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, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.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(legacy_max = NOT_GIVEN, legacy_fixed_amount = NOT_GIVEN, 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


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/faker/default/commerce.rb', line 55

def department(legacy_max = NOT_GIVEN, legacy_fixed_amount = NOT_GIVEN, max: 3, fixed_amount: false)
  warn_for_deprecated_arguments do |keywords|
    keywords << :max if legacy_max != NOT_GIVEN
    keywords << :fixed_amount if legacy_fixed_amount != NOT_GIVEN
  end

  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


99
100
101
# File 'lib/faker/default/commerce.rb', line 99

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

.price(legacy_range = NOT_GIVEN, legacy_as_string = NOT_GIVEN, 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


115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/faker/default/commerce.rb', line 115

def price(legacy_range = NOT_GIVEN, legacy_as_string = NOT_GIVEN, range: 0..100.0, as_string: false)
  warn_for_deprecated_arguments do |keywords|
    keywords << :range if legacy_range != NOT_GIVEN
    keywords << :as_string if legacy_as_string != NOT_GIVEN
  end

  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


86
87
88
# File 'lib/faker/default/commerce.rb', line 86

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

.promotion_code(legacy_digits = NOT_GIVEN, 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
37
38
39
40
# File 'lib/faker/default/commerce.rb', line 30

def promotion_code(legacy_digits = NOT_GIVEN, digits: 6)
  warn_for_deprecated_arguments do |keywords|
    keywords << :digits if legacy_digits != NOT_GIVEN
  end

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