Sha256: 35bd78daec88ed129b4f4298c05253420e1ada86432a7dbe88b72b27dfd22bc1
Contents?: true
Size: 1.75 KB
Versions: 4
Compression:
Stored size: 1.75 KB
Contents
module Tiss::<%= version %>::Validator class <%= name %> TYPE = <%= type %> <% unless ws_action.nil? %> WS_ACTION = <%= ws_action %> <% end %> ENUM_VALUES = <%= enum_values %> <% unless pattern.nil? %> PATTERN = "<%= pattern %>" REGEXP = Regexp.new("^<%= pattern %>$") <% end %> def validate_each(record, attribute, value) validate_type record, attribute, value handle_whitespaces record, attribute, value unless self.class::ENUM_VALUES.empty? validate_enumeration record, attribute, value end if self.class.const_defined? 'REGEXP' validate_regexp record, attribute, value end end private def validate_type(record, attribute, value) unless value.is_a? self.class::TYPE add_error record, attribute, "#{value}: not a #{self.class::TYPE}" end end def handle_whitespaces(record, attribute, value) if self.class.const_defined? 'WS_ACTION' case self.class::WS_ACTION when 'replace' then value.gsub! /[\n\t\r ]/, ' ' when 'collapse' then value.gsub! /[\n\t\r]/, ' ' value = value.split.join ' ' end record.send "#{attribute}=", value end end def validate_enumeration(record, attribute, value) unless self.class::ENUM_VALUES.include? value.to_s add_error record, attribute, "#{value}: not in #{self.class::ENUM_VALUES}" end end def validate_regexp(record, attribute, value) unless value =~ self.class::REGEXP add_error record, attribute, "#{value}: not matching #{self.class::PATTERN}" end end def add_error(record, attribute, message = '') record.errors[attribute] << (options[:message] || message) end end end
Version data entries
4 entries across 4 versions & 1 rubygems