Sha256: 20233f95efb1e4915cebb285fe15e3bc3d9d311a7e663887ec3949155d035356

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# -*- encoding: utf-8 -*-

require 'aequitas/virtus_integration/inline_attribute_rule_extractor/object'

module Aequitas
  module VirtusIntegration
    module InlineAttributeRuleExtractor
      class String < Object

        def extract
          rules = super
          rules.concat(Array(extract_length_rules))
          rules.concat(Array(extract_format_rules))
        end

        def extract_length_rules
          length = options.fetch(:length, false)

          case length
          when ::Integer; Rule::Length::Equal.new(attribute.name, :expected => length)
          when ::Range;   Rule::Length::Range.new(attribute.name, :range    => length)
          when ::FalseClass;
          else raise ArgumentError, "expected Integer or Range :length, got: #{length.inspect}"
          end
        end

        def extract_format_rules
          format = options.fetch(:format, false)
          Rule::Format.rules_for(attribute.name, :with => format) if format
        end

      end # class String
    end # module InlineAttributeRuleExtractor
  end # module VirtusIntegration
end # module Aequitas

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aequitas-0.0.2 lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb