active_validator 1.0.2

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZjhjNTI0ZTZjYmIxZjhhNTgyNDNiOGI1Mjc5YzZlNzNjNDA0NDNhNQ==
5
+ data.tar.gz: !binary |-
6
+ NDk1M2QwZmQ3YjgwMGYxMTk4MzM5ZDU2MmZkMzkzMmM3NTM1NTZmZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MmVkZDM0YTczOGY4Yzc3YzI2MmU2NzI1MzA0OTlhZjFjZTliMzY4YTFkMGZh
10
+ ZThlZDM4OWMwZGFhODRjYzk4Njc4ZmFhM2U4YTliNzk5MmFiMWNhOTBmYTQw
11
+ OTMxM2EyMGZlYTc1YTllMmMyMjMyYTc1ODkyZDE0MjJlMzI3NTE=
12
+ data.tar.gz: !binary |-
13
+ ZDI5YzU0OGM4ZGI4NDkwNjk3ZDgxYTRlZDY2ZmQ4NDIxMWFhY2IyNDQwYzdh
14
+ ZGM3MmNhZDFiZmRhZThmY2M5ODA1YjljNGUzOWExMzBiYmNlNjBlYjA5OTMx
15
+ YmJhZWY1Zjk3Y2IzYzFlMzBiOGNhMDIwMGI4Zjc5ZGI1NDk1YzY=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nathan Pearson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ActiveValidator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_validator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_validator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_validator"
8
+ spec.version = ActiveValidator::VERSION
9
+ spec.authors = ["Nathan Pearson"]
10
+ spec.email = ["npearson72@gmail.com"]
11
+ spec.description = "Create Rails active record style validation in your non-Rails app"
12
+ spec.summary = "Lightweight client app validators"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "activerecord"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "strong_parameters"
26
+ end
@@ -0,0 +1,2 @@
1
+ require "active_validator/version"
2
+ require 'active_validator/base'
@@ -0,0 +1,37 @@
1
+ module ActiveValidator
2
+ class Base
3
+ include ActiveModel::Model
4
+
5
+ attr_accessor :safe_params
6
+
7
+ def initialize(params)
8
+ @safe_params = delete_unsafe_params(params)
9
+ setup_attributes(safe_params)
10
+ end
11
+
12
+ ###################
13
+ #= Class Methods =#
14
+ ###################
15
+ def self.safe_params(*args)
16
+ @@safe_params = args
17
+ end
18
+
19
+ ######################
20
+ #= Instance Methods =#
21
+ ######################
22
+ def delete_unsafe_params(params)
23
+ params.permit @@safe_params
24
+ end
25
+
26
+ def setup_attributes(params)
27
+ params.each do |k,v|
28
+ self.class.send(:attr_accessor, k)
29
+ instance_variable_set(:"@#{k}", v)
30
+ end
31
+ end
32
+
33
+ def error_messages
34
+ { error: errors.full_messages.uniq } unless self.valid?
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveValidator
2
+ VERSION = "1.0.2"
3
+ end
@@ -0,0 +1,16 @@
1
+ # Create a class that is namespaced with the ActiveValidator module and
2
+ # is subclassed by ActiveValidator::Base and place it in a directory
3
+ # that your application will initialize. In a client based Rails app
4
+ # that will not use a database, you can place these into .lib/validatable
5
+ #
6
+ # module ActiveValidator
7
+ # class SampleValidator < ActiveValidator::Base
8
+
9
+ # # Setup your validators using active_record methods.
10
+ # validates :email, presence: true
11
+
12
+ # # Define params that are white-listed. These will be enforced
13
+ # # using Rails safe parameters.
14
+ # safe_params :email, :password
15
+ # end
16
+ # end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveValidator::Base do
4
+ before do
5
+ @params = { foo: 123, bar: 456 }
6
+ @params.stub(:permit).and_return(@params)
7
+ end
8
+
9
+ describe 'core functionality' do
10
+ it 'should return false when validation rules are not met' do
11
+ v = ActiveValidator::MySubClass.new(@params)
12
+ v.foo = ''
13
+ expect(v.valid?).to eq(false)
14
+ end
15
+
16
+ it 'should return true when validation rules are met' do
17
+ v = ActiveValidator::MySubClass.new(@params)
18
+ expect(v.valid?).to eq(true)
19
+ end
20
+ end
21
+
22
+ describe 'setup_attributes()' do
23
+ it 'should create custom attr_accessor methods in subclass' do
24
+ v = ActiveValidator::MySubClass.new(@params)
25
+ expect(v.respond_to?(:foo)).to eq(true)
26
+ end
27
+
28
+ it 'should assign values from params to custom attr_accessors subclass' do
29
+ v = ActiveValidator::MySubClass.new(@params)
30
+ expect(v.foo).to eq(123)
31
+ end
32
+ end
33
+
34
+ describe 'error_messages()' do
35
+ it 'should return a properly formatted error hash if errors exist' do
36
+ v = ActiveValidator::MySubClass.new(@params)
37
+ v.foo = ''
38
+ v.valid?
39
+ expect(v.error_messages).to eq({:error=>["Foo can't be blank"]})
40
+ end
41
+
42
+ it 'should return nil if no errors exist' do
43
+ v = ActiveValidator::MySubClass.new(@params)
44
+ v.valid?
45
+ expect(v.error_messages).to eq(nil)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ require 'active_record'
7
+ require 'active_validator'
8
+ require 'support/custom_validators'
9
+
10
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,8 @@
1
+ module ActiveValidator
2
+ class MySubClass < ActiveValidator::Base
3
+
4
+ validates :foo, presence: true
5
+
6
+ safe_params :foo, :bar
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Pearson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: strong_parameters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Create Rails active record style validation in your non-Rails app
84
+ email:
85
+ - npearson72@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - active_validator.gemspec
97
+ - lib/active_validator.rb
98
+ - lib/active_validator/base.rb
99
+ - lib/active_validator/version.rb
100
+ - lib/sample_validator.rb
101
+ - spec/active_validator/base_spec.rb
102
+ - spec/spec_helper.rb
103
+ - spec/support/custom_validators.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.1.10
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Lightweight client app validators
128
+ test_files:
129
+ - spec/active_validator/base_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/support/custom_validators.rb
132
+ has_rdoc: