digget 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 844819201cdd222faf30bdd0b8abf0cc2a63b24c
4
+ data.tar.gz: 71c0f7d24f13af6fdc051f925da254902d1a9e6f
5
+ SHA512:
6
+ metadata.gz: 6f88cfb2e064c08028ef62d7e72a976e905b2dbdabe509772e66778bd9626d0bb5a58be3d7b4e7e0111a41165cab365a5cd23b686db42e3f026176470681bd1d
7
+ data.tar.gz: 13aeb6030bc94ca3d26d58059e86ec0c209d6e79278fa5b29506360761e8c87b29cce1a788f342f7a4039891b4f3261af09ffce6c44fce310866bcf73c232b65
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Pieterjan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ [![codecov](https://codecov.io/gh/pielambr/digget/branch/master/graph/badge.svg)](https://codecov.io/gh/pielambr/digget)
2
+ [![Build Status](https://travis-ci.org/pielambr/digget.svg?branch=master)](https://travis-ci.org/pielambr/digget)
3
+ # Digget
4
+ Short description and motivation.
5
+
6
+ ## Usage
7
+ How to use my plugin.
8
+
9
+ ## Installation
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'digget'
14
+ ```
15
+
16
+ And then execute:
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+ ```bash
23
+ $ gem install digget
24
+ ```
25
+
26
+ ## Contributing
27
+ Contribution directions go here.
28
+
29
+ ## License
30
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ require 'yard'
11
+
12
+ YARD::Rake::YardocTask.new do |t|
13
+ t.files = ['lib/**/*.rb']
14
+ t.options = ['--private']
15
+ t.stats_options = ['--list-undoc']
16
+ end
17
+
18
+ task default: :spec
@@ -0,0 +1,6 @@
1
+ require 'digget/validator'
2
+ require 'i18n'
3
+
4
+ module Digget
5
+ I18n.load_path += Dir["lib/translations/digget.yml"]
6
+ end
@@ -0,0 +1,115 @@
1
+ require 'i18n'
2
+
3
+ module Digget
4
+ # This class is the validator superclass containing the logic to cast
5
+ # the parameters and verify the options that are defined on them
6
+ class Validator
7
+ def initialize(params, object = nil)
8
+ @object = object
9
+ @casted_params = {}
10
+ @errors = []
11
+ @params = params
12
+ end
13
+
14
+ # This method returns an array with all the errors during validation
15
+ # @return [Array] An array containing the validation errors
16
+ attr_reader :errors
17
+
18
+ # This method returns a hash with the parameters casted to their requested type
19
+ # @return [Hash] A hash with as a key the parameter symbol and the casted value as value
20
+ attr_reader :casted_params
21
+
22
+ # This method verifies one of the parameters with the provided options
23
+ # @param name [Symbol] The name of the param in the `params` hash
24
+ # @param type [Class] Type that the parameter should be casted to
25
+ # @param options [Hash] Validation options for this parameter
26
+ def verify(name, type, options = {})
27
+ casted_param = cast(@params[name], type)
28
+ return unless @errors.empty?
29
+ @casted_params[name] = casted_param
30
+ verify_options(casted_param, name, options)
31
+ casted_param
32
+ end
33
+
34
+ def filter(name, type, options = {})
35
+ param = verify(name, type, options)
36
+ return object unless @errors.empty?
37
+ filter_options(param, name, options)
38
+ end
39
+
40
+ # This method is a helper method to determine whether the validation of the parameters was successful
41
+ # @return `true` if there were no errors during parameter checking, `false` otherwise
42
+ def valid?
43
+ @errors.empty?
44
+ end
45
+
46
+ private
47
+
48
+ # This method casts the parameter to the requested type if possible
49
+ # @param param [String] Parameter that is about to get casted
50
+ # @param type [Class] The type that the parameter should be after this method
51
+ # @return Returns a casted version of the provided parameter
52
+ def cast(param, type)
53
+ if param.nil?
54
+ return nil
55
+ elsif type == Integer
56
+ return Integer(param)
57
+ elsif type == String
58
+ return String(param)
59
+ elsif type == Float
60
+ return Float(param)
61
+ elsif type == Date
62
+ return Date.parse(param)
63
+ elsif type == Time
64
+ return Time.parse(param)
65
+ end
66
+ rescue ArgumentError
67
+ errors.append(I18n.t('digget.cast', param: param, type: type))
68
+ end
69
+
70
+ # This method verifies the parameters and adds errors when a parameter
71
+ # doesn't conform with the provided verification options
72
+ # @param param The parameter value that needs to be verified
73
+ # @param name [String] The name of the parameter, used to be displayed in error messages
74
+ # @param options [Hash] The verification options for the provided parameter
75
+ def verify_options(param, name, options = {})
76
+ options.each do |key, value|
77
+ if key == :required && value
78
+ next unless param.nil?
79
+ @errors.append(I18n.t('digget.required', name: name))
80
+ elsif key == :min && !param.nil?
81
+ next unless param < value
82
+ @errors.append(I18n.t('digget.min', name: name, value: value))
83
+ elsif key == :max && !param.nil?
84
+ next unless param > value
85
+ @errors.append(I18n.t('digget.max', name: name, value: value))
86
+ elsif key == :min_length && !param.nil?
87
+ next unless param.length < value
88
+ @errors.append(I18n.t('digget.min_length', name: name, value: value))
89
+ elsif key == :max_length && !param.nil?
90
+ next unless param.length > value
91
+ @errors.append(I18n.t('digget.max_length', name: name, value: value))
92
+ elsif key == :length && !param.nil?
93
+ next unless param.length == value
94
+ @errors.append(I18n.t('digget.length', name: name, value: value))
95
+ end
96
+ end
97
+ end
98
+
99
+ def filter_options(param, name, options = {})
100
+ name = options.key?(:column) ? options[:column] : name
101
+ return unless @object && options.key?(:filter)
102
+ filter = options[:filter]
103
+ if filter == :min
104
+ @object = @object.where(@object.arel_table[name].gteq(param))
105
+ elsif filter == :max
106
+ @object = @object.where(@object.arel_table[name].lteq(param))
107
+ elsif filter == :equal
108
+ @object = @object.where(@object.arel_table[name].eq(param))
109
+ elsif filter == :not_equal
110
+ @object = @object.where(@object.arel_table[name].not_eq(param))
111
+ end
112
+ end
113
+ end
114
+
115
+ end
@@ -0,0 +1,3 @@
1
+ module Digget
2
+ VERSION = '0.1.0'
3
+ end
File without changes
@@ -0,0 +1,9 @@
1
+ en:
2
+ digget:
3
+ cast: "The parameter `%{param}` cannot be converted to type `%{type}`"
4
+ required: "The parameter `%{name}` is required"
5
+ min: "The parameter `%{name}` should be at least %{value}"
6
+ max: "The parameter `%{name}` can be %{value} at most"
7
+ min_length: "The parameter `%{name}` should have a length of at least %{value}"
8
+ max_length: "The parameter `%{name}` should have a length of %{value} at most"
9
+ length: "The parameter `%{name}` should have a length of %{value}"
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digget
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pieterjan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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: rspec-rails
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: byebug
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: yard
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
+ - !ruby/object:Gem::Dependency
84
+ name: codecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Description of Digget.
112
+ email:
113
+ - me@pielambr.be
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - MIT-LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - lib/digget.rb
122
+ - lib/digget/validator.rb
123
+ - lib/digget/version.rb
124
+ - lib/tasks/digget_tasks.rake
125
+ - lib/translations/digget.yml
126
+ homepage: https://www.pielambr.be
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.6.12
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Summary of Digget.
150
+ test_files: []