service_actor-promptable 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8800d1583285536b5b23e60a3bbf1a54b19df3e388d763c55f93ad4536f1c12c
4
- data.tar.gz: 62d24c3fcc870b8bc69e9a63d7af2eb7c5047cf4ce9414041edfab33b8be7762
3
+ metadata.gz: 86c769e2c2f536f08cf0ee375bdad88b01e06b130a657aaa2cd6c179939a5214
4
+ data.tar.gz: d406cd55a3c94872c88beb19ad894291ac45f89818afeafd8417c07721c6bbdb
5
5
  SHA512:
6
- metadata.gz: cad2618d0d12523d4e4cfc6fe32887ed3f613397c0be38bc9ee83b4ef742faef97afa8698cf9b4932f3a68b8d96462b20b273abc766585b8925cdfd1c237d463
7
- data.tar.gz: 1d0b7712bc6dd38c056bf07935c0440afdf6c5466513524a81b2e158c4ae1c5c48d2831b591403bfe91083e602e9ddb16827ac9575de3c41d7d26b437e6ce2ce
6
+ metadata.gz: c6bdca3a3d7ff922ab88f83eb89535e98deefb6cbf25e8a3df8a25af84367dd253e9b87e4a1ede9c29f513b9db6a65bcce37661d03b23f9341f7aa6dfc8fcb82
7
+ data.tar.gz: 035e23cc95bbb9385bcedf3def851c80e19c2cc469aaf8978257f527a2791602cb0f71a0ad4664101a53d6487771985b2b04de4ddb71eacd651ab578b5b0039b
data/CHANGELOG.md CHANGED
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
13
13
 
14
14
  ### Removed
15
15
 
16
+ ## [1.0.2] - 2022-07-03
17
+ ### Added
18
+ - Supports running unattended, with configurable toggle
19
+
16
20
  ## [1.0.1] - 2022-06-30
17
21
  ### Added
18
22
  - Introspectable Version module
@@ -26,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
26
30
  - DSL => `prompt_with`
27
31
  - Class attribute => `prompt`
28
32
 
29
- [Unreleased]: https://github.com/pboling/service_actor-promptable/compare/v1.0.1...HEAD
33
+ [Unreleased]: https://github.com/pboling/service_actor-promptable/compare/v1.0.2...HEAD
34
+ [1.0.2]: https://github.com/pboling/service_actor-promptable/compare/1.0.1...v1.0.2
30
35
  [1.0.1]: https://github.com/pboling/service_actor-promptable/compare/1.0.0...v1.0.1
31
36
  [1.0.0]: https://github.com/pboling/service_actor-promptable/compare/3079772bb43c02ebfca364d27d062e573acdb091...v1.0.0
data/README.md CHANGED
@@ -167,11 +167,38 @@ class ShouldContinue < Actor
167
167
  def call
168
168
  self.answer = prompt.ask("What is the capital of Assyria?", default: "Uh, I don't know that")
169
169
  # or for a Yes/No query:
170
- # self.answer = prompt.yes?("Do it?") # (Y/n)
170
+ #
171
171
  end
172
172
  end
173
173
  ```
174
174
 
175
+ If you have an actor that may be run unattended, and thus want to skip the prompts:
176
+ ```rb
177
+ require "tty/prompt"
178
+
179
+ class ShouldContinue < Actor
180
+ include ServiceActor::Promptable
181
+
182
+ prompt_with TTY::Prompt.new, unattended_options: { prompt_toggle: :unattended, answer_with: true}
183
+ output :answer, type: String
184
+
185
+ def call
186
+ # When not running unattended, this will prompt as normal.
187
+ # When running unattended it will use the `answer_with` value.
188
+ self.answer = prompt.yes?("Do it?")
189
+ end
190
+ end
191
+
192
+ # Run unattended:
193
+ ShouldContinue.call(unattended: true) # will not prompt, and `answer` will be `true`!
194
+
195
+ # Run the same class, no code changes, but attended:
196
+ ShouldContinue.call(unattended: false) # will prompt!
197
+
198
+ # Attended (unattended: false) is the default for classes that include ServiceActor::Promptable (obviously!)
199
+ ShouldContinue.call # will prompt!
200
+ ```
201
+
175
202
  ### How to test
176
203
 
177
204
  Let's say you have the `ShouldContinue` class above, how do you provide user input to test the Actor?
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ServiceActor
4
+ module Promptable
5
+ # Sometimes a Service Actor should be able to run unattended or attended
6
+ class Unattended < BasicObject
7
+ def initialize(answer_with)
8
+ @answer_with = answer_with
9
+ end
10
+
11
+ def method_missing(_name, *_args)
12
+ ::Kernel.puts "Prompt is NoOp. Running unattended!"
13
+ @answer_with
14
+ end
15
+ end
16
+ end
17
+ end
@@ -3,7 +3,7 @@
3
3
  module ServiceActor
4
4
  module Promptable
5
5
  module Version
6
- VERSION = "1.0.1"
6
+ VERSION = "1.0.2"
7
7
  end
8
8
  end
9
9
  end
@@ -6,11 +6,26 @@ require "version_gem"
6
6
  # This library
7
7
  require "service_actor"
8
8
  require "service_actor/promptable/version"
9
+ require "service_actor/promptable/unattended"
9
10
 
10
11
  module ServiceActor
11
- # Add a prompt to an actor class
12
+ # Adds the `prompt_with` DSL to actors. This allows you to set a prompt interface.
13
+ # It is suggested to use the `tty-prompt` gem, but any prompt tool should work.
14
+ # Then you are able to call `.prompt` and manipulate the prompt during a play.
15
+ #
16
+ # class PromptableActor < Actor
17
+ # prompt_with TTY::Prompt.new
18
+ # # or alternatively
19
+ # def call
20
+ # super
21
+ # self.prompt = TTY::Prompt.new
22
+ # end
23
+ # end
12
24
  module Promptable
13
- class Error < StandardError; end
25
+ UNATTENDED_DEFAULTS = {
26
+ prompt_toggle: :unattended,
27
+ answer_with: true
28
+ }.freeze
14
29
 
15
30
  def self.included(base)
16
31
  base.extend(ClassMethods)
@@ -21,11 +36,20 @@ module ServiceActor
21
36
  def inherited(child)
22
37
  super
23
38
 
24
- child.prompt ||= @prompt
39
+ child.prompt_with(
40
+ prompt,
41
+ unattended_options: ServiceActor::Promptable::UNATTENDED_DEFAULTS.merge(unattended_options).dup
42
+ )
25
43
  end
26
44
 
27
- def prompt_with(prompter)
28
- @prompt = prompter
45
+ def prompt_with(prompter, unattended_options: {})
46
+ unattended_options = ServiceActor::Promptable::UNATTENDED_DEFAULTS.merge(unattended_options)
47
+ self.prompt = prompter
48
+ self.unattended_options = unattended_options
49
+ return unless unattended_options.key?(:prompt_toggle) && unattended_options[:prompt_toggle].is_a?(Symbol)
50
+
51
+ # Create an Actor input :unattended (default) which must be either true or false
52
+ input unattended_options[:prompt_toggle], in: [true, false], allow_nil: false, default: false
29
53
  end
30
54
 
31
55
  def prompt
@@ -35,15 +59,35 @@ module ServiceActor
35
59
  def prompt=(prompter)
36
60
  @prompt = prompter
37
61
  end
62
+
63
+ def unattended_options
64
+ @unattended_options
65
+ end
66
+
67
+ def unattended_options=(unattended_options)
68
+ @unattended_options = unattended_options
69
+ end
38
70
  end
39
71
 
40
72
  def prompt
41
- self.class.prompt
73
+ if self.class.unattended_options[:prompt_toggle] && send(self.class.unattended_options[:prompt_toggle])
74
+ Unattended.new(self.class.unattended_options[:answer_with])
75
+ else
76
+ self.class.prompt
77
+ end
42
78
  end
43
79
 
44
80
  def prompt=(prompter)
45
81
  self.class.prompt = prompter
46
82
  end
83
+
84
+ def unattended_options
85
+ self.class.unattended_options
86
+ end
87
+
88
+ def unattended_options=(unattended_options)
89
+ self.class.unattended_options = unattended_options
90
+ end
47
91
  end
48
92
  end
49
93
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_actor-promptable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-30 00:00:00.000000000 Z
11
+ date: 2022-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: service_actor
@@ -198,6 +198,7 @@ files:
198
198
  - README.md
199
199
  - SECURITY.md
200
200
  - lib/service_actor/promptable.rb
201
+ - lib/service_actor/promptable/unattended.rb
201
202
  - lib/service_actor/promptable/version.rb
202
203
  - sig/service_actor/promptable.rbs
203
204
  homepage: https://github.com/pboling/service_actor-promptable
@@ -205,10 +206,10 @@ licenses:
205
206
  - MIT
206
207
  metadata:
207
208
  homepage_uri: https://github.com/pboling/service_actor-promptable
208
- source_code_uri: https://github.com/pboling/service_actor-promptable/tree/v1.0.1
209
- changelog_uri: https://github.com/pboling/service_actor-promptable/blob/v1.0.1/CHANGELOG.md
209
+ source_code_uri: https://github.com/pboling/service_actor-promptable/tree/v1.0.2
210
+ changelog_uri: https://github.com/pboling/service_actor-promptable/blob/v1.0.2/CHANGELOG.md
210
211
  bug_tracker_uri: https://github.com/pboling/service_actor-promptable/issues
211
- documentation_uri: https://www.rubydoc.info/gems/service_actor-promptable/1.0.1
212
+ documentation_uri: https://www.rubydoc.info/gems/service_actor-promptable/1.0.2
212
213
  wiki_uri: https://github.com/pboling/service_actor-promptable/wiki
213
214
  rubygems_mfa_required: 'true'
214
215
  post_install_message: