weak_parameters 0.0.4 → 0.0.5
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/README.md +2 -2
 - data/lib/weak_parameters/base_validator.rb +25 -1
 - data/lib/weak_parameters/version.rb +1 -1
 - data/spec/dummy/app/controllers/recipes_controller.rb +2 -2
 - data/spec/requests/recipes_spec.rb +22 -0
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 574d6b069ecee483c92abe98a1f0aeefc6c77ce1
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 3f69d7e525b372617d94ab264dcaaa69602943df
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: cc55b3b93a4fd17d76fe10d17c011158c69439aa2d7f79c512588d7439122542014bb7372096e583e8f1db739ff566ef95c476757e4c8398d332c829798ff376
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: f1577e70e84ffdbe6bd96dd03cf85cae30f733736da65628d2e2f387ea96a73c8a9ed7484f0b4e1ea77829c92b6b1a41b3248b108d5015898483d2c7cae63bef
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -11,8 +11,8 @@ gem "weak_parameters" 
     | 
|
| 
       11 
11 
     | 
    
         
             
            # WeakParameters provides `validates` class method to define validations.
         
     | 
| 
       12 
12 
     | 
    
         
             
            class RecipesController < ApplicationController
         
     | 
| 
       13 
13 
     | 
    
         
             
              validates :create do
         
     | 
| 
       14 
     | 
    
         
            -
                string :name, required: true
         
     | 
| 
       15 
     | 
    
         
            -
                integer :type
         
     | 
| 
      
 14 
     | 
    
         
            +
                string :name, required: true, except: ["charlie", "dave"]
         
     | 
| 
      
 15 
     | 
    
         
            +
                integer :type, only: "1".."3"
         
     | 
| 
       16 
16 
     | 
    
         
             
              end
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
18 
     | 
    
         
             
              def create
         
     | 
| 
         @@ -9,7 +9,7 @@ module WeakParameters 
     | 
|
| 
       9 
9 
     | 
    
         
             
                end
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         
             
                def validate
         
     | 
| 
       12 
     | 
    
         
            -
                  raise_error  
     | 
| 
      
 12 
     | 
    
         
            +
                  raise_error unless valid?
         
     | 
| 
       13 
13 
     | 
    
         
             
                end
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
       15 
15 
     | 
    
         
             
                def required?
         
     | 
| 
         @@ -22,6 +22,19 @@ module WeakParameters 
     | 
|
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
                private
         
     | 
| 
       24 
24 
     | 
    
         | 
| 
      
 25 
     | 
    
         
            +
                def valid?
         
     | 
| 
      
 26 
     | 
    
         
            +
                  case
         
     | 
| 
      
 27 
     | 
    
         
            +
                  when required? && nil?
         
     | 
| 
      
 28 
     | 
    
         
            +
                    false
         
     | 
| 
      
 29 
     | 
    
         
            +
                  when exist? && invalid_type?
         
     | 
| 
      
 30 
     | 
    
         
            +
                    false
         
     | 
| 
      
 31 
     | 
    
         
            +
                  when exist? && exceptional?
         
     | 
| 
      
 32 
     | 
    
         
            +
                    false
         
     | 
| 
      
 33 
     | 
    
         
            +
                  else
         
     | 
| 
      
 34 
     | 
    
         
            +
                    true
         
     | 
| 
      
 35 
     | 
    
         
            +
                  end
         
     | 
| 
      
 36 
     | 
    
         
            +
                end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
       25 
38 
     | 
    
         
             
                def nil?
         
     | 
| 
       26 
39 
     | 
    
         
             
                  value.nil?
         
     | 
| 
       27 
40 
     | 
    
         
             
                end
         
     | 
| 
         @@ -30,6 +43,17 @@ module WeakParameters 
     | 
|
| 
       30 
43 
     | 
    
         
             
                  !nil?
         
     | 
| 
       31 
44 
     | 
    
         
             
                end
         
     | 
| 
       32 
45 
     | 
    
         | 
| 
      
 46 
     | 
    
         
            +
                def exceptional?
         
     | 
| 
      
 47 
     | 
    
         
            +
                  case
         
     | 
| 
      
 48 
     | 
    
         
            +
                  when options[:only].try(:exclude?, value)
         
     | 
| 
      
 49 
     | 
    
         
            +
                    true
         
     | 
| 
      
 50 
     | 
    
         
            +
                  when options[:except].try(:include?, value)
         
     | 
| 
      
 51 
     | 
    
         
            +
                    true
         
     | 
| 
      
 52 
     | 
    
         
            +
                  else
         
     | 
| 
      
 53 
     | 
    
         
            +
                    false
         
     | 
| 
      
 54 
     | 
    
         
            +
                  end
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
       33 
57 
     | 
    
         
             
                def value
         
     | 
| 
       34 
58 
     | 
    
         
             
                  params[key]
         
     | 
| 
       35 
59 
     | 
    
         
             
                end
         
     | 
| 
         @@ -78,6 +78,28 @@ describe "Recipes" do 
     | 
|
| 
       78 
78 
     | 
    
         
             
                  end
         
     | 
| 
       79 
79 
     | 
    
         
             
                end
         
     | 
| 
       80 
80 
     | 
    
         | 
| 
      
 81 
     | 
    
         
            +
                context "with invalid param to :only condition" do
         
     | 
| 
      
 82 
     | 
    
         
            +
                  before do
         
     | 
| 
      
 83 
     | 
    
         
            +
                    params[:type] = 0
         
     | 
| 
      
 84 
     | 
    
         
            +
                  end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                  it "returns 400" do
         
     | 
| 
      
 87 
     | 
    
         
            +
                    post "/recipes", params
         
     | 
| 
      
 88 
     | 
    
         
            +
                    response.status.should == 400
         
     | 
| 
      
 89 
     | 
    
         
            +
                  end
         
     | 
| 
      
 90 
     | 
    
         
            +
                end
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                context "with invalid param to :except condition" do
         
     | 
| 
      
 93 
     | 
    
         
            +
                  before do
         
     | 
| 
      
 94 
     | 
    
         
            +
                    params[:name] = "invalid"
         
     | 
| 
      
 95 
     | 
    
         
            +
                  end
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                  it "returns 400" do
         
     | 
| 
      
 98 
     | 
    
         
            +
                    post "/recipes", params
         
     | 
| 
      
 99 
     | 
    
         
            +
                    response.status.should == 400
         
     | 
| 
      
 100 
     | 
    
         
            +
                  end
         
     | 
| 
      
 101 
     | 
    
         
            +
                end
         
     | 
| 
      
 102 
     | 
    
         
            +
             
     | 
| 
       81 
103 
     | 
    
         
             
                context "with valid condition", :autodoc do
         
     | 
| 
       82 
104 
     | 
    
         
             
                  it "creates a new recipe" do
         
     | 
| 
       83 
105 
     | 
    
         
             
                    post "/recipes", params
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: weak_parameters
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.5
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Ryo Nakamura
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2013-06- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2013-06-18 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: rails
         
     |