converters_before_validation 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63341ad4fd640864f114a55de5f24fe4cf07f101f6f22c35186f4645c5f2576f
4
+ data.tar.gz: fd7f1da7981b581c8a7d7f291ec30e065dc49b04549429a27f94de727f2c9dea
5
+ SHA512:
6
+ metadata.gz: 193c8a070e0b0ad3d0d30abc3260911b1269ae8f5d67d0fe181fb55396a5dcbc5ff3eee61d1727d889f2dfc3ac64ca6822540bfff38850e821bc10647bea778f
7
+ data.tar.gz: a02b046458b30bdf00339b99d8bbdeff4e93a6f997749822f3c6c3922be10f14da38e815c2d509f3272ce9dc021115280fbc13712069055bd8f52a940a5a7655
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Kanaikin Slava
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.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # ConvertersBeforeValidation
2
+
3
+ string_converter_before_validation adds before_validation callback, which apply necessary function to attributes.
4
+
5
+ There are some derivatives:
6
+
7
+ * squish_before_validation_for
8
+ * downcase_before_validation_for
9
+ * upcase_before_validation_for
10
+
11
+ ## Usage
12
+
13
+ Let we have model Article with some fields: author_name, contact_email, title, camel_tag.
14
+
15
+ ```ruby
16
+ # app/models/article.rb
17
+ class Article < ApplicationRecord
18
+
19
+ validates :contact_email, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "Not email format" }
20
+
21
+ end
22
+ ```
23
+
24
+ Let's take fields' values from some not accurate input:
25
+
26
+ ```
27
+ >> article = Article.new(author_name: ' Boss ', contact_email: ' Boss@ExAmple.Org', title: ' hello ', camel_tag: 'hello_world')
28
+
29
+ >> article.valid?
30
+ => false
31
+
32
+ >> article.author_name
33
+ => ' Boss '
34
+
35
+ >> article.contact_email
36
+ => ' Boss@ExAmple.Org'
37
+
38
+ >> article.title
39
+ => ' hello '
40
+
41
+ >> article.camel_tag
42
+ => 'hello_world'
43
+
44
+ ```
45
+
46
+ So, we can want to format values before save it to our base:
47
+ contact_email = contact_email.squish.downcase
48
+ author_name = author_name.squish
49
+ title = title.squish.upcase
50
+ camel_tag = camel_tag.camelcase
51
+
52
+ This gem's functions are usefull for it. They add before_validation callbacks to model and do this job for us.
53
+
54
+ ```ruby
55
+ # app/models/article.rb
56
+ class Article < ApplicationRecord
57
+
58
+ downcase_before_validation_for :contact_email
59
+
60
+ squish_before_validation_for :author_name, :contact_email, :title
61
+
62
+ upcase_before_validation_for :title
63
+
64
+ # and another string function
65
+ string_converter_before_validation :camelcase, :camel_tag
66
+
67
+ validates :contact_email, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "Not email format" }
68
+
69
+ end
70
+ ```
71
+
72
+ ```
73
+ >> article = Article.new(author_name: ' Boss ', contact_email: ' Boss@ExAmple.Org', title: ' hello ', camel_tag: 'hello_world')
74
+
75
+ >> article.valid?
76
+ => true
77
+
78
+ >> article.author_name
79
+ => 'Boss'
80
+
81
+ >> article.contact_email
82
+ => 'boss@example.org'
83
+
84
+ >> article.title
85
+ => 'HELLO'
86
+
87
+ >> article.camel_tag
88
+ => 'HelloWorld'
89
+
90
+ ```
91
+
92
+ ## Installation
93
+
94
+ Add this line to your application's Gemfile:
95
+
96
+ ```ruby
97
+ gem 'converters_before_validation'
98
+ ```
99
+
100
+ And then execute:
101
+
102
+ ```bash
103
+ bundle
104
+ ```
105
+
106
+ Or install it yourself as:
107
+
108
+ ```bash
109
+ gem install converters_before_validation
110
+ ```
111
+
112
+ ## Contributing
113
+ Contribution directions go here.
114
+
115
+ ## License
116
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
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 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ConvertersBeforeValidation'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,48 @@
1
+ module ConvertersBeforeValidation
2
+ ActiveSupport.on_load(:active_record) do
3
+ # Adds private callback before_validation, which apply necessary function
4
+ # to attributes.
5
+ #
6
+ # Usage:
7
+ # string_converter_before_validation :camelcase, :field1, :field2, :field3
8
+ #
9
+ # Parameters:
10
+ # formatter_function::
11
+ # Name of String functions: downcase, squish, upcase, camelcase, ...
12
+ # (Example: 'camelcase', :camelcase, "camelcase")
13
+ #
14
+ # attributes_list::
15
+ # List of attributes, which values will be converted with formatter_function.
16
+ def self.string_converter_before_validation(formatter_function, *attributes_list)
17
+ callback_name = "callback_converters_before_validation_string_#{formatter_function}"
18
+
19
+ before_validation callback_name.to_sym
20
+
21
+ class_eval(%(
22
+ def #{callback_name}
23
+ #{attributes_list.map { |aa| " self.#{aa.to_s.squish} = #{aa.to_s.squish}.to_s.#{formatter_function};" }.join("\n")}
24
+ end
25
+ ))
26
+
27
+ private callback_name.to_s
28
+ end
29
+
30
+ # Usage:
31
+ # downcase_before_validation_for :email, :login
32
+ def self.downcase_before_validation_for(*attributes_list)
33
+ string_converter_before_validation :downcase, *attributes_list
34
+ end
35
+
36
+ # Usage:
37
+ # squish_before_validation_for :name, :surname, :email, :login
38
+ def self.squish_before_validation_for(*attributes_list)
39
+ string_converter_before_validation :squish, *attributes_list
40
+ end
41
+
42
+ # Usage:
43
+ # upcase_before_validation_for :title
44
+ def self.upcase_before_validation_for(*attributes_list)
45
+ string_converter_before_validation :upcase, *attributes_list
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module ConvertersBeforeValidation
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :converters_before_validation do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: converters_before_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Kanaikin Slava
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 7.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 5.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 7.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: activerecord
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 5.0.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: 7.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 5.0.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: 7.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rails
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 6.0.0.rc1
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: 6.0.0.rc1
67
+ - !ruby/object:Gem::Dependency
68
+ name: sqlite3
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ description: 'Adds before_validation callback, which apply necessary function to attributes.
82
+ There are some derivatives: squish_before_validation_for, downcase_before_validation_for,
83
+ upcase_before_validation_for.'
84
+ email:
85
+ - sportix@gotar.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/converters_before_validation.rb
94
+ - lib/converters_before_validation/version.rb
95
+ - lib/tasks/converters_before_validation_tasks.rake
96
+ homepage: https://github.com/slavik117/converters_before_validation
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.0.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Formatters for ActiveRecord attributes by before_validation callback
119
+ test_files: []