va 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +6 -1
- data/lib/va/version.rb +1 -1
- data/lib/va.rb +22 -4
- data/spec/errors_spec.rb +50 -0
- metadata +3 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 0c2d90020330d06782347f0a78de34f49d02649f
         | 
| 4 | 
            +
              data.tar.gz: 7f35f588cda7e35ef2eb675e39bff9031e8496a6
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: b0f2484df8044cbadd7a1a4f0e7131246c31bd80c274fae99f8a68c41d78f4d7b00e4c4475a79d610bb42427dcbbf623cd3c9a908c942dbfe09bde9597cef62a
         | 
| 7 | 
            +
              data.tar.gz: cb2e88857bc6ebf2ada32ce9020eefb9903e2852986bd2e40f9714994ae2f8fcd2cf4569638a245e1ebc8f9665ecb2be71c6c3be4604d55bc532dacd1bb155ec
         | 
    
        data/Rakefile
    CHANGED
    
    | @@ -1,7 +1,12 @@ | |
| 1 1 | 
             
            require "bundler/gem_tasks"
         | 
| 2 2 |  | 
| 3 3 | 
             
            task :test do
         | 
| 4 | 
            -
               | 
| 4 | 
            +
              arg_files = ENV["FILES"] && ENV["FILES"].split(/[\s,]+/)
         | 
| 5 | 
            +
              all_files = Rake::FileList["./spec/**/*_spec.rb"]
         | 
| 6 | 
            +
              files = arg_files || all_files
         | 
| 7 | 
            +
              puts "\nRuning tests for: #{ files.join(" ") }\n\n"
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              system *["matest"].concat(files)
         | 
| 5 10 | 
             
            end
         | 
| 6 11 |  | 
| 7 12 | 
             
            task :default => :test
         | 
    
        data/lib/va/version.rb
    CHANGED
    
    
    
        data/lib/va.rb
    CHANGED
    
    | @@ -3,6 +3,7 @@ require "va/version" | |
| 3 3 | 
             
            module Va
         | 
| 4 4 | 
             
              class Model
         | 
| 5 5 | 
             
                attr_reader :attributes
         | 
| 6 | 
            +
                attr_reader :errors
         | 
| 6 7 |  | 
| 7 8 | 
             
                def initialize(args={})
         | 
| 8 9 | 
             
                  @attributes = {}
         | 
| @@ -10,12 +11,26 @@ module Va | |
| 10 11 | 
             
                    key = k.to_sym
         | 
| 11 12 | 
             
                    @attributes[key] = v if self.class.keys.include?(key)
         | 
| 12 13 | 
             
                  end
         | 
| 14 | 
            +
                  @errors = {}
         | 
| 15 | 
            +
                  @valid = validate
         | 
| 13 16 | 
             
                end
         | 
| 14 17 |  | 
| 15 | 
            -
                def  | 
| 16 | 
            -
                  self.class.validations. | 
| 17 | 
            -
                    validation.call(*attrs.map { |attr| self.send(attr)})
         | 
| 18 | 
            +
                def validate
         | 
| 19 | 
            +
                  invalid_validations = self.class.validations.select { |attrs, msg, validation|
         | 
| 20 | 
            +
                    is_invalid = !validation.call(*attrs.map { |attr| self.send(attr)})
         | 
| 21 | 
            +
                    key = attrs.count == 1 ? attrs.first : attrs
         | 
| 22 | 
            +
                    errors[key] = msg || "is invalid" if is_invalid
         | 
| 23 | 
            +
                    is_invalid
         | 
| 18 24 | 
             
                  }
         | 
| 25 | 
            +
                  invalid_validations.empty?
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                def message(msg="", *attrs)
         | 
| 29 | 
            +
                  raise __callee__.inspect
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
                
         | 
| 32 | 
            +
                def valid?
         | 
| 33 | 
            +
                  @valid
         | 
| 19 34 | 
             
                end
         | 
