checkable 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7aba1e017411c601bb90fe25a0d64a05078b8865
4
+ data.tar.gz: 532a27db89f7a0b494b21ce966f1570db816a36a
5
+ SHA512:
6
+ metadata.gz: d34ba6331a323c088cfb6b186cbdf6ace97c8bddafad30cc24f8945b0e0ced4852cb05e68b10443b8845d91ff046f3908a53ce39210e7e6a5785609b81c8522d
7
+ data.tar.gz: 4a54d95cb69b846aa1c5bfda1323bf1be7fbf13a5a07419521a6c60e26fedf8d2134885365062c3f89700ac0534c16b43c83f53cf16bee9d817d9825f3658e56
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .#*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in checkable.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 conanite
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # Checkable
2
+
3
+ A simple runner for checks, kind of like unit tests for user data, for cases
4
+ where Rails validations won't cut the mustard. Runs a set of "checks" against
5
+ your objects and lets you know what passes and what fails. Use the report as
6
+ a basis for proposing fixes to your users.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'checkable'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install checkable
21
+
22
+ ## Usage
23
+
24
+ A "check" is an object that responds to #check with one argument, the object to check.
25
+ This method returns true if the check passes, and false if the check fails. Register your
26
+ check by calling Checkable.register(target_type_name, check_instance)
27
+
28
+ See specs for some more details.
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( http://github.com/<my-github-username>/checkable/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8; mode: ruby -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'checkable/version'
7
+
8
+ Gem::Specification.new do |gem|
9
+ gem.name = "checkable"
10
+ gem.version = Checkable::VERSION
11
+ gem.authors = ["Conan Dalton"]
12
+ gem.license = "MIT"
13
+ gem.email = ["conan@conandalton.net"]
14
+ gem.summary = %q{A simple runner for checks, kind of like unit tests for user data, for cases where Rails validations won't cut the mustard. }
15
+ gem.description = %q{Runs a set of "checks" against your objects and lets you know what passes and what fails. Use the report as a basis for proposing fixes to your users. }
16
+ gem.homepage = "https://github.com/conanite/checkable"
17
+
18
+ gem.add_development_dependency 'rspec', '~> 2.9'
19
+ gem.add_development_dependency 'rspec_numbering_formatter'
20
+
21
+ gem.files = `git ls-files -z`.split("\x0")
22
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
24
+ gem.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,65 @@
1
+ require "checkable/version"
2
+
3
+ module Checkable
4
+ @@checks = Hash.new { |h,k| h[k] = [] }
5
+
6
+ def self.register type, check
7
+ @@checks[type] << check
8
+ end
9
+
10
+ def self.checks_for type
11
+ @@checks[type]
12
+ end
13
+
14
+ class FocusOnCheckClass
15
+ def initialize check_class
16
+ @check_class = check_class
17
+ end
18
+
19
+ def enabled? check
20
+ check.class == @check_class
21
+ end
22
+ end
23
+
24
+ class NoFocus
25
+ def self.enabled? _
26
+ true
27
+ end
28
+ end
29
+
30
+ class Report
31
+ attr_accessor :object, :passing, :failing
32
+
33
+ def initialize object
34
+ @object = object
35
+ @passing = []
36
+ @failing = []
37
+ end
38
+
39
+ def run check
40
+ (check.check(object) ? passing : failing) << check
41
+ end
42
+
43
+ def ok?
44
+ (passing.size > 0) && (failing.size == 0)
45
+ end
46
+ end
47
+
48
+ class Checker
49
+ attr_accessor :reports, :focus
50
+
51
+ def initialize focus=NoFocus
52
+ @focus = focus
53
+ @reports = []
54
+ end
55
+
56
+ def check object
57
+ report = Report.new object
58
+ checks = Checkable.checks_for object.class.name
59
+ checks.each { |check|
60
+ report.run check if @focus.enabled?(check)
61
+ }
62
+ @reports << report
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Checkable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Checkable do
4
+ let(:checker) { Checkable::Checker.new filter }
5
+
6
+ def run_checks thing
7
+ checker.check thing
8
+ checker.reports.first
9
+ end
10
+
11
+ describe "using a filter to restrict the set of checks applied" do
12
+ let(:filter) { Checkable::FocusOnCheckClass.new CheckWidgetIsFixed }
13
+
14
+ it "should be ok if the widget is fixed" do
15
+ expect(run_checks Widget.new(Battery.new, true)).to be_ok
16
+ end
17
+
18
+ it "should not be ok if the widget is not fixed" do
19
+ expect(run_checks Widget.new(Battery.new, false)).not_to be_ok
20
+ end
21
+ end
22
+
23
+ describe Widget do
24
+ let(:filter) { Checkable::NoFocus }
25
+
26
+ it "should not be ok if the widget is not fixed and has no battery" do
27
+ report = run_checks Widget.new(nil, false)
28
+ expect(report).not_to be_ok
29
+ expect(report.passing.size).to eq 0
30
+ expect(report.failing.size).to eq 2
31
+ end
32
+
33
+ it "should not be ok if the widget is is fixed but has no battery" do
34
+ report = run_checks Widget.new(nil, true)
35
+ expect(report.passing.size).to eq 1
36
+ expect(report.failing.size).to eq 1
37
+ expect(report).not_to be_ok
38
+ end
39
+
40
+ it "should not be ok if the widget is not fixed" do
41
+ report = run_checks Widget.new(Battery.new, false)
42
+ expect(report).not_to be_ok
43
+ expect(report.passing.size).to eq 1
44
+ expect(report.failing.size).to eq 1
45
+ end
46
+
47
+ it "should be ok if the widget is fixed and has a battery" do
48
+ report = run_checks Widget.new(Battery.new, true)
49
+ expect(report).to be_ok
50
+ expect(report.passing.size).to eq 2
51
+ expect(report.failing.size).to eq 0
52
+ end
53
+ end
54
+
55
+ describe Battery do
56
+ let(:filter) { Checkable::NoFocus }
57
+
58
+ it "should be ok if the widget is fixed" do
59
+ expect(run_checks Battery.new(true)).to be_ok
60
+ end
61
+
62
+ it "should not be ok if the widget is not fixed" do
63
+ expect(run_checks Battery.new).not_to be_ok
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,55 @@
1
+ require 'checkable'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
20
+
21
+ class Widget
22
+ attr_accessor :battery, :fixed
23
+ def initialize battery, fixed
24
+ @battery, @fixed = battery, fixed
25
+ end
26
+ end
27
+
28
+ class Battery
29
+ attr_accessor :charged
30
+ def initialize charged=false
31
+ @charged = charged
32
+ end
33
+ end
34
+
35
+ class CheckWidgetIsFixed
36
+ def check widget
37
+ widget.fixed
38
+ end
39
+ end
40
+
41
+ class CheckWidgetHasBattery
42
+ def check widget
43
+ widget.battery != nil
44
+ end
45
+ end
46
+
47
+ class CheckBatteryIsCharged
48
+ def check battery
49
+ battery.charged
50
+ end
51
+ end
52
+
53
+ Checkable.register "Widget", CheckWidgetIsFixed.new
54
+ Checkable.register "Widget", CheckWidgetHasBattery.new
55
+ Checkable.register "Battery", CheckBatteryIsCharged.new
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: checkable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Conan Dalton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec_numbering_formatter
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
+ description: 'Runs a set of "checks" against your objects and lets you know what passes
42
+ and what fails. Use the report as a basis for proposing fixes to your users. '
43
+ email:
44
+ - conan@conandalton.net
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - Gemfile
52
+ - MIT-LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - checkable.gemspec
56
+ - lib/checkable.rb
57
+ - lib/checkable/version.rb
58
+ - spec/checkable_spec.rb
59
+ - spec/spec_helper.rb
60
+ homepage: https://github.com/conanite/checkable
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A simple runner for checks, kind of like unit tests for user data, for cases
84
+ where Rails validations won't cut the mustard.
85
+ test_files:
86
+ - spec/checkable_spec.rb
87
+ - spec/spec_helper.rb