mini_sanity 1.0.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: c5da04d9d5a007087a446a04332d0ffc6384a5d8
4
+ data.tar.gz: a26754f66a63338980c31d97461d8a443d868106
5
+ SHA512:
6
+ metadata.gz: d595fc9b17a60fb29ece9f2d553c0549aaa341ce055d16589157e4be2be072b0c28d92c8ea3a35973d6bce0aaf30444b1644f6f3704033e39462eb3289c9d8e7
7
+ data.tar.gz: '08db65b91fc28caa65d47a711afb0cb338ad0edfd1a601e78e642bb1846f4e150547612b42f996e77222c9ac27c8a35a86fa437da97a7c12fbc8c7af4cadb005'
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.5
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mini_sanity.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jonathan Hefner
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,71 @@
1
+ # mini_sanity
2
+
3
+ In-line [sanity checks], written as extensions to core Ruby objects. See
4
+ API listing below, or browse the [full documentation].
5
+
6
+ [sanity checks]: https://en.wikipedia.org/wiki/Sanity_check
7
+ [full documentation]: http://www.rubydoc.info/gems/mini_sanity/
8
+
9
+
10
+ # Example
11
+
12
+ ```ruby
13
+ require "json"
14
+ require "pathname"
15
+
16
+ path = Pathname.new("hosted_files.json").assert_exist!
17
+
18
+ hosted_files = JSON.parse(path.read).assert_instance_of!(Array)
19
+
20
+ urls = hosted_files.flat_map do |file_info|
21
+ file_info.fetch("mirror_urls").refute_empty!
22
+ end
23
+
24
+ domains = urls.map do |url|
25
+ url.assert_match!(%r"^https?://").split("/")[2]
26
+ end.uniq
27
+ ```
28
+
29
+
30
+ # API
31
+
32
+ - [Object](http://www.rubydoc.info/gems/mini_sanity/Object)
33
+ - [#assert_instance_of!](http://www.rubydoc.info/gems/mini_sanity/Object:assert_instance_of%21)
34
+ - [#assert_kind_of!](http://www.rubydoc.info/gems/mini_sanity/Object:assert_kind_of%21)
35
+ - [#assert_respond_to!](http://www.rubydoc.info/gems/mini_sanity/Object:assert_respond_to%21)
36
+ - [#refute_nil!](http://www.rubydoc.info/gems/mini_sanity/Object:refute_nil%21)
37
+ - [Enumerable](http://www.rubydoc.info/gems/mini_sanity/Enumerable)
38
+ - [#refute_empty!](http://www.rubydoc.info/gems/mini_sanity/Enumerable:refute_empty%21)
39
+ - [String](http://www.rubydoc.info/gems/mini_sanity/String)
40
+ - [#assert_match!](http://www.rubydoc.info/gems/mini_sanity/String:assert_match%21)
41
+ - [#refute_empty!](http://www.rubydoc.info/gems/mini_sanity/String:refute_empty%21)
42
+ - [#refute_match!](http://www.rubydoc.info/gems/mini_sanity/String:refute_match%21)
43
+ - [Pathname](http://www.rubydoc.info/gems/mini_sanity/Pathname)
44
+ - [#assert_exist!](http://www.rubydoc.info/gems/mini_sanity/Pathname:assert_exist%21)
45
+ - [#refute_exist!](http://www.rubydoc.info/gems/mini_sanity/Pathname:refute_exist%21)
46
+
47
+
48
+ ## Installation
49
+
50
+ Install from [Ruby Gems](https://rubygems.org/gems/mini_sanity):
51
+
52
+ ```bash
53
+ $ gem install mini_sanity
54
+ ```
55
+
56
+ Then require in your Ruby script:
57
+
58
+ ```ruby
59
+ require "mini_sanity"
60
+ ```
61
+
62
+
63
+ ## Contributing
64
+
65
+ Run `rake test` to run the tests. You can also run `rake irb` for an
66
+ interactive prompt that pre-loads the project code.
67
+
68
+
69
+ ## License
70
+
71
+ [MIT License](https://opensource.org/licenses/MIT)
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "yard"
4
+
5
+
6
+ YARD::Rake::YardocTask.new(:doc) do |t|
7
+ end
8
+
9
+ desc "Launch IRB with this gem pre-loaded"
10
+ task :irb do
11
+ require "mini_sanity"
12
+ require "irb"
13
+ ARGV.clear
14
+ IRB.start
15
+ end
16
+
17
+ Rake::TestTask.new(:test) do |t|
18
+ t.libs << "test"
19
+ t.libs << "lib"
20
+ t.test_files = FileList["test/**/*_test.rb"]
21
+ end
22
+
23
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ require_relative "mini_sanity/version"
2
+ require_relative "mini_sanity/error"
3
+ require_relative "mini_sanity/enumerable"
4
+ require_relative "mini_sanity/object"
5
+ require_relative "mini_sanity/pathname"
6
+ require_relative "mini_sanity/string"
@@ -0,0 +1,23 @@
1
+ module Enumerable
2
+
3
+ # Checks that the Enumerable is not empty, and returns the Enumerable
4
+ # unmodified. If the Enumerable fails this check, an exception is
5
+ # raised.
6
+ #
7
+ # @example
8
+ # [7, 8].refute_empty! # == [7, 8]
9
+ # [].refute_empty! # raises exception
10
+ #
11
+ # @param name [String, Symbol]
12
+ # optional name to include in the error message
13
+ # @return [self]
14
+ # @raise [MiniSanity::Error]
15
+ # if the Enumerable is empty
16
+ def refute_empty!(name = nil)
17
+ if self.empty?
18
+ raise MiniSanity::Error.new("#{name || self.class} is empty")
19
+ end
20
+ self
21
+ end
22
+
23
+ end
@@ -0,0 +1,2 @@
1
+ class MiniSanity::Error < RuntimeError
2
+ end
@@ -0,0 +1,91 @@
1
+ class Object
2
+
3
+ # Checks that the Object is not nil, and returns the Object
4
+ # unmodified. If the Object fails this check, an exception is raised.
5
+ #
6
+ # @example
7
+ # [7, 8].first.refute_nil! # == 7
8
+ # [].first.refute_nil! # raises exception
9
+ #
10
+ # @param name [String, Symbol]
11
+ # optional name to include in the error message
12
+ # @return [self]
13
+ # @raise [MiniSanity::Error]
14
+ # if the Object is nil
15
+ def refute_nil!(name = nil)
16
+ if self.nil?
17
+ message = name ? "#{name} is nil" : "unexpected nil"
18
+ raise MiniSanity::Error.new(message)
19
+ end
20
+ self
21
+ end
22
+
23
+ # Checks that the Object is an instance of a given class, and returns
24
+ # the Object unmodified. If the Object fails this check, an exception
25
+ # is raised.
26
+ #
27
+ # @example
28
+ # "abc".assert_instance_of!(String) # == "abc"
29
+ # "abc".assert_instance_of!(Numeric) # raises exception
30
+ #
31
+ # @param klass [Class]
32
+ # class to check
33
+ # @param name [String, Symbol]
34
+ # optional name to include in the error message
35
+ # @return [self]
36
+ # @raise [MiniSanity::Error]
37
+ # if the Object is not an instance of +klass+
38
+ def assert_instance_of!(klass, name = nil)
39
+ unless self.instance_of?(klass)
40
+ prelude = name ? "#{name} is instance of #{self.class}" : "unexpected #{self.class}"
41
+ raise MiniSanity::Error.new("#{prelude}; expected #{klass}")
42
+ end
43
+ self
44
+ end
45
+
46
+ # Checks that the Object is an instance of a given class or one of its
47
+ # subclasses, and returns the Object unmodified. If the Object fails
48
+ # this check, an exception is raised.
49
+ #
50
+ # @example
51
+ # 42.assert_kind_of!(Numeric) # == 42
52
+ # 42.assert_kind_of!(Float) # raises exception
53
+ #
54
+ # @param klass [Class]
55
+ # class to check
56
+ # @param name [String, Symbol]
57
+ # optional name to include in the error message
58
+ # @return [self]
59
+ # @raise [MiniSanity::Error]
60
+ # if the Object is not an instance of +klass+ or its subclasses
61
+ def assert_kind_of!(klass, name = nil)
62
+ unless self.kind_of?(klass)
63
+ prelude = name ? "#{name} is instance of #{self.class}" : "unexpected #{self.class}"
64
+ raise MiniSanity::Error.new("#{prelude}; expected #{klass} or one of its subclasses")
65
+ end
66
+ self
67
+ end
68
+
69
+ # Checks that the Object responds to a given method, and returns the
70
+ # Object unmodified. If the Object fails this check, an exception is
71
+ # raised.
72
+ #
73
+ # @example
74
+ # "abc".assert_respond_to!(:empty?) # == "abc"
75
+ # "abc".assert_respond_to!(:pop) # raises exception
76
+ #
77
+ # @param method_name [String, Symbol]
78
+ # name of method to check
79
+ # @param name [String, Symbol]
80
+ # optional name to include in the error message
81
+ # @return [self]
82
+ # @raise [MiniSanity::Error]
83
+ # if the Object does not respond to +method_name+
84
+ def assert_respond_to!(method_name, name = nil)
85
+ unless self.respond_to?(method_name)
86
+ raise MiniSanity::Error.new("#{name || self.class} does not respond to #{method_name}")
87
+ end
88
+ self
89
+ end
90
+
91
+ end
@@ -0,0 +1,45 @@
1
+ class Pathname
2
+
3
+ # Checks that the file or directory indicated by the Pathname exists,
4
+ # and returns the Pathname unmodified. If the Pathname fails this
5
+ # check, an exception is raised.
6
+ #
7
+ # @example
8
+ # Pathname.new(__FILE__).assert_exist! # == Pathname.new(__FILE__)
9
+ # Pathname.new("/dev/null/nope").assert_exist! # == raises exception
10
+ #
11
+ # @param name [String, Symbol]
12
+ # optional name to include in the error message
13
+ # @return [self]
14
+ # @raise [MiniSanity::Error]
15
+ # if the file or directory indicated by the Pathname does not exist
16
+ def assert_exist!(name = nil)
17
+ unless self.exist?
18
+ descriptor = name ? "#{name} (#{self})" : self.to_s
19
+ raise MiniSanity::Error.new("#{descriptor} does not exist")
20
+ end
21
+ self
22
+ end
23
+
24
+ # Checks that the file or directory indicated by the Pathname does
25
+ # not already exist, and returns the Pathname unmodified. If the
26
+ # Pathname fails this check, an exception is raised.
27
+ #
28
+ # @example
29
+ # Pathname.new("/dev/null/nope").refute_exist! # == Pathname.new("/dev/null/nope")
30
+ # Pathname.new(__FILE__).refute_exist! # raises exception
31
+ #
32
+ # @param name [String, Symbol]
33
+ # optional name to include in the error message
34
+ # @return [self]
35
+ # @raise [MiniSanity::Error]
36
+ # if the file or directory indicated by the Pathname already exists
37
+ def refute_exist!(name = nil)
38
+ if self.exist?
39
+ descriptor = name ? "#{name} (#{self})" : self.to_s
40
+ raise MiniSanity::Error.new("#{descriptor} already exists")
41
+ end
42
+ self
43
+ end
44
+
45
+ end
@@ -0,0 +1,68 @@
1
+ class String
2
+
3
+ # Checks that the String is not empty, and returns the String
4
+ # unmodified. If the String fails this check, an exception is raised.
5
+ #
6
+ # @example
7
+ # "abc".refute_empty! # == "abc"
8
+ # "".refute_empty! # raises exception
9
+ #
10
+ # @param name [String, Symbol]
11
+ # optional name to include in the error message
12
+ # @return [self]
13
+ # @raise [MiniSanity::Error]
14
+ # if the String is empty
15
+ def refute_empty!(name = nil)
16
+ if self.empty?
17
+ raise MiniSanity::Error.new("#{name || self.class} is empty")
18
+ end
19
+ self
20
+ end
21
+
22
+ # Checks that the String matches a given regular expression, and
23
+ # returns the String unmodified. If the String fails this check, an
24
+ # exception is raised.
25
+ #
26
+ # @example
27
+ # "abc".assert_match!(/b/) # == "abc"
28
+ # "abc".assert_match!(/x/) # raises exception
29
+ #
30
+ # @param regexp [Regexp]
31
+ # regular expression to check
32
+ # @param name [String, Symbol]
33
+ # optional name to include in the error message
34
+ # @return [self]
35
+ # @raise [MiniSanity::Error]
36
+ # if the String does not match +regexp+
37
+ def assert_match!(regexp, name = nil)
38
+ unless regexp =~ self
39
+ descriptor = name ? "#{name} (#{self.inspect})" : self.inspect
40
+ raise MiniSanity::Error.new("#{descriptor} does not match #{regexp.inspect}")
41
+ end
42
+ self
43
+ end
44
+
45
+ # Checks that the String does not match a given regular expression,
46
+ # and returns the String unmodified. If the String fails this check,
47
+ # an exception is raised.
48
+ #
49
+ # @example
50
+ # "abc".refute_match!(/x/) # == "abc"
51
+ # "abc".refute_match!(/b/) # raises exception
52
+ #
53
+ # @param regexp [Regexp]
54
+ # regular expression to check
55
+ # @param name [String, Symbol]
56
+ # optional name to include in the error message
57
+ # @return [self]
58
+ # @raise [MiniSanity::Error]
59
+ # if the String matches +regexp+
60
+ def refute_match!(regexp, name = nil)
61
+ if regexp =~ self
62
+ descriptor = name ? "#{name} (#{self.inspect})" : self.inspect
63
+ raise MiniSanity::Error.new("#{descriptor} matches #{regexp.inspect}; expected no match")
64
+ end
65
+ self
66
+ end
67
+
68
+ end
@@ -0,0 +1,3 @@
1
+ module MiniSanity
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mini_sanity/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mini_sanity"
8
+ spec.version = MiniSanity::VERSION
9
+ spec.authors = ["Jonathan Hefner"]
10
+ spec.email = ["jonathan.hefner@gmail.com"]
11
+
12
+ spec.summary = %q{In-line sanity checks}
13
+ spec.homepage = "https://github.com/jonathanhefner/mini_sanity"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.15"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "yard", "~> 0.9"
27
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_sanity
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Hefner
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ description:
70
+ email:
71
+ - jonathan.hefner@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/mini_sanity.rb
83
+ - lib/mini_sanity/enumerable.rb
84
+ - lib/mini_sanity/error.rb
85
+ - lib/mini_sanity/object.rb
86
+ - lib/mini_sanity/pathname.rb
87
+ - lib/mini_sanity/string.rb
88
+ - lib/mini_sanity/version.rb
89
+ - mini_sanity.gemspec
90
+ homepage: https://github.com/jonathanhefner/mini_sanity
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.8
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: In-line sanity checks
114
+ test_files: []