-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidied up Faker::Vehicle with fixes for Rails
- Loading branch information
Showing
2 changed files
with
59 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
module Faker | ||
class Vehicle < Base | ||
@vin_chars = '0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ' | ||
@vin_map = '0123456789X' | ||
@vin_weights = '8765432X098765432' | ||
VIN_CHARS = '0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ' | ||
VIN_MAP = '0123456789X' | ||
VIN_WEIGHTS = '8765432X098765432' | ||
|
||
class << self | ||
#ISO 3779 | ||
def vin | ||
manufacture = fetch_all('vehicle.manufacture').sample | ||
_, wmi, wmi_ext = fetch_all('vehicle.manufacture').sample | ||
|
||
c = @vin_chars.split('').reject{ |n| n == '.'} | ||
vehicle_identification_number = manufacture["wmi"].split('').concat( Array.new(14) { c.sample } ) | ||
(12..14).to_a.each_with_index { |n, i| vehicle_identification_number[n] = manufacture["win_ext"][i] } unless manufacture["win_ext"].nil? | ||
c = VIN_CHARS.split('').reject{ |n| n == '.'} | ||
vehicle_identification_number = wmi.split('').concat( Array.new(14) { c.sample } ) | ||
(12..14).to_a.each_with_index { |n, i| vehicle_identification_number[n] = wmi_ext[i] } unless wmi_ext.nil? | ||
vehicle_identification_number[10] = fetch('vehicle.year') | ||
vehicle_identification_number[8] = vin_checksum(vehicle_identification_number) | ||
|
||
vehicle_identification_number.join.upcase | ||
end | ||
|
||
def manufacture | ||
fetch_all('vehicle.manufacture').sample["name"] | ||
fetch_all('vehicle.manufacture').sample.first | ||
end | ||
|
||
private | ||
|
||
def calculate_vin_weight(character, i) | ||
(@vin_chars.index(character) % 10) * @vin_map.index(@vin_weights [i]) | ||
(VIN_CHARS.index(character) % 10) * VIN_MAP.index(VIN_WEIGHTS[i]) | ||
end | ||
|
||
def vin_checksum(vehicle_identification_number) | ||
@vin_map[vehicle_identification_number.each_with_index.map(&method(:calculate_vin_weight)).inject(:+) % 11] | ||
VIN_MAP[vehicle_identification_number.each_with_index.map(&method(:calculate_vin_weight)).inject(:+) % 11] | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters