preform 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5dc370e793ab96fc75b18581696c8b174cf9fb7a
4
+ data.tar.gz: 0662d9ce4610cba517b489f53b5b6003479d2be1
5
+ SHA512:
6
+ metadata.gz: e2c644425393b44b72c9ab87e3a4c41c658867353ba22192cfb0556dbff1d9818cbf00c641a4e7c3c54efa9863a341091154ed303c8a56439e54a0c201f12cdf
7
+ data.tar.gz: '08c297085a3b1ee7a5055ddcdee4bf7037ca0f865b6ff8329e07f62a358845b77f0630286315600913949e89b562dd58b9735ae31c9f048ed536328e3ebf8e90'
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .gs/
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ preform (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ clap (1.0.0)
10
+ cutest (1.2.3)
11
+ clap
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ cutest
18
+ preform!
19
+
20
+ BUNDLED WITH
21
+ 1.15.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Steven Weiss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Preform
2
+
3
+ A small library for validating user input. It includes mixin,
4
+ Validatable, and two opinionated classes that include it: Form, and Field.
5
+
6
+ ## Usage
7
+
8
+ ### Installation
9
+
10
+ `gem install preform`
11
+
12
+ ### API
13
+
14
+ #### Validatable
15
+
16
+ `validate`: Template command method that needs to be implemented. Runs to determine errors.
17
+
18
+ `valid?`: Query to see if any errors exist.
19
+
20
+ `errors`: Returns the errors hash.
21
+
22
+ `assert`: Tests a value to see if an error should be added or not. Always returns a boolean so asserts can be nested.
23
+
24
+ #### Form
25
+
26
+ `params`: Raw params given to the form.
27
+
28
+ `attributes`: Hook that allows you to transform params for use in your application. By default returns raw params.
29
+
30
+ #### Field
31
+
32
+ `key`: Reader for the form key, used when merging errors into a form.
33
+
34
+ `value`: Raw value to validate.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ task :install do
2
+ sh 'mkdir -p .gs & gs bundle install --system'
3
+ end
4
+
5
+ task :test, :file_name do |t, args|
6
+ file_name = args[:file_name] || '*'
7
+
8
+ sh "gs cutest -r ./test/#{file_name}_test.rb"
9
+ end
data/lib/preform.rb ADDED
@@ -0,0 +1,59 @@
1
+ module Preform
2
+ VERSION = '0.1.0'
3
+
4
+ module Validatable
5
+ def validate
6
+ raise NotImplementedError, "validate must be implemented on #{self.class}"
7
+ end
8
+
9
+ def valid?
10
+ errors.empty?
11
+ end
12
+
13
+ def errors
14
+ unless defined?(@errors)
15
+ @errors = default_errors
16
+
17
+ validate
18
+ end
19
+
20
+ @errors
21
+ end
22
+
23
+ def default_errors
24
+ Hash.new { |hash, key| hash[key] = [] }
25
+ end
26
+
27
+ def assert(predicate, key, msg)
28
+ return if predicate
29
+
30
+ errors[key.to_sym] << msg
31
+
32
+ false # Allows assertations to be nested.
33
+ end
34
+ end
35
+
36
+ class Field
37
+ include Validatable
38
+
39
+ attr_reader :key, :value
40
+
41
+ def initialize(key, value)
42
+ @key, @value = key, value
43
+ end
44
+ end
45
+
46
+ class Form
47
+ include Validatable
48
+
49
+ attr_reader :params
50
+
51
+ def initialize(params)
52
+ @params = params
53
+ end
54
+
55
+ def attributes
56
+ params
57
+ end
58
+ end
59
+ end
data/preform.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ require_relative "./lib/preform"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "preform"
5
+ s.summary = "Preform"
6
+ s.version = Preform::VERSION
7
+ s.authors = ["Steve Weiss"]
8
+ s.email = ["weissst@mail.gvsu.edu"]
9
+ s.homepage = "https://github.com/sirscriptalot/preform"
10
+ s.license = "MIT"
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_development_dependency "cutest"
14
+ end
@@ -0,0 +1,72 @@
1
+ require 'digest'
2
+
3
+ require_relative '../lib/preform'
4
+
5
+ class EmailField < Preform::Field
6
+ def validate
7
+ assert value.include?('@'), key, 'invalid format'
8
+ end
9
+ end
10
+
11
+ class PasswordField < Preform::Field
12
+ def validate
13
+ assert value.length > 6, key, 'too short'
14
+ end
15
+
16
+ def digest
17
+ Digest::MD5.digest(value) # pretend this is secure
18
+ end
19
+ end
20
+
21
+ class AccountForm < Preform::Form
22
+ attr_reader :email, :password
23
+
24
+ def validate
25
+ @email = EmailField.new(:email, params['email'])
26
+ @password = PasswordField.new(:password, params['password'])
27
+
28
+ errors.merge!(email.errors)
29
+ errors.merge!(password.errors)
30
+ end
31
+
32
+ def attributes
33
+ { email: email.value, password_digest: password.digest }
34
+ end
35
+ end
36
+
37
+ def assert_valid(form)
38
+ assert form.valid?, "#{form} is not valid, it should be"
39
+ end
40
+
41
+ def refute_valid(form)
42
+ assert !form.valid?, "#{form} is valid, it should not be"
43
+ end
44
+
45
+ def assert_errors_includes(form, key, msg)
46
+ assert form.errors[key].include?(msg), "#{form.errors[key]} does not include #{msg}, it should"
47
+ end
48
+
49
+ setup do
50
+ invalid_params = { 'email' => '', 'password' => '123456'}
51
+
52
+ AccountForm.new(invalid_params)
53
+ end
54
+
55
+ test 'invalid form' do |form|
56
+ refute_valid form
57
+
58
+ assert_errors_includes form, :email, 'invalid format'
59
+
60
+ assert_errors_includes form, :password, 'too short'
61
+ end
62
+
63
+ setup do
64
+ valid_params = { 'email' => 'something@example.com', 'password' => 'sosecret' }
65
+
66
+ AccountForm.new(valid_params)
67
+ end
68
+
69
+ test 'valid form' do |form|
70
+ assert_valid form
71
+ end
72
+
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: preform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Weiss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - weissst@mail.gvsu.edu
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/preform.rb
41
+ - preform.gemspec
42
+ - test/perform_test.rb
43
+ homepage: https://github.com/sirscriptalot/preform
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.11
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Preform
67
+ test_files: []