Sha256: 70622dc9e6216e34f0fd489f524bce814fa9cc537ddbc4a9a9ad629d48be4118

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module ConditionalValidation
  # ConditionalValidation::ValidationFlag is extended by model classes to add
  # the `validation_flag` macro.
  module ValidationFlag
    # Macro method for defining attr_accessor methods, and the associated
    # enable/disable/predicate methods that wrap the attr_acessor methods, for
    # determining when to run validation sets on a model.
    #
    # @param flags [*Array<String>] the validation flag names
    #
    # @example
    #   class User
    #     validation_flag :address_attributes
    #   end
    def validation_flag(*flags)
      raise ArgumentError, "flags can't be empty" if flags.empty?

      accessor_method_names = flags.map { |flag| "_#{flag}_validation_flag" }
      attr_accessor(*accessor_method_names)

      flags.each do |flag|
        # def enable_address_attributes_validation
        # def disable_address_attributes_validation
        # def validate_on_address_attributes?
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def enable_#{flag}_validation
            self._#{flag}_validation_flag = true
            self
          end

          def disable_#{flag}_validation
            self._#{flag}_validation_flag = false
            self
          end

          def validate_on_#{flag}?
            !!_#{flag}_validation_flag
          end
        RUBY
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conditional_validation-1.0.0 lib/conditional_validation/validation_flag.rb