Sha256: accaac43531bd33f5864bab7ce6d907cdead5a26ee62b58be9c62f60cc990374
Contents?: true
Size: 1.34 KB
Versions: 2
Compression:
Stored size: 1.34 KB
Contents
class GtinValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value, options) record.errors[attribute] << (options[:message] || I18n.t('errors.messages.gtin')) end end private def includes_key?(value, options) value.any? { |key| options.keys.include?(key) } end def valid_format?(value, options) if options[:strict] value =~ /^[0-9]+$/ else value =~ /^[0-9 ]+$/ end end def valid_length?(value, options) value_size = value.size case options[:format] when :ean_8, :gtin_8, :ucc_8 value_size == 8 when :gtin_12, :upc, :upc_a value_size == 12 when :ean, :ean_13, :gtin_13, :ucc_13 value_size == 13 when :gtin_14, :ucc_14 value_size == 14 else [8, 12, 13, 14].include?(value_size) end end def valid_checksum?(value) reversed_values = value.reverse odd = even = 0 (1..(reversed_values.length - 1)).each do |i| i.even? ? (even += reversed_values[i].chr.to_i) : (odd += reversed_values[i].chr.to_i) end reversed_values[0].chr.to_i == (10 - ((odd * 3) + even) % 10) end def valid?(value, options) striped_value = value.to_s.gsub(/\D/, '') valid_format?(value, options) && valid_length?(striped_value, options) && valid_checksum?(striped_value) end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
flash_validators-3.0.1 | lib/flash_validators/validators/gtin_validator.rb |
flash_validators-3.0.0 | lib/flash_validators/validators/gtin_validator.rb |