needful_things 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1609e9a3c29932e2f6a9a4b40fe89c25ac9e54cd82265af67efba7144d0150f3
4
+ data.tar.gz: c9924bf10e755db7c8378bf6d0f63b946274ac69e0d20e91a65bb3416dd15eb4
5
+ SHA512:
6
+ metadata.gz: e30dfecbda7f7aef4da658b6f16cbefab8ff9bc59c4e267b70b61fd0178704ee9ea39861d413615c3bed258bb1e2c6c6033404dd68961c32f971a7ad4deba434
7
+ data.tar.gz: '0968932404a844a5e52d26fe9e53b02f582214d773767ca71bfd3ce08d57e26a86937bad4ac97811fdc049b60a23253a4045deaa859a966ffc4621ac50e6d0e6'
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ *~
11
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in needful_things.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Dennis Walters
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.
@@ -0,0 +1,60 @@
1
+ # NeedfulThings #
2
+
3
+ A required hash fields checker. At present, it rolls with the assumption that not only is the hash key present, but its associated value is also non-nil.
4
+
5
+ ## Installation ##
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'needful_things'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install needful_things
20
+
21
+ ## Usage ##
22
+
23
+ In lieu of real documentation, here's an example.
24
+
25
+ ```ruby
26
+ require 'needful_things'
27
+
28
+ class SomeOperation
29
+ include NeedfulThings
30
+
31
+ needs name: :widget_id, otherwise: :widget_id_not_present
32
+
33
+ def call(input = {})
34
+ neediness(input) do |needs|
35
+ needs.fulfilled do
36
+ true
37
+ end
38
+
39
+ needs.unmet do |reason|
40
+ false
41
+ end
42
+ end
43
+ end
44
+ end
45
+ ```
46
+
47
+ ## Development ##
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing ##
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ess/needful_things.
56
+
57
+
58
+ ## License ##
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "needful_things"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require 'needful_things/dsl'
2
+ require 'needful_things/instance_methods'
3
+ require 'needful_things/version'
4
+
5
+ module NeedfulThings
6
+ include InstanceMethods
7
+
8
+ def self.included(base)
9
+ base.send :extend, DSL
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require 'needful_things/engine'
2
+
3
+ module NeedfulThings
4
+
5
+ module DSL
6
+ def needs(name:, otherwise:)
7
+ needs_engine.register(name: name, otherwise: otherwise)
8
+ end
9
+
10
+ def needs_engine
11
+ @needs_engine ||= Engine.new
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,38 @@
1
+ require 'dry/monads'
2
+ require 'needful_things/matcher'
3
+
4
+ module NeedfulThings
5
+
6
+ class Engine
7
+ include Dry::Monads[:maybe, :result, :try]
8
+
9
+ def register(name:, otherwise:)
10
+ requirements[name] = otherwise
11
+ end
12
+
13
+ def verify(input = {}, &block)
14
+ Matcher.(
15
+ Maybe(find_unmet(input)).fmap {|need| reason(need)}.to_result.flip,
16
+ &block
17
+ )
18
+ end
19
+
20
+ private
21
+ def requirements
22
+ @requirements ||= {}
23
+ end
24
+
25
+ def find_unmet(input = {})
26
+ needs.reject {|need| Try {input.fetch(need)}.value_or(nil)}.first
27
+ end
28
+
29
+ def needs
30
+ requirements.keys
31
+ end
32
+
33
+ def reason(need)
34
+ requirements.fetch(need)
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,9 @@
1
+ module NeedfulThings
2
+
3
+ module InstanceMethods
4
+ def neediness(input = {}, &block)
5
+ self.class.needs_engine.verify(input, &block)
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,17 @@
1
+ require 'dry/matcher'
2
+
3
+ module NeedfulThings
4
+
5
+ Matcher = Dry::Matcher.new(
6
+ fulfilled: Dry::Matcher::Case.new(
7
+ match: -> result {result.success?},
8
+ resolve: -> result {result.value!}
9
+ ),
10
+
11
+ unmet: Dry::Matcher::Case.new(
12
+ match: -> result {result.failure?},
13
+ resolve: -> result {result.failure}
14
+ )
15
+ )
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ module NeedfulThings
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/needful_things/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "needful_things"
5
+ spec.version = NeedfulThings::VERSION
6
+ spec.authors = ["Dennis Walters"]
7
+ spec.email = ["pooster@gmail.com"]
8
+
9
+ spec.summary = %q{A required hash fields checker}
10
+ spec.homepage = "https://github.com/ess/needful_things-ruby"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/ess/needful_things-ruby"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency 'dry-matcher', '~> 0.8.0'
27
+ spec.add_dependency 'dry-monads', '~> 1.3.0'
28
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: needful_things
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dennis Walters
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-matcher
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.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: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-monads
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.0
41
+ description:
42
+ email:
43
+ - pooster@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/needful_things.rb
56
+ - lib/needful_things/dsl.rb
57
+ - lib/needful_things/engine.rb
58
+ - lib/needful_things/instance_methods.rb
59
+ - lib/needful_things/matcher.rb
60
+ - lib/needful_things/version.rb
61
+ - needful_things.gemspec
62
+ homepage: https://github.com/ess/needful_things-ruby
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ homepage_uri: https://github.com/ess/needful_things-ruby
67
+ source_code_uri: https://github.com/ess/needful_things-ruby
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.3.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.1.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: A required hash fields checker
87
+ test_files: []