consenter 1.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
+ SHA256:
3
+ metadata.gz: 381f1b81e49de4299e3c390f79a9b3a0713496f4324549b32f7dab9cf546b6c0
4
+ data.tar.gz: 213e9cb26ef6d8ea4f7141e2144ffeeef2eb7181d9bc3fba0be231ebdc5e9c65
5
+ SHA512:
6
+ metadata.gz: 43511490721fced00095c3fcbe9c99471ff66a112058c451755e9f64dd50da486092893da23070888e255a243be4ca7ea2c9a7bb92275b40bb193f30d25e8009
7
+ data.tar.gz: 82556a9705b39381c507fca95a5f05274e73c8253430007db1badf9cd84e5ff23371f86d654707349dafa478159f9c7e07fcbfe69d9d29217107393a55b353c4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
data/.pryrc ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/setup'
2
+ require 'consenter'
@@ -0,0 +1,2 @@
1
+ Style/Documentation:
2
+ Enabled: false
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in consenter.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ # Consenter
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/consenter`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'consenter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install consenter
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pvdb/consenter.
@@ -0,0 +1,22 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ # rubocop:disable Style/HashSyntax
11
+
12
+ task :validate_gemspec do
13
+ Bundler.load_gemspec('consenter.gemspec').validate
14
+ end
15
+
16
+ task :version => :validate_gemspec do
17
+ puts Consenter::VERSION
18
+ end
19
+
20
+ task :default => :test
21
+
22
+ # rubocop:enable Style/HashSyntax
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'consenter'
5
+
6
+ require 'pry'
7
+ Pry.start
@@ -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,29 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'consenter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'consenter'
8
+ spec.version = Consenter::VERSION
9
+ spec.authors = ['Peter Vandenberk']
10
+ spec.email = ['pvandenberk@mac.com']
11
+
12
+ spec.summary = 'Idiomatic Enumerable extension to filter by consent'
13
+ spec.description = 'Ask for consent for each element in an Enumerable'
14
+ spec.homepage = 'https://github.com/pvdb/consenter'
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`
18
+ .split("\x0")
19
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'pry', '~> 0.11'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ end
@@ -0,0 +1,88 @@
1
+ # rubocop:disable all
2
+
3
+ require 'consenter/version'
4
+
5
+ require 'io/console'
6
+
7
+ class Consenter # :nodoc:
8
+
9
+ VALID_RESPONSES = {
10
+ 'y' => 'yes to this',
11
+ 'n' => 'no to this',
12
+ 'Y' => 'yes to this and all remaining',
13
+ 'N' => 'no to this and all remaining',
14
+ 'q' => 'quit',
15
+ '?' => 'help',
16
+ }.freeze
17
+
18
+ private_constant :VALID_RESPONSES
19
+
20
+ HELP = VALID_RESPONSES.map { |k, v|
21
+ "#{k} - #{v}"
22
+ }.join($/).freeze
23
+
24
+ private_constant :HELP
25
+
26
+ ANSWERS = VALID_RESPONSES.keys.join(',').freeze
27
+
28
+ private_constant :ANSWERS
29
+
30
+ def initialize(prompt = '%s', inspector = :to_s)
31
+ @prompt = prompt + ' [' + ANSWERS + '] '
32
+ @inspector = inspector
33
+ end
34
+
35
+ def consent_for?(arg)
36
+ key_pressed = \
37
+ case
38
+ when @yes_to_all then 'y'
39
+ when @no_to_all then 'n'
40
+ else
41
+ loop do
42
+ description = case @inspector
43
+ when Symbol then arg.send(@inspector)
44
+ when Method then @inspector.call(arg)
45
+ end
46
+ IO.console.print format(@prompt, description)
47
+ case answer = (key = IO.console.gets) ? key.strip : nil
48
+ when *['y', 'n'] then break answer
49
+ when 'Y' then @yes_to_all = true and break 'y'
50
+ when 'N' then @no_to_all = true and break 'n'
51
+ when 'q' then break nil
52
+ when nil then break nil # CTRL-D
53
+ when '?' then IO.console.puts HELP
54
+ else IO.console.puts HELP
55
+ end
56
+ end
57
+ end
58
+ key_pressed && key_pressed == 'y'
59
+ end
60
+
61
+ private :consent_for?
62
+
63
+ def for(enumerable, &block)
64
+ @yes_to_all = false
65
+ @no_to_all = false
66
+ enumerable.each do |arg|
67
+ case consent_for? arg
68
+ when true then block.yield(arg) # user pressed 'y' or 'Y'
69
+ when false then nil # user pressed 'n' or 'N'
70
+ when nil then break # user pressed 'q'
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ module Enumerable # :nodoc:
78
+ def each_consented(prompt = '%s', inspector = :to_s)
79
+ unless block_given?
80
+ return to_enum(__method__, prompt) do size; end
81
+ end
82
+ Consenter.new(prompt, inspector).for(self) do |*args|
83
+ yield(*args)
84
+ end
85
+ end
86
+ end
87
+
88
+ # rubocop:enable all
@@ -0,0 +1,3 @@
1
+ class Consenter
2
+ VERSION = '1.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Vandenberk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-04 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Ask for consent for each element in an Enumerable
70
+ email:
71
+ - pvandenberk@mac.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".pryrc"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - consenter.gemspec
86
+ - lib/consenter.rb
87
+ - lib/consenter/version.rb
88
+ homepage: https://github.com/pvdb/consenter
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Idiomatic Enumerable extension to filter by consent
111
+ test_files: []