module DailyAffirmation::Affirmations

Attributes

object[RW]

Public Class Methods

included(descendant) click to toggle source
# File lib/daily_affirmation/affirmations.rb, line 3
def self.included(descendant)
  descendant.extend(ClassMethods)
end
new(object) click to toggle source

Creates a new instance of the validator.

@param object [Object] the object to validate. @return [self]

# File lib/daily_affirmation/affirmations.rb, line 11
def initialize(object)
  self.object = object
end

Public Instance Methods

error_messages() click to toggle source

Returns an array of error message if any, otherwise an empty array.

@return [Array<String>]

# File lib/daily_affirmation/affirmations.rb, line 37
def error_messages
  validate[1]
end
valid?() click to toggle source

Tells you if the object is valid based on the affirmations.

@return [true, false]

# File lib/daily_affirmation/affirmations.rb, line 18
def valid?
  validate[0]
end
validate() click to toggle source

Returns an array of length 2 telling you if the object is valid, along with any error messages.

@return [Array(Boolean, Array<String>)] Array of length 2 containing

validation results.
# File lib/daily_affirmation/affirmations.rb, line 27
def validate
  @validate ||= [
    affirmations.map(&:first).all?,
    affirmations.map(&:last).compact
  ]
end

Private Instance Methods

affirm(affirmation) click to toggle source
# File lib/daily_affirmation/affirmations.rb, line 50
def affirm(affirmation)
  type = affirmation[:type]
  attribute = affirmation[:attribute]
  args = affirmation.reject { |k, _| [:type, :attribute].include?(k) }

  if ProcessAffirmationEvaluator.new(object, attribute, args).process?
    validator = Object.const_get(
      "DailyAffirmation::Validators::#{type.to_s.capitalize}Validator"
    )
    results = validator.new(object, attribute, args).affirm

    if args.fetch(:inverse, false)
      [!results[0], results[1]]
    else
      results
    end
  else
    [true, nil]
  end
end
affirmations() click to toggle source
# File lib/daily_affirmation/affirmations.rb, line 45
def affirmations
  @affirmations ||= self.class.affirmations
    .map { |affirmation| affirm(affirmation) }
end