Sha256: 3c74ac75356312f60464de1f5741fd9951612b28982757ca217e2ca0b06d2608
Contents?: true
Size: 1.38 KB
Versions: 1
Compression:
Stored size: 1.38 KB
Contents
class GtinValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value, options) record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.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.first.chr.to_i == (0 == (odd * 3 + even) % 10 ? 0 : 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
yukonisuru-0.0.1 | lib/yukonisuru/validators/gtin_validator.rb |