Class: RestMyCase::AccusationAttorneys::Each

Inherits:
Base
  • Object
show all
Defined in:
lib/rest_my_case/accusation_attorneys/each.rb

Overview

I DO NOT CLAIM OWNERSHIP OF THIS CODE, THIS CODE WAS TAKEN FROM “ActiveModel” GEM AND ADAPTED TO RUN WITHOUT “ActiveSupport” ORIGINAL SOURCE FILE: ActiveModel::EachValidator

Direct Known Subclasses

Format, Length, Numericality, Presence

Instance Attribute Summary collapse

Attributes inherited from Base

#base, #options

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Each



12
13
14
15
16
17
# File 'lib/rest_my_case/accusation_attorneys/each.rb', line 12

def initialize(options)
  @attributes = Array(options.delete(:attributes))
  fail ArgumentError, ":attributes cannot be blank" if @attributes.empty?
  super
  check_validity!
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/rest_my_case/accusation_attorneys/each.rb', line 10

def attributes
  @attributes
end

Instance Method Details

#check_validity!Object



33
# File 'lib/rest_my_case/accusation_attorneys/each.rb', line 33

def check_validity!; end

#validate(record) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rest_my_case/accusation_attorneys/each.rb', line 19

def validate(record)
  attributes.each do |attribute|
    value = record.respond_to?(attribute) ? record.send(attribute) : nil
    next if (value.nil? && options[:allow_nil]) || (Helpers.blank?(value) && options[:allow_blank])
    validate_each(record, attribute, value)
  end
end

#validate_each(record, attribute, value) ⇒ Object

Override this method in subclasses with the validation logic, adding errors to the records errors array where necessary.



29
30
31
# File 'lib/rest_my_case/accusation_attorneys/each.rb', line 29

def validate_each(record, attribute, value)
  fail NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method"
end