tram-examiner 0.0.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
+ SHA1:
3
+ metadata.gz: d816f154d33841991a60815a414ebfdf5602d28a
4
+ data.tar.gz: 4ae299542eb04b2d9f7903623b4bafd7512f67a5
5
+ SHA512:
6
+ metadata.gz: 3c3e6de927cbb1ca297bc30b787085be44aafa4309d796826d059028ae5928cf399bed8d4fcd48764744121acc5fcf166946c0a4e08b277dc84a07f1b005ae03
7
+ data.tar.gz: 462a0f758959d001d73f0d3c09abd915e0d49ede32e43cd8b24800e92230d9d3f04d2d28aca6a0d273e469be61db7940547640107500ec1dd5db7bafda26a353
data/.codeclimate.yml ADDED
@@ -0,0 +1,18 @@
1
+ ---
2
+ engines:
3
+ rubocop:
4
+ enabled: true
5
+ checks:
6
+ Rubocop/Style/FrozenStringLiteralComment:
7
+ enabled: false
8
+ Rubocop/Style/Documentation:
9
+ enabled: false
10
+ Rubocop/Style/ClassAndModuleChildren:
11
+ enabled: false
12
+ Rubocop/Style/SpaceInLambdaLiteral:
13
+ enabled: false
14
+ Style/SingleLineBlockParams:
15
+ enabled: false
16
+ ratings:
17
+ paths:
18
+ - "lib/**.rb"
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.rubocop.todo.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ ---
2
+ AllCops:
3
+ DisplayCopNames: true
4
+ DisplayStyleGuide: true
5
+ StyleGuideCopsOnly: true
6
+ TargetRubyVersion: 2.3
7
+
8
+ Style/CaseEquality:
9
+ Enabled: false
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ Style/FileName:
15
+ Exclude:
16
+ - 'lib/tram-examiner.rb'
17
+
18
+ Style/LambdaCall:
19
+ Enabled: false
20
+
21
+ Style/MethodMissing:
22
+ Enabled: false
23
+
24
+ Style/MultilineMethodCallIndentation:
25
+ Exclude:
26
+ - spec/**.rb
27
+
28
+ Style/NumericPredicate:
29
+ Enabled: false
30
+
31
+ Style/RaiseArgs:
32
+ Enabled: false
33
+
34
+ Style/RescueModifier:
35
+ Enabled: false
36
+
37
+ Style/StringLiterals:
38
+ EnforcedStyle: double_quotes
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ script:
6
+ - bundle exec rspec
7
+ - bundle exec rubocop
8
+ rvm:
9
+ - '2.3.0'
10
+ - '2.4.0'
11
+ - ruby-head
12
+ before_install: gem install bundler -v 1.12.5
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: ruby-head
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in evil-client.gemspec
4
+ gemspec
5
+
6
+ gem "dry-memoizer", git: "https://github.com/nepalez/dry-memoizer"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Andrew Kozin (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,103 @@
1
+ # Tram::Examiner
2
+
3
+ The gem allows to add a standalone validator to Ruby classes.
4
+
5
+ This is a part of collection of patterns extracted from Rails projects with a special focus on separation and composability of data validators.
6
+
7
+ [![Gem Version][gem-badger]][gem]
8
+ [![Build Status][travis-badger]][travis]
9
+ [![Dependency Status][gemnasium-badger]][gemnasium]
10
+ [![Code Climate][codeclimate-badger]][codeclimate]
11
+
12
+ <a href="https://evilmartians.com/">
13
+ <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'tram-examiner'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ ```shell
26
+ $ bundle
27
+ ```
28
+
29
+ Or install it yourself as:
30
+
31
+ ```shell
32
+ $ gem install tram-examiner
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ Include the module to the class and define validation rules inside the `schema` block.
38
+
39
+ ```ruby
40
+ require "tram-examiner"
41
+
42
+ class User < Struct.new(:name, :age)
43
+ include Tram::Examiner
44
+
45
+ examiner do
46
+ validates :name, presence: true
47
+ end
48
+ end
49
+ ```
50
+
51
+ Inside the block you should use standard ActiveModel validations. The trick is that instead of including `ActiveModel::Validations` directly, the examiner adds validations to a separate decorator, wrapped around validated object.
52
+
53
+ It adds a variable `@__examiner__`, which refers to that standalone decorator. Access to validation result is provided via delegators `valid?`, `validate!` and `errors`.
54
+
55
+ ```ruby
56
+ user = User.new '', 46
57
+
58
+ user.valid? # => false
59
+ user.validate! # Boom with #<<ActiveModel::ValidationError: Validation failed: Name can't be blank>
60
+ user.errors.messages # => { name: ["cannot be blank"] }
61
+ ```
62
+
63
+ Under the hood:
64
+
65
+ ```ruby
66
+ user.is_a? ActiveModel::Validations
67
+ # => false
68
+
69
+ user.instance_variable_get(:@__examiner__).is_a? ActiveModel::Validations
70
+ # => true
71
+ ```
72
+
73
+ For syntax sugar you can use `let` memoizer helper inside the `examiner` block:
74
+
75
+ ```ruby
76
+ class User < Struct.new(:first_name, :last_name, :age)
77
+ include Tram::Examiner
78
+
79
+ examiner do
80
+ let(:name) { [first_name, last_name].compact.join(" ") }
81
+ validates :name, presence: true
82
+ end
83
+ end
84
+
85
+ user = User.new '', '', 51
86
+ user.errors.messages # => { name: ["cannot be blank"] }
87
+ ```
88
+
89
+ Notice the block is evaluated in the scope of standalone validator, not the current class!
90
+ That is why neither `let`, nor `validates` are available outside of the block.
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
95
+
96
+ [codeclimate-badger]: https://img.shields.io/codeclimate/github/tram-rb/tram-examiner.svg?style=flat
97
+ [codeclimate]: https://codeclimate.com/github/tram-rb/tram-examiner
98
+ [gem-badger]: https://img.shields.io/gem/v/tram-examiner.svg?style=flat
99
+ [gem]: https://rubygems.org/gems/tram-examiner
100
+ [gemnasium-badger]: https://img.shields.io/gemnasium/tram-rb/tram-examiner.svg?style=flat
101
+ [gemnasium]: https://gemnasium.com/tram-rb/tram-examiner
102
+ [travis-badger]: https://img.shields.io/travis/tram-rb/tram-examiner/master.svg?style=flat
103
+ [travis]: https://travis-ci.org/tram-rb/tram-examiner
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,36 @@
1
+ require "active_model"
2
+ require "dry-memoizer"
3
+
4
+ module Tram::Examiner
5
+ # Container for standalone validation of PORO instance
6
+ class Results < SimpleDelegator
7
+ extend Dry::Memoizer # adds `let` syntax
8
+ include ActiveModel::Validations
9
+
10
+ class << self
11
+ def subject(value = nil)
12
+ (value ? @subject = value : @subject)
13
+ end
14
+
15
+ def model_name
16
+ @model_name ||= ActiveModel::Name.new(subject || Tram::Examiner)
17
+ end
18
+
19
+ def inspect
20
+ "Tram::Examiner::Results[#{model_name}]"
21
+ end
22
+ end
23
+
24
+ def inspect
25
+ "#<Tram::Examiner::Results[#{model_name}] @errors=#{errors}>"
26
+ end
27
+
28
+ private
29
+
30
+ def initialize(subject)
31
+ return super if subject.is_a?(self.class.subject)
32
+ raise "#{self.class.inspect} was designed as a standalone validator" \
33
+ " for instances of #{self.class.subject}, not the #{subject.class}."
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ module Tram
2
+ # Adds standalone validator to the instances of current class
3
+ module Examiner
4
+ require_relative "examiner/results"
5
+
6
+ # Adds class method helper to define validation rules
7
+ module DSL
8
+ def examiner(&block)
9
+ @examiner ||= Class.new(Results).tap { |exam| exam.subject(self) }
10
+ @examiner.tap { |klass| klass.instance_eval(&block) if block }
11
+ end
12
+
13
+ def inherited(klass)
14
+ new_examiner = Class.new(@examiner).tap { |exam| exam.subject(klass) }
15
+ klass.instance_variable_set :@examiner, new_examiner
16
+ end
17
+ end
18
+
19
+ def self.included(klass)
20
+ klass.extend DSL
21
+ end
22
+
23
+ def validate!
24
+ (@__examiner__ = self.class.examiner.new(self)).validate!
25
+ end
26
+
27
+ def valid?
28
+ (@__examiner__ = self.class.examiner.new(self)).valid?
29
+ end
30
+
31
+ def errors
32
+ (@__examiner__ ||= self.class.examiner.new(self).tap(&:valid?)).errors
33
+ end
34
+ end
35
+ end
@@ -0,0 +1 @@
1
+ require_relative "tram/examiner"
@@ -0,0 +1,20 @@
1
+ begin
2
+ require "pry"
3
+ rescue LoadError
4
+ nil
5
+ end
6
+
7
+ require "tram-examiner"
8
+
9
+ RSpec.configure do |config|
10
+ config.order = :random
11
+ config.filter_run focus: true
12
+ config.run_all_when_everything_filtered = true
13
+
14
+ # Prepare the Test namespace for constants defined in specs
15
+ config.around(:each) do |example|
16
+ Test = Module.new
17
+ example.run
18
+ Object.send :remove_const, :Test
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe Tram::Examiner do
4
+ before do
5
+ class Test::User
6
+ include Tram::Examiner
7
+
8
+ attr_reader :first_name, :second_name
9
+ def initialize(first_name, second_name)
10
+ @first_name = first_name
11
+ @second_name = second_name
12
+ end
13
+
14
+ examiner do
15
+ let(:name) { [first_name, second_name].compact.join(" ") }
16
+ validates :name, presence: true
17
+ end
18
+ end
19
+ end
20
+
21
+ it "doesn't include ActiveModel::Validations into the class" do
22
+ expect(Test::User.new("", "")).not_to be_a ActiveModel::Validations
23
+ end
24
+
25
+ context "#valid?" do
26
+ it "works" do
27
+ expect(Test::User.new("", "")).not_to be_valid
28
+ expect(Test::User.new("Andy", "")).to be_valid
29
+ end
30
+ end
31
+
32
+ context "#errors" do
33
+ it "works" do
34
+ expect(Test::User.new("", "").errors).not_to be_empty
35
+ expect(Test::User.new("Andy", "").errors).to be_empty
36
+ end
37
+ end
38
+
39
+ context "#validate!" do
40
+ it "works" do
41
+ expect { Test::User.new("", "").validate! }
42
+ .to raise_error ActiveModel::ValidationError
43
+
44
+ expect { Test::User.new("Andy", "").validate! }
45
+ .not_to raise_error
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "tram-examiner"
3
+ gem.version = "0.0.1"
4
+ gem.author = "Andrew Kozin (nepalez)"
5
+ gem.email = "andrew.kozin@gmail.com"
6
+ gem.homepage = "https://github.com/nepalez/tram-examiner"
7
+ gem.summary = "Standalone validator for Rails objects"
8
+ gem.license = "MIT"
9
+
10
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
11
+ gem.test_files = gem.files.grep(/^spec/)
12
+ gem.extra_rdoc_files = Dir["README.md", "LICENSE", "CHANGELOG.md"]
13
+
14
+ gem.required_ruby_version = ">= 2.3"
15
+
16
+ gem.add_runtime_dependency "dry-memoizer", "~> 0.0.1"
17
+ gem.add_runtime_dependency "activemodel", "~> 5.0"
18
+
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "rspec", "~> 3.0"
21
+ gem.add_development_dependency "rubocop"
22
+ gem.add_development_dependency "pry"
23
+ gem.add_development_dependency "pry-byebug"
24
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tram-examiner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kozin (nepalez)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-memoizer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
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: pry
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: pry-byebug
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:
112
+ email: andrew.kozin@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - README.md
117
+ files:
118
+ - ".codeclimate.yml"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - lib/tram-examiner.rb
128
+ - lib/tram/examiner.rb
129
+ - lib/tram/examiner/results.rb
130
+ - spec/spec_helper.rb
131
+ - spec/tram/examiner_spec.rb
132
+ - tram-examiner.gemspec
133
+ homepage: https://github.com/nepalez/tram-examiner
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '2.3'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.5.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Standalone validator for Rails objects
157
+ test_files:
158
+ - spec/spec_helper.rb
159
+ - spec/tram/examiner_spec.rb