Sha256: d3dec1f247381164f860e75cfc6d015f64a2873ee14b5b2d36c36003a883476b

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'object_attorney/validations/custom'
require 'object_attorney/allegation'

module ObjectAttorney

  module ClassMethods

    attr_writer :allegations, :defendant_options

    def defendant_options
      @defendant_options ||= {}
    end

    def defend(name, options = {})
      defendant_options.merge!(options.merge(name: name))
    end

    def allegations
      @allegations ||= Hash.new { |hash, key| hash[key] = [] }
    end

    def validate(*args, &block)
      allegations[nil] << Allegation.new(Validations::Custom.new(args, &block))
    end

    def validates_with(*args, &block)
      options = args.extract_options!

      args.each do |validation_class|
        store_allegations_by_attribute validation_class.new(options, &block)
      end
    end

    def store_allegations_by_attribute(validation)
      validation.attributes.each do |attribute|
        allegations[attribute.to_sym] << Allegation.new(validation)
      end
    end

    # Copy allegations on inheritance.
    def inherited(base)
      base.allegations = allegations.clone
      base.defendant_options = defendant_options.clone

      super
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
object_attorney-3.0.2 lib/object_attorney/class_methods.rb
object_attorney-3.0.0 lib/object_attorney/class_methods.rb