hexx-validators 0.0.1

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: 8e2e8c899b341405b70bf30cc88cd931852f7e6d
4
+ data.tar.gz: 555bbe83dd159db2b846eef787f6be5941e97d8c
5
+ SHA512:
6
+ metadata.gz: 9fdf076348d82f53607d0c2c445a4dde412962c112231e7108d409bacf7cf2f2623b39c9b2109d645466c8734c73c15d3f937169381316775a6b84c88774675e
7
+ data.tar.gz: a13cfe553e1f24a480d25ba02b66ee77530ac7cade9575783dea90103f51a1d1f73cb15ee86504fe50c6f66f198d7f16dde215f8b256b5ec24b7a8d5fd947313
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ service_name: travis-ci
data/.metrics ADDED
@@ -0,0 +1,5 @@
1
+ # Settings for metric_fu and its packages are collected in the `config/metrics`
2
+ # and loaded by the Hexx::Suit::Metrics::MetricFu.
3
+
4
+ require "hexx-suit"
5
+ Hexx::Suit::Metrics::MetricFu.load
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ inherit_from: config/metrics/rubocop.yml
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ language: ruby
3
+ rvm:
4
+ - 2.1
5
+ - 2.2
data/.yardopts ADDED
@@ -0,0 +1,3 @@
1
+ --asset LICENSE
2
+ --exclude lib/hexx/validators/version.rb
3
+ --out doc/api
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ guard :rspec, cmd: "bundle exec rspec spec" do
4
+
5
+ watch("lib/hexx-validators.rb") { "spec" }
6
+
7
+ watch("lib/hexx/validators.rb") { "spec" }
8
+
9
+ watch %r{^lib/hexx/validators/(\w+)\.rb$} do |m|
10
+ "spec/tests/#{m[1]}_spec.rb"
11
+ end
12
+
13
+ watch(/^spec.+_spec\.rb$/)
14
+
15
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
16
+
17
+ watch("spec/spec_helper*.rb") { "spec" }
18
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2014 Andrew Kozin, https://github.com/nepalez
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,90 @@
1
+ # Hexx::Validators
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/hexx-validators.svg?style=flat)][gem]
4
+ [![Build Status](https://img.shields.io/travis/nepalez/hexx-validators/master.svg?style=flat)][travis]
5
+ [![Dependency Status](https://img.shields.io/gemnasium/nepalez/hexx-validators.svg?style=flat)][gemnasium]
6
+ [![Code Climate](https://img.shields.io/codeclimate/github/nepalez/hexx-validators.svg?style=flat)][codeclimate]
7
+ [![Coverage](https://img.shields.io/coveralls/nepalez/hexx-validators.svg?style=flat)][coveralls]
8
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
9
+
10
+ [MIT LICENSE]: file:./LICENSE
11
+ [codeclimate]: https://codeclimate.com/github/nepalez/hexx-validators
12
+ [coveralls]: https://coveralls.io/r/nepalez/hexx-validators
13
+ [gem]: https://rubygems.org/gems/hexx-validators
14
+ [gemnasium]: https://gemnasium.com/nepalez/hexx-validators
15
+ [travis]: https://travis-ci.org/nepalez/hexx-validators
16
+
17
+ The [Thor-based](https://github.com/erikhuda/thor) scaffolder of custom ActiveModel validators with unit test and its support files.
18
+
19
+ The module is the part of [hexx] scaffolders collection.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem "hexx-validators"
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ ```
32
+ $ bundle
33
+ ```
34
+
35
+ Or install it yourself as:
36
+
37
+ ```
38
+ $ gem install hexx-validators
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ ```ruby
44
+ require "hexx-validators"
45
+
46
+ Hexx::Validators::Generator.start %w(
47
+ in_the_past
48
+ -o allow_nil time_zone
49
+ -e not_in_the_past
50
+ )
51
+ ```
52
+
53
+ ### Arguments and Options
54
+
55
+ The generator takes one argument (the name of the validator, such as `in_the_past`) and two options:
56
+
57
+ * `-o` - the names of validator options (such as allow_nil etc.).
58
+ * `-e` - the names of error messages for I18n translations.
59
+
60
+ Depending on options the unit test spec will be pre-populated with combinations of options.
61
+
62
+ ### Provided Results
63
+
64
+ The generator creates:
65
+
66
+ * the validator (`app/validators/in_the_past.rb`)
67
+ by convention I follow, it will be namespaced correspondingly: `MyGem::Validators::InThePast`.
68
+ * the unit test specification `spec/tests/validators/in_the_past_spec.rb`.
69
+
70
+ ## Compatibility
71
+
72
+ Tested under MRI rubies >= 2.1. Rubies under 2.1 aren't supported.
73
+
74
+ Uses [RSpec] 3.0+ for testing and [hexx-suit] for dev/test tools collection.
75
+
76
+ ## Contributing
77
+
78
+ * Fork the project.
79
+ * Read the [STYLEGUIDE](config/metrics/STYLEGUIDE).
80
+ * Make your feature addition or bug fix.
81
+ * Add tests for it. This is important so I don't break it in a
82
+ future version unintentionally.
83
+ * Commit, do not mess with Rakefile or version
84
+ (if you want to have your own version, that is fine but bump version
85
+ in a commit by itself I can ignore when I pull)
86
+ * Send me a pull request. Bonus points for topic branches.
87
+
88
+ ## License
89
+
90
+ See [MIT LICENSE](LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require "bundler/setup"
4
+ rescue LoadError
5
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
6
+ exit
7
+ end
8
+
9
+ # Loads bundler tasks
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ # Loads the Hexx::Suit and its tasks
13
+ require "hexx-suit"
14
+ Hexx::Suit.install_tasks
15
+
16
+ # Sets the Hexx::Suit :test task to default
17
+ task default: "check:coverage"
@@ -0,0 +1,231 @@
1
+ = Ruby Style Guide
2
+
3
+ Adapted from Dan Kubb's Ruby Style Guide
4
+ https://github.com/dkubb/styleguide/blob/master/RUBY-STYLE
5
+
6
+ == Commiting:
7
+
8
+ * Write descriptive commit messages, following the pattern:
9
+
10
+ [TYPE] name
11
+
12
+ The message, describing the changes being made
13
+
14
+ * Use the types below to mark commits:
15
+
16
+ - FEATURE - for adding new features
17
+ - UPDATE - for backward-compatible changes;
18
+ - CHANGE - for backward-incompatible changes;
19
+ - BUG FIX - for fixing bugs;
20
+ - REFACTORING - for other changes of the code not affecting the API;
21
+ - OTHER - for changes in documentaton, metrics etc, not touching the code;
22
+ - VERSION - for new versions.
23
+
24
+ * Try to separate commits of different types (such as UPDATE and CHANGE).
25
+
26
+ * Try to separate various features from each other.
27
+
28
+ * Include specification to the same commit as the code.
29
+
30
+ * Run all tests before making a commit.
31
+ Never commit the code that breaks tests.
32
+
33
+ * Use metric (run `rake check`) before making a commit.
34
+
35
+ * Do refactoring before making a commit. Best writing is rewriting.
36
+
37
+ * Follow semantic versioning.
38
+
39
+ http://semver.org/
40
+
41
+ * For versions name the commit after a version number, following the pattern:
42
+
43
+ VERSION 1.0.0-rc2
44
+
45
+
46
+ == Formatting:
47
+
48
+ * Use UTF-8. Declare encoding in the first line of every file.
49
+
50
+ # encoding: utf-8
51
+
52
+ * Use 2 space indent, no tabs.
53
+
54
+ * Use Unix-style line endings.
55
+
56
+ * Use spaces around operators, after commas, colons and semicolons,
57
+ around { and before }.
58
+
59
+ * No spaces after (, [ and before ], ).
60
+
61
+ * Align `when` and `else` with `case`.
62
+
63
+ * Use an empty line before the return value of a method (unless it
64
+ only has one line), and an empty line between defs.
65
+
66
+ * Use empty lines to break up a long method into logical paragraphs.
67
+
68
+ * Keep lines fewer than 80 characters.
69
+
70
+ * Strip trailing whitespace.
71
+
72
+
73
+ == Syntax:
74
+
75
+ * Write for 2.0.
76
+
77
+ * Use double quotes
78
+
79
+ http://viget.com/extend/just-use-double-quoted-ruby-strings
80
+
81
+ * Use def with parentheses when there are arguments.
82
+
83
+ * Never use for, unless you exactly know why.
84
+
85
+ * Never use then, except in case statements.
86
+
87
+ * Use when x then ... for one-line cases.
88
+
89
+ * Use &&/|| for boolean expressions, and/or for control flow. (Rule
90
+ of thumb: If you have to use outer parentheses, you are using the
91
+ wrong operators.)
92
+
93
+ * Avoid double negation (!!), unless Null Objects are expected.
94
+
95
+ http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness
96
+
97
+ * Avoid multiline ?:, use if.
98
+
99
+ * Use {...} when defining blocks on one line. Use do...end for multiline
100
+ blocks.
101
+
102
+ * Avoid return where not required.
103
+
104
+ * Use ||= freely.
105
+
106
+ * Use OO regexps, and avoid =~ $0-9, $~, $` and $' when possible.
107
+
108
+ * Do not use Enumerable#inject when the "memo" object does not change between
109
+ iterations, use Enumerable#each_with_object instead (in ruby 1.9,
110
+ active_support and backports).
111
+
112
+ * Prefer ENV.fetch to ENV[] syntax.
113
+ Prefer block syntax for ENV.fetch to usage of the second argument.
114
+
115
+
116
+ == Naming:
117
+
118
+ * Use snake_case for methods.
119
+
120
+ * Use CamelCase for classes and modules. (Keep acronyms like HTTP,
121
+ RFC, XML uppercase.)
122
+
123
+ * Use SCREAMING_SNAKE_CASE for other constants.
124
+
125
+ * Do not use single letter variable names. Avoid uncommunicative names.
126
+
127
+ * Use consistent variable names. Try to keep the variable names close
128
+ to the object class name.
129
+
130
+ * Use names prefixed with _ for unused variables.
131
+
132
+ * When defining a predicate method that compares against another object of
133
+ a similar type, name the argument "other".
134
+
135
+ * Prefer map over collect, detect over find, select over find_all.
136
+
137
+ * Use def self.method to define singleton methods.
138
+
139
+ * Avoid alias when alias_method will do.
140
+
141
+
142
+ == Comments:
143
+
144
+ * Use YARD and its conventions for API documentation. Don't put an
145
+ empty line between the comment block and the def.
146
+
147
+ * Comments longer than a word are capitalized and use punctuation.
148
+ Use one space after periods.
149
+
150
+ * Avoid superfluous comments.
151
+
152
+
153
+ == Code structuring:
154
+
155
+ * Break code into packages, decoupled from the environment.
156
+
157
+ * Wrap packages into gems.
158
+
159
+ * Inject dependencies explicitly.
160
+ Leave all outer references on the border of any package. Inside
161
+ the package use internal references only.
162
+
163
+ * Follow SOLID principles.
164
+
165
+ http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
166
+
167
+ * Only give a method one purpose for existing. If you pass in a boolean
168
+ to a method, what you're saying is that this method has two different
169
+ behaviours. Just split it into two single purpose methods. If you have
170
+ to use the words "AND" or "OR" to describe what the method does it
171
+ probably does too much.
172
+
173
+ * Avoid long methods.
174
+ Try to keep them at no more than 6 lines long, and preferably 4 or less.
175
+
176
+ If sections of a method are logically separate by blank lines, then
177
+ that's probably a sign that those sections should be split into separate
178
+ methods.
179
+
180
+ * Avoid hashes-as-optional-parameters. Does the method do too much?
181
+
182
+ * Avoid long parameter lists.
183
+
184
+ * Add "global" methods to Kernel (if you have to) and make them private.
185
+
186
+ * Use OptionParser for parsing complex command line options and
187
+ ruby -s for trivial command line options.
188
+
189
+ * Avoid needless metaprogramming.
190
+
191
+ * Always freeze objects assigned to constants.
192
+
193
+
194
+ == General:
195
+
196
+ * Code in a functional way, avoid mutation when it makes sense.
197
+
198
+ * Try to have methods either return the state of the object and have
199
+ no side effects, or return self and have side effects. This is
200
+ otherwise known as Command-query separation (CQS):
201
+
202
+ http://en.wikipedia.org/wiki/Command-query_separation
203
+
204
+ * Do not mutate arguments unless that is the purpose of the method.
205
+
206
+ * Try following TRUE heuristics by Sandi Metz
207
+
208
+ http://designisrefactoring.com/2015/02/08/introducing-sandi-metz-true/
209
+
210
+ * Do not mess around in core classes when writing libraries.
211
+ Namespace your code inside the modules, or wrap core classes to
212
+ decorators of your own.
213
+
214
+ * Do not program defensively.
215
+
216
+ http://www.erlang.se/doc/programming_rules.shtml#HDR11
217
+
218
+ * Keep the code simple.
219
+
220
+ * Don't overdesign.
221
+
222
+ * Don't underdesign.
223
+
224
+ * Avoid bugs.
225
+
226
+ * Read other style guides and apply the parts that don't dissent with
227
+ this list.
228
+
229
+ * Be consistent.
230
+
231
+ * Use common sense.
@@ -0,0 +1,5 @@
1
+ ---
2
+ abc_max: "10"
3
+ line_length: "80"
4
+ no_doc: "y"
5
+ no_readme: "y"
@@ -0,0 +1,6 @@
1
+ ---
2
+ ignore_files:
3
+ - spec
4
+ - config
5
+ minimum_churn_count: 0
6
+ start_date: "1 year ago"
@@ -0,0 +1,2 @@
1
+ ---
2
+ minimum_score: 8
@@ -0,0 +1,15 @@
1
+ ---
2
+ folders: # The list of folders to be used by any metric.
3
+ - lib
4
+ - app
5
+ metrics: # The list of allowed metrics. The other metrics are disabled.
6
+ - cane
7
+ - churn
8
+ - flay
9
+ - flog
10
+ - reek
11
+ - roodi
12
+ - saikuro
13
+ format: html
14
+ output: tmp/metric_fu
15
+ verbose: false
@@ -0,0 +1,3 @@
1
+ ---
2
+ checkset: basic
3
+ output: "tmp/pippi/output.log"
@@ -0,0 +1 @@
1
+ ---
@@ -0,0 +1,24 @@
1
+ ---
2
+ AssignmentInConditionalCheck:
3
+ CaseMissingElseCheck:
4
+ ClassLineCountCheck:
5
+ line_count: 100
6
+ ClassNameCheck:
7
+ pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
8
+ ClassVariableCheck:
9
+ CyclomaticComplexityBlockCheck:
10
+ complexity: 4
11
+ CyclomaticComplexityMethodCheck:
12
+ complexity: 8
13
+ EmptyRescueBodyCheck:
14
+ ForLoopCheck:
15
+ MethodLineCountCheck:
16
+ line_count: 7
17
+ MethodNameCheck:
18
+ pattern: !ruby/regexp /^[\||\^|\&|\!]$|^[_a-z<>=\[|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/
19
+ ModuleLineCountCheck:
20
+ line_count: 100
21
+ ModuleNameCheck:
22
+ pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
23
+ ParameterNumberCheck:
24
+ parameter_count: 5
@@ -0,0 +1,75 @@
1
+ ---
2
+ # settings added by the 'hexx-suit' module
3
+ # output: "tmp/rubocop"
4
+ # format: "html"
5
+
6
+ AllCops:
7
+ Exclude:
8
+ - '**/db/schema.rb'
9
+
10
+ Lint/HandleExceptions:
11
+ Exclude:
12
+ - '**/*_spec.rb'
13
+
14
+ Lint/RescueException:
15
+ Exclude:
16
+ - '**/*_spec.rb'
17
+
18
+ Style/AccessorMethodName:
19
+ Exclude:
20
+ - '**/*_spec.rb'
21
+
22
+ Style/AsciiComments:
23
+ Enabled: false
24
+
25
+ Style/ClassAndModuleChildren:
26
+ Exclude:
27
+ - '**/*_spec.rb'
28
+
29
+ Style/Documentation:
30
+ Exclude:
31
+ - '**/version.rb'
32
+ - '**/*_spec.rb'
33
+
34
+ Style/EmptyLinesAroundBlockBody:
35
+ Enabled: false
36
+
37
+ Style/EmptyLinesAroundClassBody:
38
+ Enabled: false
39
+
40
+ Style/EmptyLinesAroundMethodBody:
41
+ Enabled: false
42
+
43
+ Style/EmptyLinesAroundModuleBody:
44
+ Enabled: false
45
+
46
+ Style/EmptyLineBetweenDefs:
47
+ Enabled: false
48
+
49
+ Style/FileName:
50
+ Enabled: false
51
+
52
+ Style/RaiseArgs:
53
+ EnforcedStyle: compact
54
+
55
+ Style/SingleLineMethods:
56
+ Exclude:
57
+ - '**/*_spec.rb'
58
+
59
+ Style/SingleSpaceBeforeFirstArg:
60
+ Enabled: false
61
+
62
+ Style/SpecialGlobalVars:
63
+ Exclude:
64
+ - '**/Gemfile'
65
+ - '**/*.gemspec'
66
+
67
+ Style/StringLiterals:
68
+ EnforcedStyle: double_quotes
69
+
70
+ Stype/StringLiteralsInInterpolation:
71
+ EnforcedStyle: double_quotes
72
+
73
+ Style/TrivialAccessors:
74
+ Exclude:
75
+ - '**/*_spec.rb'
@@ -0,0 +1,3 @@
1
+ ---
2
+ warn_cyclo: 4
3
+ error_cyclo: 6
@@ -0,0 +1,6 @@
1
+ ---
2
+ output: tmp/coverage
3
+ filters: # The list of paths to be excluded from coverage checkup
4
+ - "spec/"
5
+ - "config/"
6
+ groups: []
@@ -0,0 +1,37 @@
1
+ ---
2
+ # Settings added by the 'hexx-suit' gem
3
+ output: "tmp/yardstick/output.log"
4
+ path: "lib/**/*.rb"
5
+ rules:
6
+ ApiTag::Presence:
7
+ enabled: true
8
+ exclude: []
9
+ ApiTag::Inclusion:
10
+ enabled: true
11
+ exclude: []
12
+ ApiTag::ProtectedMethod:
13
+ enabled: true
14
+ exclude: []
15
+ ApiTag::PrivateMethod:
16
+ enabled: false
17
+ exclude: []
18
+ ExampleTag:
19
+ enabled: true
20
+ exclude: []
21
+ ReturnTag:
22
+ enabled: true
23
+ exclude: []
24
+ Summary::Presence:
25
+ enabled: true
26
+ exclude: []
27
+ Summary::Length:
28
+ enabled: true
29
+ exclude: []
30
+ Summary::Delimiter:
31
+ enabled: true
32
+ exclude: []
33
+ Summary::SingleLine:
34
+ enabled: true
35
+ exclude: []
36
+ threshold: 100
37
+ verbose: false
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "hexx/validators/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "hexx-validators"
6
+ s.version = Hexx::Validators::VERSION.dup
7
+ s.author = "Andrew Kozin"
8
+ s.email = "andrew.kozin@gmail.com"
9
+ s.homepage = "https://github.com/nepalez/hexx"
10
+ s.summary = "Scaffolder for custom validators."
11
+ s.description = "Thor-based generator of an ActiveModel validators" \
12
+ "and its unit test templates."
13
+ s.license = "MIT"
14
+
15
+ s.require_paths = ["lib"]
16
+ s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
+ s.test_files = Dir["spec/**/*.rb"]
18
+ s.extra_rdoc_files = Dir["README.md", "LICENSE", "config/metrics/STYLEGUIDE"]
19
+
20
+ s.platform = Gem::Platform::RUBY
21
+ s.required_ruby_version = "~> 2.1"
22
+
23
+ s.add_runtime_dependency "hexx", "~> 8.0"
24
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ describe <%= project.type %>::<%= validator.type %>, type: :validator do
4
+
5
+ # For the validator spec those definitions are made by default:
6
+ #
7
+ # Matchers:
8
+ # * pass_with(value)
9
+ # * fail_with(value)
10
+ #
11
+ # See all the definitions at `spec/support/validators/`.
12
+
13
+ # The validator is tested by assigning values to the :name attribute.
14
+ subject { described_class.new options.merge(attributes: %w(name)) }
15
+
16
+ context "without options" do
17
+
18
+ # let!(:options) { {} }
19
+
20
+ # it "passes with [TODO: valid value]" do
21
+ # expect(subject).to pass_with [TODO: valid value]
22
+ # end
23
+ <% keys.each do |key| -%>
24
+
25
+ # it "fails with [TODO: wrong value]" do
26
+ # expect(subject).to fail_with [TODO: wrong value], key: :<%= key %>
27
+ # end
28
+ <% end -%>
29
+ end
30
+ <% params.each do |option| -%>
31
+
32
+ context "<%= option -%>: [TODO: option value]" do
33
+
34
+ # let!(:options) { { <%= option -%>: [TODO: option value] } }
35
+
36
+ # it "passes with [TODO: valid value]" do
37
+ # expect(subject).to pass_with [TODO: valid value]
38
+ # end
39
+ <% keys.each do |key| -%>
40
+
41
+ # it "fails with [TODO: wrong value]" do
42
+ # expect(subject).to fail_with [TODO: wrong value], key: :<%= key %>
43
+ # end
44
+ <% end -%>
45
+ end
46
+ <% end -%>
47
+
48
+ # Describe a behaviour of the validator for specific combinations of options,
49
+ # if the behaviour depends on their interaction.
50
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ # Includes specific helpers and matchers for validator specs
4
+ RSpec.configure do |config|
5
+ config.before :each, type: :validator do
6
+ require_relative "../validators/matchers"
7
+ end
8
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ # ==============================================================================
4
+ # Definition of validator spec matchers
5
+ # ==============================================================================
6
+ require "active_model"
7
+
8
+ # Checks that custom validator object fails with given value assigned to the
9
+ # +name+ attribute.
10
+ #
11
+ # @example
12
+ # subject { MyValidator.new attributes: :name }
13
+ # expect(subject).to fail_with some_value, key: :invalid
14
+ #
15
+ # @param [Object] value The value to check against.
16
+ # @param [Symbol] key The key for the required I18n error message.
17
+ RSpec::Matchers.define :fail_with do |value, key: nil|
18
+ match do |validator|
19
+ record = spy :record, errors: Hexx::NULL
20
+ validator.validate_each(record, :name, value)
21
+ expect(record).to have_received(:errors) do |_, error_key, *|
22
+ expect(error_key).to eq key if key
23
+ end
24
+ end
25
+ end
26
+
27
+ # Checks that custom validator object passes with given value assigned to the
28
+ # +name+ attribute.
29
+ #
30
+ # @example
31
+ # subject { MyValidator.new attributes: :name }
32
+ # expect(subject).to pass_with some_value
33
+ #
34
+ # @param [Object] value The value to check against.
35
+ RSpec::Matchers.define :pass_with do |value|
36
+ match do |validator|
37
+ record = spy :record, errors: Hexx::NULL
38
+ validator.validate_each(record, :name, value)
39
+ expect(record).not_to have_received(:errors)
40
+ end
41
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+
3
+ <% project.namespaces.each do |item| -%>
4
+ module <%= item %>
5
+
6
+ <% end -%>
7
+ module <%= project.const %>
8
+
9
+ # Custom ActiveModule validators
10
+ <% validator.namespaces.each do |item| -%>
11
+ module <%= item %>
12
+
13
+ <% end -%>
14
+ # Checks [TODO: something]
15
+ #
16
+ # @example
17
+ # class Foo
18
+ # include ActiveModule::Validations
19
+ #
20
+ # validates_with(
21
+ # <%= project.type %>::<%= validator.type %>,
22
+ # attributes: [:foo],
23
+ <% params.each do |item| -%>
24
+ # <%= item %>: [TODO: option value],
25
+ <% end -%>
26
+ # )
27
+ # end
28
+ #
29
+ # # Validation passes when [TODO: some condition satisfied]
30
+ # foo = Foo.new foo: [TODO: value]
31
+ # foo.valid? # => true
32
+ #
33
+ # # Validation fails when [TODO: some condition satisfied]
34
+ # foo = Foo.new foo: [TODO: value]
35
+ # foo.valid? # => false
36
+ #
37
+ <% params.each do |item| -%>
38
+ # @option [TODO] :<%= item %>
39
+ # [TODO: describe the option]
40
+ <% end -%>
41
+ #
42
+ # @api private
43
+ class <%= validator.const %> < ActiveModel::EachValidator
44
+
45
+ # # @private
46
+ # def validate_each(record, attribute, value)
47
+ # @value = value
48
+ <% keys.each do |key| -%>
49
+ # errors.add attribute, :<%= key %> if <%= key %>?
50
+ <% end -%>
51
+ # end
52
+
53
+ # private
54
+
55
+ # attr_reader :value
56
+ <% keys.each do |key| -%>
57
+
58
+ # def <%= key %>?
59
+ # ...
60
+ # end
61
+ <% end -%>
62
+
63
+ end # class <%= validator.const %>
64
+ <% validator.namespaces.reverse.each do |item| -%>
65
+
66
+ end # module <%= item %>
67
+ <% end -%>
68
+
69
+ end # module <%= project.const %>
70
+ <% project.namespaces.reverse.each do |item| -%>
71
+
72
+ end # module <%= item %>
73
+ <% end -%>
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+
3
+ module Hexx
4
+
5
+ module Validators
6
+
7
+ # Scaffolder for a custom validator creates:
8
+ #
9
+ # * scope class definition in `app/validators`;
10
+ # * scope specification in `spec/tests/validators`.
11
+ #
12
+ class Generator < Hexx::Generator
13
+
14
+ # @!scope class
15
+ # @!method start(arguments)
16
+ # Runs the scaffolder with a list of arguments from CLI.
17
+ # @param [Array<String>] arguments The list of arguments from CLI.
18
+ # @return [undefined]
19
+
20
+ # Root folder for the generator's templates and description.
21
+ # @return [String] The absolute path to folder.
22
+ # @api private
23
+ def self.source_root
24
+ ::File.expand_path "../generator", __FILE__
25
+ end
26
+
27
+ namespace :validator
28
+ desc "Scaffolds a custom validator with unit test and translations."
29
+
30
+ argument(
31
+ :name,
32
+ describe: "The name of the validator",
33
+ banner: "NAME",
34
+ type: :string
35
+ )
36
+
37
+ class_option(
38
+ :options,
39
+ aliases: "-o",
40
+ default: [],
41
+ describe: "The list of validator options",
42
+ type: :array
43
+ )
44
+
45
+ class_option(
46
+ :errors,
47
+ aliases: "-e",
48
+ default: [],
49
+ describe: "The list of error keys for I18n translations",
50
+ type: :array
51
+ )
52
+
53
+ # @private
54
+ def add_support
55
+ directory "support", "spec/support", skip: true
56
+ end
57
+
58
+ # @private
59
+ def add_specification
60
+ template "spec.erb", "spec/tests/#{ validator.path }_spec.rb"
61
+ end
62
+
63
+ # @private
64
+ def add_declaration
65
+ template "validator.erb", "app/#{ validator.path }.rb"
66
+ end
67
+
68
+ private
69
+
70
+ def project
71
+ @project ||= Hexx::Name.new ::File.basename(destination_root)
72
+ end
73
+
74
+ def validator
75
+ @validator ||= Hexx::Name.new "validators/#{ name }"
76
+ end
77
+
78
+ def keys
79
+ @keys ||= options["errors"].map(&:snake_case)
80
+ end
81
+
82
+ def params
83
+ @params ||= options["options"]
84
+ end
85
+
86
+ end # class Generator
87
+
88
+ end # module Validators
89
+
90
+ end # module Hexx
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ module Hexx
4
+
5
+ module Validators
6
+ # The semantic version of the module.
7
+ # @see http://semver.org/ Semantic versioning 2.0
8
+ VERSION = "0.0.1".freeze
9
+
10
+ end # module Validators
11
+
12
+ end # module Hexx
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ # Shared namespace for the Hexx-based gems.
4
+ module Hexx
5
+
6
+ # Namespace for content of the 'hexx-validators'.
7
+ module Validators
8
+
9
+ end # module Validators
10
+
11
+ end # module Hexx
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "hexx"
3
+
4
+ require_relative "hexx/validators"
5
+ require_relative "hexx/validators/version"
6
+ require_relative "hexx/validators/generator"
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ # Loads the RSpec test suit.
4
+ require "hexx-suit"
5
+
6
+ # Loads runtime metrics in the current scope
7
+ Hexx::Suit.load_metrics_for(self) if ENV["USE_METRICS"]
8
+
9
+ # Loads the RSpec support files.
10
+ Dir.chdir File.expand_path("..", __FILE__) do
11
+ Dir["./support/config/*.rb"].each { |file| require file }
12
+ end
13
+
14
+ # Loads the code of the module.
15
+ require "hexx-validators"
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+
3
+ # See additional helpers and matchers for :sandbox and :capture tagged specs
4
+ # in the config/initializers folder of the 'hexx' gem.
5
+ describe Hexx::Validators::Generator, :sandbox, :capture do
6
+
7
+ describe ".start" do
8
+
9
+ shared_examples "adding support files" do
10
+
11
+ before { subject }
12
+
13
+ it "[creates file]" do
14
+ expect("spec/support/config/validator.rb").to be_present_in_sandbox
15
+ expect("spec/support/validators/matchers.rb").to be_present_in_sandbox
16
+ end
17
+
18
+ end # examples
19
+
20
+ shared_examples "adding specification" do
21
+
22
+ let(:file) { "spec/tests/validators/in_the_past_spec.rb" }
23
+ before { subject }
24
+
25
+ it "[creates file]" do
26
+ expect(file).to be_present_in_sandbox
27
+ end
28
+
29
+ it "[describes validator]" do
30
+ content = read_in_sandbox(file)
31
+ expect(content).to include "describe Sandbox::Validators::InThePast"
32
+ end
33
+
34
+ end # examples
35
+
36
+ shared_examples "adding validator" do
37
+
38
+ let(:file) { "app/validators/in_the_past.rb" }
39
+ before { subject }
40
+
41
+ it "[creates a file]" do
42
+ expect(file).to be_present_in_sandbox
43
+ end
44
+
45
+ it "[defines a validator]" do
46
+ content = read_in_sandbox(file)
47
+ definition = "class InThePast < ActiveModel::EachValidator"
48
+ expect(content).to include definition
49
+ end
50
+
51
+ end # examples
52
+
53
+ subject do
54
+ try_in_sandbox { described_class.start %w(in_the_past).concat opts }
55
+ end
56
+
57
+ context "in_the_past" do
58
+
59
+ let(:opts) { %w() }
60
+
61
+ it_behaves_like "adding support files"
62
+ it_behaves_like "adding specification"
63
+ it_behaves_like "adding validator"
64
+
65
+ end # context
66
+
67
+ context "in_the_past -e not_in_the_past" do
68
+
69
+ let(:opts) { %w(-e not_in_the_past) }
70
+
71
+ it_behaves_like "adding support files"
72
+ it_behaves_like "adding specification"
73
+ it_behaves_like "adding validator"
74
+
75
+ it "adds check-outs for errors" do
76
+ subject
77
+ content = read_in_sandbox "app/validators/in_the_past.rb"
78
+ expect(content).to include "def not_in_the_past?"
79
+ end
80
+
81
+ end # context
82
+
83
+ end # describe .start
84
+
85
+ end # describe Hexx::Validators::Generator
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hexx-validators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kozin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hexx
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '8.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '8.0'
27
+ description: Thor-based generator of an ActiveModel validatorsand its unit test templates.
28
+ email: andrew.kozin@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ - LICENSE
34
+ - config/metrics/STYLEGUIDE
35
+ files:
36
+ - ".coveralls.yml"
37
+ - ".metrics"
38
+ - ".rspec"
39
+ - ".rubocop.yml"
40
+ - ".travis.yml"
41
+ - ".yardopts"
42
+ - Gemfile
43
+ - Guardfile
44
+ - LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - config/metrics/STYLEGUIDE
48
+ - config/metrics/cane.yml
49
+ - config/metrics/churn.yml
50
+ - config/metrics/flay.yml
51
+ - config/metrics/metric_fu.yml
52
+ - config/metrics/pippi.yml
53
+ - config/metrics/reek.yml
54
+ - config/metrics/roodi.yml
55
+ - config/metrics/rubocop.yml
56
+ - config/metrics/saikuro.yml
57
+ - config/metrics/simplecov.yml
58
+ - config/metrics/yardstick.yml
59
+ - hexx-validators.gemspec
60
+ - lib/hexx-validators.rb
61
+ - lib/hexx/validators.rb
62
+ - lib/hexx/validators/generator.rb
63
+ - lib/hexx/validators/generator/spec.erb
64
+ - lib/hexx/validators/generator/support/config/validator.rb
65
+ - lib/hexx/validators/generator/support/validators/matchers.rb
66
+ - lib/hexx/validators/generator/validator.erb
67
+ - lib/hexx/validators/version.rb
68
+ - spec/spec_helper.rb
69
+ - spec/tests/generator_spec.rb
70
+ homepage: https://github.com/nepalez/hexx
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.2.2
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Scaffolder for custom validators.
94
+ test_files:
95
+ - spec/spec_helper.rb
96
+ - spec/tests/generator_spec.rb
97
+ has_rdoc: