Sha256: e87fd9e49157071bf3d834239d919ce065ea4cfe73bca5db10d607ee8394b754

Contents?: true

Size: 960 Bytes

Versions: 9

Compression:

Stored size: 960 Bytes

Contents

module ActiveModel
  module Validations
    # Decides how to get the value from array or other type of attribute.
    class CollectionItemsValidator::ArrayAttributeGetter
      class << self
        def get(attribute, when_array:, when_normal:)
          new(attribute, when_array: when_array, when_normal: when_normal).get
        end
      end

      def initialize(attribute, when_array:, when_normal:)
        @attribute = attribute
        @when_array = when_array
        @when_normal = when_normal
      end

      def get
        if match_data
          when_array.call attibute_name, index
        else
          when_normal.call
        end
      end

      private

      attr_reader :attribute, :when_array, :when_normal

      def match_data
        @match_data ||= /\A(.*)\[\d*\]\Z/.match(attribute)
      end

      def attibute_name
        match_data[1].to_sym
      end

      def index
        match_data[2].to_i
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ph_model-1.2.0 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-1.1.3 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-1.1.2 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-1.1.1 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-1.1.0 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-1.0.2 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-1.0.1 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-1.0.0 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb
ph_model-0.0.1 lib/active_model/validations/collection_items_validator/array_attribute_getter.rb