| 20 35 |  | 
| 21 36 | 
             
                private
         | 
| @@ -24,10 +39,13 @@ module Va | |
| 24 39 | 
             
                end
         | 
| 25 40 |  | 
| 26 41 | 
             
                def self.validate(*attrs, &block)
         | 
| 42 | 
            +
                  msg = if attrs.last.is_a? String
         | 
| 43 | 
            +
                          attrs.pop
         | 
| 44 | 
            +
                        end
         | 
| 27 45 | 
             
                  attrs.each do |attr|
         | 
| 28 46 | 
             
                    raise UnknownAttribute unless keys.include?(attr)
         | 
| 29 47 | 
             
                  end
         | 
| 30 | 
            -
                  validations << [attrs, block]
         | 
| 48 | 
            +
                  validations << [attrs, msg, block]
         | 
| 31 49 | 
             
                end
         | 
| 32 50 |  | 
| 33 51 | 
             
                def self.validate_present(*attrs)
         | 
    
        data/spec/errors_spec.rb
    ADDED
    
    | @@ -0,0 +1,50 @@ | |
| 1 | 
            +
            require "./spec/spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            scope "errors" do
         | 
| 4 | 
            +
              class Truthy < Va::Model
         | 
| 5 | 
            +
                attribute :t
         | 
| 6 | 
            +
                validate(:t, "not truthy") do |t|
         | 
| 7 | 
            +
                  t
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              spec do
         | 
| 12 | 
            +
                va = Truthy.new(t: false)
         | 
| 13 | 
            +
                @errors = va.errors
         | 
| 14 | 
            +
                @errors == { t: "not truthy"}
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              class Falsey < Va::Model
         | 
| 18 | 
            +
                attribute :f1
         | 
| 19 | 
            +
                attribute :f2
         | 
| 20 | 
            +
                validate(:f1, :f2, "not falsey") do |f1, f2|
         | 
| 21 | 
            +
                  !f1 && !f2
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              spec do
         | 
| 26 | 
            +
                va = Falsey.new(f1: false, f2: true)
         | 
| 27 | 
            +
                @errors = va.errors
         | 
| 28 | 
            +
                @errors == { [:f1, :f2] => "not falsey"}
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              class WithoutMessages < Va::Model
         | 
| 32 | 
            +
                attribute :a
         | 
| 33 | 
            +
                attribute :b
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                validate(:a) do |a|
         | 
| 36 | 
            +
                  false
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                validate(:a, :b) do |a, b|
         | 
| 40 | 
            +
                  false
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
              spec do
         | 
| 45 | 
            +
                va = WithoutMessages.new
         | 
| 46 | 
            +
                @errors = va.errors
         | 
| 47 | 
            +
                @errors == { :a => "is invalid", [:a, :b] => "is invalid" }
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: va
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.2.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Federico Iachetti
         | 
| @@ -68,6 +68,7 @@ files: | |
| 68 68 | 
             
            - Rakefile
         | 
| 69 69 | 
             
            - lib/va.rb
         | 
| 70 70 | 
             
            - lib/va/version.rb
         | 
| 71 | 
            +
            - spec/errors_spec.rb
         | 
| 71 72 | 
             
            - spec/spec_helper.rb
         | 
| 72 73 | 
             
            - spec/va_spec.rb
         | 
| 73 74 | 
             
            - spec/validations_spec.rb
         | 
| @@ -97,6 +98,7 @@ signing_key: | |
| 97 98 | 
             
            specification_version: 4
         | 
| 98 99 | 
             
            summary: Minimalistic framework agnostic Validator.
         | 
| 99 100 | 
             
            test_files:
         | 
| 101 | 
            +
            - spec/errors_spec.rb
         | 
| 100 102 | 
             
            - spec/spec_helper.rb
         | 
| 101 103 | 
             
            - spec/va_spec.rb
         | 
| 102 104 | 
             
            - spec/validations_spec.rb
         |