Module: FFaker::Code

Extended by:
Code, ModuleUtils
Included in:
Code
Defined in:
lib/ffaker/code.rb

Instance Method Summary collapse

Methods included from ModuleUtils

const_missing, k, underscore, unique

Methods included from RandomUtils

#fetch_sample, #rand, #shuffle

Instance Method Details

#eanObject


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ffaker/code.rb', line 9

def ean
  ean = rand(100_000_000_000..999_999_999_999).to_s

  sum = 0
  ean.chars.each_with_index { |c, i| sum += c.to_i * (i.odd? ? 3 : 1) }

  nearest_multiple_of_ten = sum.round(-1)
  nearest_multiple_of_ten += 10 if nearest_multiple_of_ten < sum

  "#{ean}#{nearest_multiple_of_ten - sum}"
end