No release in over 3 years
Low commit activity in last 3 years
Validate custom ruby objects using validation defined in the class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Build Status Gem Version Code Climate Coverage Status

Overview

This is very simple gem to allow custom validations in a ruby object. You are supposed to define your validations.

Dependencies

There are no runtime dependencies for this gem. This gem runs with ruby 1.9+ or compatible versions. Ruby 1.8 isn't supported. Please use version 0.1.6 of simple_validation if you wish to use ruby 1.8.

Installation

gem install simple_validation

Usage

require 'simple_validation'

class AlienNumber
  include SimpleValidation

  validate :digits_are_positive
  validate :digits_are_less_than_ten, :if => [:digits_count_even?]

  def initialize(*digits)
    @digits = digits
  end

  def value
    @digits.reduce(0) {|result, digit| result + digit}
  end

  private

  def digits_are_positive
    @digits.each do |digit|
      add_error("#{digit} is negative") unless digit > 0
    end
  end

  def digits_are_less_than_ten
    @digits.each do |digit|
      add_error("#{digit} is greater than 9") unless digit < 10
    end
  end

  def digits_count_even?
    @digits.size.even?
  end
end

valid_number = AlienNumber.new(1, 2, 3)
valid_number.valid? # true
valid_number.invalid? # false
valid_number.errors # []
valid_number.value # 6

invalid_number = AlienNumber.new(-1, 12)
invalid_number.valid? # false
invalid_number.invalid? # true
invalid_number.errors # ["-1 is negative", "12 is greater than 9"]
invalid_number.value # 11

another_invalid_number = AlienNumber.new(-10, 12, 15)
another_invalid_number.valid? # false
another_invalid_number.invalid? # true
another_invalid_number.errors # ["-10 is negative"]
another_invalid_number.value # 17

Running tests

  1. Clone the repository.
  2. Run bundle from the root directory.
  3. Run bundle rake from the root directory.

License

This tool is released under the MIT license. Please refer to LICENSE for more details.