module SmartCore::Validator::DSL

Public Class Methods

extended(base_klass) click to toggle source

@param base_klass [Class] @return [void]

@api private @since 0.1.0

# File lib/smart_core/validator/dsl.rb, line 13
def extended(base_klass)
  base_klass.instance_variable_set(:@__commands__, CommandSet.new)
  base_klass.instance_variable_set(:@__attributes__, AttributeSet.new)

  base_klass.singleton_class.prepend(Module.new do
    def inherited(child_klass)
      child_klass.instance_variable_set(:@__commands__, CommandSet.new)
      child_klass.instance_variable_set(:@__attributes__, AttributeSet.new)

      child_klass.commands.concat(commands)
      child_klass.attributes.concat(attributes)

      super(child_klass)
    end
  end)
end
inherited(child_klass) click to toggle source
Calls superclass method
# File lib/smart_core/validator/dsl.rb, line 18
def inherited(child_klass)
  child_klass.instance_variable_set(:@__commands__, CommandSet.new)
  child_klass.instance_variable_set(:@__attributes__, AttributeSet.new)

  child_klass.commands.concat(commands)
  child_klass.attributes.concat(attributes)

  super(child_klass)
end

Public Instance Methods

attribute(attribute_name, default: nil) click to toggle source

@param attribute_name [String, Symbol] @return [void]

@api public @since 0.1.0

# File lib/smart_core/validator/dsl.rb, line 36
def attribute(attribute_name, default: nil)
  attribute = SmartCore::Validator::Attribute.new(attribute_name, default)
  attributes << attribute
  attr_reader attribute.name
end
attributes() click to toggle source

@return [SmartCore::Validator::AttributeSet]

@api private @since 0.1.0

# File lib/smart_core/validator/dsl.rb, line 46
def attributes
  @__attributes__
end
clear_commands() click to toggle source

@return [void]

@api private @since 0.1.0

# File lib/smart_core/validator/dsl.rb, line 62
def clear_commands
  commands.clear
end
commands() click to toggle source

@return [SmartCore::Validator::CommandSet]

@api private @since 0.1.0

# File lib/smart_core/validator/dsl.rb, line 54
def commands
  @__commands__
end
validate(validating_method, &nested_validations) click to toggle source

@param validating_method [Symbol, String] @param nested_validations [Proc] @return [void]

@see SmartCore::Validator::Commands::AddValidation @see SmartCore::Validator::Commands::AddNestedValidations

@api public @since 0.1.0

# File lib/smart_core/validator/dsl.rb, line 75
def validate(validating_method, &nested_validations)
  if block_given?
    commands << Commands::AddNestedValidations.new(validating_method, nested_validations)
  else
    commands << Commands::AddValidation.new(validating_method)
  end
end
validate_with(validating_klass, &nested_validations) click to toggle source

@param validating_klass [Class<SmartCore::Validator>] @param nested_validations [Proc] @return [void]

@see SmartCore::Validator::Commands::ValidateWith

@api public @since 0.1.0

# File lib/smart_core/validator/dsl.rb, line 91
def validate_with(validating_klass, &nested_validations)
  commands << Commands::ValidateWith.new(validating_klass, nested_validations)
end