class ActiveModel::Validations::CollectionItemsValidator::ArrayAttributeGetter

Decides how to get the value from array or other type of attribute.

Attributes

attribute[R]
when_array[R]
when_normal[R]

Public Class Methods

get(attribute, when_array:, when_normal:) click to toggle source
# File lib/active_model/validations/collection_items_validator/array_attribute_getter.rb, line 6
def get(attribute, when_array:, when_normal:)
  new(attribute, when_array: when_array, when_normal: when_normal).get
end
new(attribute, when_array:, when_normal:) click to toggle source
# File lib/active_model/validations/collection_items_validator/array_attribute_getter.rb, line 11
def initialize(attribute, when_array:, when_normal:)
  @attribute = attribute
  @when_array = when_array
  @when_normal = when_normal
end

Public Instance Methods

get() click to toggle source
# File lib/active_model/validations/collection_items_validator/array_attribute_getter.rb, line 17
def get
  if match_data
    when_array.call attibute_name, index
  else
    when_normal.call
  end
end

Private Instance Methods

attibute_name() click to toggle source
# File lib/active_model/validations/collection_items_validator/array_attribute_getter.rb, line 33
def attibute_name
  match_data[1].to_sym
end
index() click to toggle source
# File lib/active_model/validations/collection_items_validator/array_attribute_getter.rb, line 37
def index
  match_data[2].to_i
end
match_data() click to toggle source
# File lib/active_model/validations/collection_items_validator/array_attribute_getter.rb, line 29
def match_data
  @match_data ||= /\A(.*)\[\d*\]\Z/.match(attribute)
end