-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
commerce.rb
66 lines (52 loc) · 1.58 KB
/
commerce.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
module Faker
class Commerce < Base
class << self
def color
fetch('color.name')
end
def promotion_code(digits = 6)
[
fetch('commerce.promotion_code.adjective'),
fetch('commerce.promotion_code.noun'),
Faker::Number.number(digits)
].join
end
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
def product_name
"#{fetch('commerce.product_name.adjective')} #{fetch('commerce.product_name.material')} #{fetch('commerce.product_name.product')}"
end
def material
fetch('commerce.product_name.material')
end
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
private
def categories(num)
categories = []
while categories.length < num
category = fetch('commerce.department')
categories << category unless categories.include?(category)
end
categories
end
def merge_categories(categories)
separator = fetch('separator')
comma_separated = categories.slice!(0...-1).join(', ')
[comma_separated, categories[0]].join(separator)
end
end
end
end