Permalink
Browse files

awesomeness initialized

Signed-off-by: zaiste <oh@zaiste.net>
  • Loading branch information...
0 parents commit 843ed9c89b104e462e8606e41447811860978166 @zaiste committed Aug 15, 2011
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
2 .rvmrc
@@ -0,0 +1,2 @@
+rvm use 1.9.3
+rvm gemset use valideez
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in valideez.gemspec
+gemspec
@@ -0,0 +1,7 @@
+Copyright (c) 2011 Zaiste! de Grengolada
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
+Valideez
+========
+
+Let's valideez lousy objects once and forever!
+
+Instalation
+-----------
+
+ gem install valideez
+
+Usage
+-----
+
+License
+-------
+
+Released under the MIT License. See the [LICENSE][license] file for further details.
+
+[license]: https://github.com/zaiste/valideez/blob/master/LICENSE.md
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
+require "valideez/version"
+
+module Valideez
+ # Your code goes here...
+end
@@ -0,0 +1,34 @@
+module Valideez
+ class Base
+ attr_reader :val
+ attr_accessor :validable
+
+ def initialize(val)
+ @val = val.to_s.gsub(/-/, '')
+ @validable = []
+ end
+
+ def valid?
+ p = validable.select{ |var| respond_to?("validate_#{var.to_s}", true) }
+ p.take_while { |var| send("validate_#{var.to_s}") }.size == p.size
+ end
+
+ private
+
+ def assign(opts)
+ opts.each do |attr, val|
+ # tricky: don't create an instance variable if
+ # value is nil OR value is false
+ if val
+ instance_variable_set("@#{attr}", val)
+ # create read-only accessor
+ self.class.send(:define_method, attr.to_s) do
+ instance_variable_get("@" + attr.to_s)
+ end
+
+ validable << attr.to_s
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,10 @@
+module Valideez
+ class Bastard
+ def initialize(val)
+ end
+
+ def valid?
+ true
+ end
+ end
+end
@@ -0,0 +1,12 @@
+module Valideez
+ module Common
+ def validate_format
+ not (format =~ val).nil?
+ end
+
+ def validate_length
+ length == val.size
+ end
+
+ end
+end
@@ -0,0 +1,32 @@
+module Valideez
+ class Nip < Valideez::Base
+ include Valideez::Common
+
+ def initialize(val, options = {})
+ super val
+
+ assign({
+ :sum_control => true,
+ :format => /\A\d{10}\Z/,
+ :length => 10,
+ :mask => [6, 5, 7, 2, 3, 4, 5, 6, 7],
+ :modulo => 11,
+ }.merge(options))
+
+ @arr = []
+ end
+
+ def validate_sum_control
+ mod = checksum % modulo
+ mod === @arr.shift
+ end
+
+ private
+
+ def checksum
+ @arr = val.split("").collect(&:to_i)
+ mask.inject(0) {|sum, w| sum + w * @arr.shift}
+ end
+
+ end
+end
@@ -0,0 +1,18 @@
+require 'active_model'
+
+class NipValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ @message = options[:message] || "invalid format"
+ record.errors[attribute] << @message unless Valideez::Nip.new(value).valid?
+ end
+end
+
+module ActiveModel
+ module Validations
+ module HelperMethods
+ def validates_nip_of(*attr_names)
+ validates_with NipValidator, _merge_attributes(attr_names)
+ end
+ end
+ end
+end
@@ -0,0 +1,57 @@
+require 'date'
+
+module Valideez
+ class Pesel < Valideez::Base
+ include Valideez::Common
+
+ def initialize(val, options = {})
+ super val
+
+ assign({
+ :length => 11,
+ :format => /\A\d{11}\Z/,
+ :mask => [1, 3, 7, 9, 1, 3, 7, 9, 1, 3],
+ :modulo => 10,
+ :age => 0,
+ :sum_control => true,
+ }.merge(options))
+
+ @arr = []
+ end
+
+ def validate_sum_control
+ mod = checksum % modulo
+ mod = modulo - mod
+ mod = 0 if mod == modulo
+
+ mod === @arr.shift
+ end
+
+ def validate_age
+ now = Time.now
+ y = (now.strftime("%Y").to_i - age).to_s
+ m = now.strftime("%m")
+ d = now.strftime("%d")
+
+ age == 0 || convert_to_date(val[0..5]) <= Date.parse([y, m, d].join("-"))
+ end
+
+ private
+
+ def checksum
+ @arr = val.split("").collect(&:to_i)
+ mask.inject(0) {|sum, w| sum + w * @arr.shift}
+ end
+
+ def convert_to_date(sample)
+ y, m, d = sample.scan(/../)
+
+ year = %w(19 20 21 22)[m[0].to_i / 2].to_s + y
+ month = (m.to_i - m[0].to_i * 10).to_s
+
+ Date.parse [year, month, d].join("-")
+ end
+
+ end
+
+end
@@ -0,0 +1,19 @@
+require 'active_model'
+
+class PeselValidator < ActiveModel::EachValidator
+
+ def validate_each(record, attribute, value)
+ message = options[:message] || "invalid format"
+ record.errors.add(attribute, @message) unless Valideez::Pesel.new(value).valid?
+ end
+end
+
+module ActiveModel
+ module Validations
+ module HelperMethods
+ def validates_pesel_of(*attr_names)
+ validates_with PeselValidator, _merge_attributes(attr_names)
+ end
+ end
+ end
+end
@@ -0,0 +1,24 @@
+module Valideez
+ class Phone < Valideez::Base
+ include Valideez::Common
+
+ def initialize(val, options = {})
+ super val
+
+ assign({
+ :mobile => false,
+ :format => /\A\d{9}\Z/,
+ }.merge(options))
+ end
+
+ private
+
+ def validate_mobile
+ %w(500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
+ 530 531 532 533 534 535 600 601 602 603 604 605 606 607 608 609 660 661 662 663
+ 664 665 666 667 668 669 690 691 692 693 694 695 696 697 698 699 720 721 722 723
+ 724 725 726 727 728 729 780 781 782 783 784 785 786 787 788 789 790 791 792 793
+ 794 795 796 797 798 799 881 882 882 883 883 884 885 886 887 888 889).include?(@val[0..2])
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Valideez
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,7 @@
+require "spec_helper"
+
+describe Valideez::Bastard do
+ it "is valid" do
+ Valideez::Bastard.new("1").should be_valid
+ end
+end
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+describe Valideez::Nip do
+ it "should be valid" do
+ %w[123-456-32-18].each do |n|
+ Valideez::Nip.new(n).should be_valid
+ end
+ end
+end
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe Valideez::Pesel do
+ it "should be valid"do
+ %w[49040501580 93031908746].each do |n|
+ Valideez::Pesel.new(n).should be_valid
+ end
+ end
+
+ it "should be invalid" do
+ %w[59040501580 870132114508 AABB].each do |n|
+ Valideez::Pesel.new(n).should_not be_valid
+ end
+ end
+
+ it "should be valid with minimum age defined" do
+ %w[49040501580].each do |n|
+ Valideez::Pesel.new(n, :age => 18).should be_valid
+ end
+
+ end
+
+ it "should be invalid when pesel is valid but the person is too young" do
+ %w[05220471553].each do |n|
+ Valideez::Pesel.new(n).should be_valid
+ Valideez::Pesel.new(n, :age => 18).should_not be_valid
+ end
+ end
+end
@@ -0,0 +1,22 @@
+require 'spec_helper'
+require 'rspec'
+require 'active_model'
+
+class Bastard
+ include ActiveModel::Validations
+
+ attr_accessor :pesel
+ validates :pesel, :presence => true, :pesel => true
+end
+
+describe PeselValidator do
+ before :each do
+ @model = Bastard.new
+ end
+
+ it "should be valid" do
+ @model.should_not be_valid
+ @model.pesel = '68041979693'
+ @model.should be_valid
+ end
+end
@@ -0,0 +1,6 @@
+require "bundler"
+Bundler.setup
+
+require "rspec"
+
+Dir[File.expand_path(File.dirname(__FILE__) + "/../lib/**/*.rb")].each { |f| require f }
@@ -0,0 +1,25 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "valideez/version"
+
+Gem::Specification.new do |s|
+ s.name = "valideez"
+ s.version = Valideez::VERSION
+ s.authors = ["zaiste!"]
+ s.email = ["oh@zaiste.net"]
+ s.homepage = "http://dev.zaiste.net/gem/valideez"
+ s.summary = %q{Let's valideez lousy objects once and forever}
+ s.description = %q{Your favourite validations with Ruby}
+
+ s.rubyforge_project = "valideez"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "rake"
+ s.add_development_dependency 'activemodel','>= 3.0.0'
+end

0 comments on commit 843ed9c

Please sign in to comment.