tty-prompt 0.18.0 → 0.22.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 +4 -4
- data/CHANGELOG.md +92 -0
- data/README.md +549 -248
- data/lib/tty-prompt.rb +1 -2
- data/lib/tty/prompt.rb +187 -143
- data/lib/tty/prompt/answers_collector.rb +5 -5
- data/lib/tty/prompt/{enum_paginator.rb → block_paginator.rb} +20 -19
- data/lib/tty/prompt/choice.rb +5 -7
- data/lib/tty/prompt/choices.rb +29 -11
- data/lib/tty/prompt/confirm_question.rb +38 -16
- data/lib/tty/prompt/const.rb +17 -0
- data/lib/tty/prompt/converter_dsl.rb +6 -7
- data/lib/tty/prompt/converter_registry.rb +31 -26
- data/lib/tty/prompt/converters.rb +139 -32
- data/lib/tty/prompt/enum_list.rb +57 -27
- data/lib/tty/prompt/errors.rb +31 -0
- data/lib/tty/prompt/evaluator.rb +1 -1
- data/lib/tty/prompt/expander.rb +39 -13
- data/lib/tty/prompt/keypress.rb +31 -36
- data/lib/tty/prompt/list.rb +175 -65
- data/lib/tty/prompt/mask_question.rb +4 -5
- data/lib/tty/prompt/multi_list.rb +124 -33
- data/lib/tty/prompt/multiline.rb +7 -6
- data/lib/tty/prompt/paginator.rb +38 -26
- data/lib/tty/prompt/question.rb +83 -34
- data/lib/tty/prompt/question/checks.rb +18 -0
- data/lib/tty/prompt/question/validation.rb +3 -3
- data/lib/tty/prompt/selected_choices.rb +76 -0
- data/lib/tty/prompt/slider.rb +83 -16
- data/lib/tty/prompt/statement.rb +3 -3
- data/lib/tty/prompt/suggestion.rb +6 -6
- data/lib/tty/prompt/symbols.rb +58 -34
- data/lib/tty/prompt/test.rb +36 -0
- data/lib/tty/prompt/timer.rb +75 -0
- data/lib/tty/prompt/utils.rb +1 -3
- data/lib/tty/prompt/version.rb +1 -1
- metadata +29 -227
- data/Rakefile +0 -8
- data/examples/ask.rb +0 -7
- data/examples/ask_valid.rb +0 -12
- data/examples/collect.rb +0 -21
- data/examples/echo.rb +0 -11
- data/examples/enum_select.rb +0 -7
- data/examples/enum_select_disabled.rb +0 -16
- data/examples/enum_select_paged.rb +0 -9
- data/examples/enum_select_wrapped.rb +0 -15
- data/examples/expand.rb +0 -29
- data/examples/in.rb +0 -9
- data/examples/inputs.rb +0 -10
- data/examples/key_events.rb +0 -15
- data/examples/keypress.rb +0 -9
- data/examples/mask.rb +0 -13
- data/examples/multi_select.rb +0 -8
- data/examples/multi_select_disabled.rb +0 -17
- data/examples/multi_select_paged.rb +0 -9
- data/examples/multi_select_wrapped.rb +0 -15
- data/examples/multiline.rb +0 -9
- data/examples/pause.rb +0 -9
- data/examples/select.rb +0 -20
- data/examples/select_disabled.rb +0 -18
- data/examples/select_enum.rb +0 -8
- data/examples/select_filtered.rb +0 -11
- data/examples/select_paginated.rb +0 -11
- data/examples/select_wrapped.rb +0 -15
- data/examples/slider.rb +0 -6
- data/examples/validation.rb +0 -9
- data/examples/yes_no.rb +0 -7
- data/lib/tty/prompt/messages.rb +0 -49
- data/lib/tty/prompt/timeout.rb +0 -78
- data/lib/tty/test_prompt.rb +0 -20
- data/spec/spec_helper.rb +0 -45
- data/spec/unit/ask_spec.rb +0 -132
- data/spec/unit/choice/eql_spec.rb +0 -22
- data/spec/unit/choice/from_spec.rb +0 -96
- data/spec/unit/choices/add_spec.rb +0 -12
- data/spec/unit/choices/each_spec.rb +0 -13
- data/spec/unit/choices/find_by_spec.rb +0 -10
- data/spec/unit/choices/new_spec.rb +0 -10
- data/spec/unit/choices/pluck_spec.rb +0 -9
- data/spec/unit/collect_spec.rb +0 -96
- data/spec/unit/converters/convert_bool_spec.rb +0 -58
- data/spec/unit/converters/convert_char_spec.rb +0 -11
- data/spec/unit/converters/convert_custom_spec.rb +0 -14
- data/spec/unit/converters/convert_date_spec.rb +0 -34
- data/spec/unit/converters/convert_file_spec.rb +0 -18
- data/spec/unit/converters/convert_number_spec.rb +0 -39
- data/spec/unit/converters/convert_path_spec.rb +0 -15
- data/spec/unit/converters/convert_range_spec.rb +0 -22
- data/spec/unit/converters/convert_regex_spec.rb +0 -12
- data/spec/unit/converters/convert_string_spec.rb +0 -21
- data/spec/unit/converters/on_error_spec.rb +0 -9
- data/spec/unit/distance/distance_spec.rb +0 -73
- data/spec/unit/enum_paginator_spec.rb +0 -75
- data/spec/unit/enum_select_spec.rb +0 -446
- data/spec/unit/error_spec.rb +0 -20
- data/spec/unit/evaluator_spec.rb +0 -67
- data/spec/unit/expand_spec.rb +0 -198
- data/spec/unit/keypress_spec.rb +0 -72
- data/spec/unit/mask_spec.rb +0 -132
- data/spec/unit/multi_select_spec.rb +0 -495
- data/spec/unit/multiline_spec.rb +0 -77
- data/spec/unit/new_spec.rb +0 -20
- data/spec/unit/ok_spec.rb +0 -10
- data/spec/unit/paginator_spec.rb +0 -73
- data/spec/unit/question/checks_spec.rb +0 -97
- data/spec/unit/question/default_spec.rb +0 -31
- data/spec/unit/question/echo_spec.rb +0 -38
- data/spec/unit/question/in_spec.rb +0 -115
- data/spec/unit/question/initialize_spec.rb +0 -12
- data/spec/unit/question/modifier/apply_to_spec.rb +0 -24
- data/spec/unit/question/modifier/letter_case_spec.rb +0 -41
- data/spec/unit/question/modifier/whitespace_spec.rb +0 -51
- data/spec/unit/question/modify_spec.rb +0 -41
- data/spec/unit/question/required_spec.rb +0 -92
- data/spec/unit/question/validate_spec.rb +0 -115
- data/spec/unit/question/validation/call_spec.rb +0 -31
- data/spec/unit/question/validation/coerce_spec.rb +0 -30
- data/spec/unit/result_spec.rb +0 -40
- data/spec/unit/say_spec.rb +0 -67
- data/spec/unit/select_spec.rb +0 -643
- data/spec/unit/slider_spec.rb +0 -100
- data/spec/unit/statement/initialize_spec.rb +0 -15
- data/spec/unit/subscribe_spec.rb +0 -22
- data/spec/unit/suggest_spec.rb +0 -28
- data/spec/unit/warn_spec.rb +0 -21
- data/spec/unit/yes_no_spec.rb +0 -251
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-prompt.gemspec +0 -33
@@ -6,7 +6,7 @@ module TTY
|
|
6
6
|
# Initialize answer collector
|
7
7
|
#
|
8
8
|
# @api public
|
9
|
-
def initialize(prompt, options
|
9
|
+
def initialize(prompt, **options)
|
10
10
|
@prompt = prompt
|
11
11
|
@answers = options.fetch(:answers) { {} }
|
12
12
|
end
|
@@ -25,7 +25,7 @@ module TTY
|
|
25
25
|
# Create answer entry
|
26
26
|
#
|
27
27
|
# @example
|
28
|
-
# key(:name).ask(
|
28
|
+
# key(:name).ask("Name?")
|
29
29
|
#
|
30
30
|
# @api public
|
31
31
|
def key(name, &block)
|
@@ -40,7 +40,7 @@ module TTY
|
|
40
40
|
# Change to collect all values for a key
|
41
41
|
#
|
42
42
|
# @example
|
43
|
-
# key(:colors).values.ask(
|
43
|
+
# key(:colors).values.ask("Color?")
|
44
44
|
#
|
45
45
|
# @api public
|
46
46
|
def values(&block)
|
@@ -69,8 +69,8 @@ module TTY
|
|
69
69
|
private
|
70
70
|
|
71
71
|
# @api private
|
72
|
-
def method_missing(method, *args, &block)
|
73
|
-
answer = @prompt.public_send(method, *args, &block)
|
72
|
+
def method_missing(method, *args, **options, &block)
|
73
|
+
answer = @prompt.public_send(method, *args, **options, &block)
|
74
74
|
add_answer(answer)
|
75
75
|
end
|
76
76
|
end # AnswersCollector
|
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "paginator"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Prompt
|
7
|
-
class
|
7
|
+
class BlockPaginator < Paginator
|
8
8
|
# Paginate list of choices based on current active choice.
|
9
9
|
# Move entire pages.
|
10
10
|
#
|
@@ -17,8 +17,8 @@ module TTY
|
|
17
17
|
|
18
18
|
# Don't paginate short lists
|
19
19
|
if list.size <= @per_page
|
20
|
-
@
|
21
|
-
@
|
20
|
+
@start_index = 0
|
21
|
+
@end_index = list.size - 1
|
22
22
|
if block
|
23
23
|
return list.each_with_index(&block)
|
24
24
|
else
|
@@ -32,25 +32,26 @@ module TTY
|
|
32
32
|
page = (@last_index / @per_page.to_f).ceil
|
33
33
|
pages = (list.size / @per_page.to_f).ceil
|
34
34
|
if page == 0
|
35
|
-
@
|
36
|
-
@
|
37
|
-
elsif page > 0 && page
|
38
|
-
@
|
39
|
-
@
|
35
|
+
@start_index = 0
|
36
|
+
@end_index = @start_index + @per_page - 1
|
37
|
+
elsif page > 0 && page < pages
|
38
|
+
@start_index = (page - 1) * @per_page
|
39
|
+
@end_index = @start_index + @per_page - 1
|
40
|
+
elsif page == pages
|
41
|
+
@start_index = (page - 1) * @per_page
|
42
|
+
@end_index = list.size - 1
|
40
43
|
else
|
41
|
-
@
|
42
|
-
@
|
44
|
+
@end_index = list.size - 1
|
45
|
+
@start_index = @end_index - @per_page + 1
|
43
46
|
end
|
44
47
|
|
45
|
-
sliced_list = list[@
|
46
|
-
|
48
|
+
sliced_list = list[@start_index..@end_index]
|
49
|
+
page_range = (@start_index..@end_index)
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
else
|
53
|
-
sliced_list.zip(indices).to_enum unless block_given?
|
51
|
+
return sliced_list.zip(page_range).to_enum unless block_given?
|
52
|
+
|
53
|
+
sliced_list.each_with_index do |item, index|
|
54
|
+
block[item, @start_index + index]
|
54
55
|
end
|
55
56
|
end
|
56
57
|
end # EnumPaginator
|
data/lib/tty/prompt/choice.rb
CHANGED
@@ -15,7 +15,7 @@ module TTY
|
|
15
15
|
# Choice.from([:foo, 1])
|
16
16
|
# # => <TTY::Prompt::Choice @key=nil @name="foo" @value=1 @disabled=false>
|
17
17
|
#
|
18
|
-
# Choice.from({name: :foo, value: 1, key:
|
18
|
+
# Choice.from({name: :foo, value: 1, key: "f"}
|
19
19
|
# # => <TTY::Prompt::Choice @key="f" @name="foo" @value=1 @disabled=false>
|
20
20
|
#
|
21
21
|
# @param [Object] val
|
@@ -30,19 +30,17 @@ module TTY
|
|
30
30
|
case val
|
31
31
|
when Choice
|
32
32
|
val
|
33
|
-
when String, Symbol
|
34
|
-
new(val, val)
|
35
33
|
when Array
|
36
34
|
name, value, options = *val
|
37
35
|
if name.is_a?(Hash)
|
38
36
|
convert_hash(name)
|
39
37
|
else
|
40
|
-
new(name.to_s, value
|
38
|
+
new(name.to_s, (value.nil? ? name.to_s : value), **(options || {}))
|
41
39
|
end
|
42
40
|
when Hash
|
43
41
|
convert_hash(val)
|
44
42
|
else
|
45
|
-
|
43
|
+
new(val, val)
|
46
44
|
end
|
47
45
|
end
|
48
46
|
|
@@ -51,9 +49,9 @@ module TTY
|
|
51
49
|
# @api public
|
52
50
|
def self.convert_hash(val)
|
53
51
|
if val.key?(:name) && val.key?(:value)
|
54
|
-
new(val[:name].to_s, val[:value], val)
|
52
|
+
new(val[:name].to_s, val[:value], **val)
|
55
53
|
elsif val.key?(:name)
|
56
|
-
new(val[:name].to_s, val[:name].to_s, val)
|
54
|
+
new(val[:name].to_s, val[:name].to_s, **val)
|
57
55
|
else
|
58
56
|
new(val.keys.first.to_s, val.values.first)
|
59
57
|
end
|
data/lib/tty/prompt/choices.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "forwardable"
|
4
4
|
|
5
|
-
require_relative
|
5
|
+
require_relative "choice"
|
6
6
|
|
7
7
|
module TTY
|
8
8
|
class Prompt
|
@@ -13,14 +13,8 @@ module TTY
|
|
13
13
|
include Enumerable
|
14
14
|
extend Forwardable
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
# @return [Array[Choice]]
|
19
|
-
#
|
20
|
-
# @api public
|
21
|
-
attr_reader :choices
|
22
|
-
|
23
|
-
def_delegators :choices, :length, :size, :to_ary, :empty?, :values_at
|
16
|
+
def_delegators :choices, :length, :size, :to_ary, :empty?,
|
17
|
+
:values_at, :index, :==
|
24
18
|
|
25
19
|
# Convenience for creating choices
|
26
20
|
#
|
@@ -47,6 +41,20 @@ module TTY
|
|
47
41
|
end
|
48
42
|
end
|
49
43
|
|
44
|
+
# Scope of choices which are not disabled
|
45
|
+
#
|
46
|
+
# @api public
|
47
|
+
def enabled
|
48
|
+
reject(&:disabled?)
|
49
|
+
end
|
50
|
+
|
51
|
+
def enabled_indexes
|
52
|
+
each_with_index.reduce([]) do |acc, (choice, idx)|
|
53
|
+
acc << idx unless choice.disabled?
|
54
|
+
acc
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
50
58
|
# Iterate over all choices in the collection
|
51
59
|
#
|
52
60
|
# @yield [Choice]
|
@@ -93,7 +101,7 @@ module TTY
|
|
93
101
|
# Find a matching choice
|
94
102
|
#
|
95
103
|
# @exmaple
|
96
|
-
# choices.find_by(:name,
|
104
|
+
# choices.find_by(:name, "small")
|
97
105
|
#
|
98
106
|
# @param [Symbol] attr
|
99
107
|
# the attribute name
|
@@ -105,6 +113,16 @@ module TTY
|
|
105
113
|
def find_by(attr, value)
|
106
114
|
find { |choice| choice.public_send(attr) == value }
|
107
115
|
end
|
116
|
+
|
117
|
+
protected
|
118
|
+
|
119
|
+
# The actual collection choices
|
120
|
+
#
|
121
|
+
# @return [Array[Choice]]
|
122
|
+
#
|
123
|
+
# @api private
|
124
|
+
|
125
|
+
attr_reader :choices
|
108
126
|
end # Choices
|
109
127
|
end # Prompt
|
110
128
|
end # TTY
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
3
|
+
require_relative "question"
|
4
|
+
require_relative "utils"
|
5
5
|
|
6
6
|
module TTY
|
7
7
|
class Prompt
|
@@ -14,7 +14,7 @@ module TTY
|
|
14
14
|
# @option options [String] :negative
|
15
15
|
#
|
16
16
|
# @api public
|
17
|
-
def initialize(prompt, options
|
17
|
+
def initialize(prompt, **options)
|
18
18
|
super
|
19
19
|
@suffix = options.fetch(:suffix) { UndefinedSetting }
|
20
20
|
@positive = options.fetch(:positive) { UndefinedSetting }
|
@@ -73,9 +73,9 @@ module TTY
|
|
73
73
|
def render_question
|
74
74
|
header = "#{@prefix}#{message} "
|
75
75
|
if !@done
|
76
|
-
header += @prompt.decorate("(#{@suffix})", @help_color) +
|
76
|
+
header += @prompt.decorate("(#{@suffix})", @help_color) + " "
|
77
77
|
else
|
78
|
-
answer =
|
78
|
+
answer = conversion.call(@input)
|
79
79
|
label = answer ? @positive : @negative
|
80
80
|
header += @prompt.decorate(label, @active_color)
|
81
81
|
end
|
@@ -85,34 +85,56 @@ module TTY
|
|
85
85
|
|
86
86
|
protected
|
87
87
|
|
88
|
+
# Decide how to handle input from user
|
89
|
+
#
|
90
|
+
# @api private
|
91
|
+
def process_input(question)
|
92
|
+
@input = read_input(question)
|
93
|
+
if Utils.blank?(@input)
|
94
|
+
@input = default ? positive : negative
|
95
|
+
end
|
96
|
+
@evaluator.call(@input)
|
97
|
+
end
|
98
|
+
|
88
99
|
# @api private
|
89
100
|
def setup_defaults
|
101
|
+
infer_default
|
102
|
+
@convert = conversion
|
90
103
|
return if suffix? && positive?
|
91
104
|
|
92
105
|
if suffix? && (!positive? || !negative?)
|
93
|
-
parts = @suffix.split(
|
106
|
+
parts = @suffix.split("/")
|
94
107
|
@positive = parts[0]
|
95
108
|
@negative = parts[1]
|
96
|
-
@convert = conversion
|
97
109
|
elsif !suffix? && positive?
|
98
110
|
@suffix = create_suffix
|
99
|
-
@convert = conversion
|
100
111
|
else
|
101
112
|
create_default_labels
|
102
|
-
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# @api private
|
117
|
+
def infer_default
|
118
|
+
converted = Converters.convert(:bool, default.to_s)
|
119
|
+
if converted == Const::Undefined
|
120
|
+
raise InvalidArgument, "default needs to be `true` or `false`"
|
121
|
+
else
|
122
|
+
default(converted)
|
103
123
|
end
|
104
124
|
end
|
105
125
|
|
106
126
|
# @api private
|
107
127
|
def create_default_labels
|
108
|
-
@suffix = default ?
|
109
|
-
@positive = default ?
|
110
|
-
@negative = default ?
|
128
|
+
@suffix = default ? "Y/n" : "y/N"
|
129
|
+
@positive = default ? "Yes" : "yes"
|
130
|
+
@negative = default ? "no" : "No"
|
131
|
+
@validation = /^(y(es)?|no?)$/i
|
132
|
+
@messages[:valid?] = "Invalid input."
|
111
133
|
end
|
112
134
|
|
113
135
|
# @api private
|
114
136
|
def create_suffix
|
115
|
-
(default ? positive.capitalize : positive.downcase) +
|
137
|
+
(default ? positive.capitalize : positive.downcase) + "/" +
|
116
138
|
(default ? negative.downcase : negative.capitalize)
|
117
139
|
end
|
118
140
|
|
@@ -120,12 +142,12 @@ module TTY
|
|
120
142
|
#
|
121
143
|
# @api private
|
122
144
|
def conversion
|
123
|
-
|
145
|
+
->(input) do
|
124
146
|
positive_word = Regexp.escape(positive)
|
125
147
|
positive_letter = Regexp.escape(positive[0])
|
126
|
-
pattern = Regexp.new("
|
148
|
+
pattern = Regexp.new("^(#{positive_word}|#{positive_letter})$", true)
|
127
149
|
!input.match(pattern).nil?
|
128
|
-
|
150
|
+
end
|
129
151
|
end
|
130
152
|
end # ConfirmQuestion
|
131
153
|
end # Prompt
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Prompt
|
5
|
+
module Const
|
6
|
+
Undefined = Object.new.tap do |obj|
|
7
|
+
def obj.to_s
|
8
|
+
"undefined"
|
9
|
+
end
|
10
|
+
|
11
|
+
def obj.inspect
|
12
|
+
"undefined".inspect
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end # Const
|
16
|
+
end # Prompt
|
17
|
+
end # TTY
|
@@ -1,21 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "converter_registry"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Prompt
|
7
7
|
module ConverterDSL
|
8
8
|
def converter_registry
|
9
|
-
@
|
9
|
+
@__converter_registry ||= ConverterRegistry.new
|
10
10
|
end
|
11
11
|
|
12
|
-
def converter(
|
13
|
-
|
14
|
-
self
|
12
|
+
def converter(*names, &block)
|
13
|
+
converter_registry.register(*names, &block)
|
15
14
|
end
|
16
15
|
|
17
|
-
def convert(name,
|
18
|
-
|
16
|
+
def convert(name, input)
|
17
|
+
converter_registry[name].call(input)
|
19
18
|
end
|
20
19
|
end # ConverterDSL
|
21
20
|
end # Prompt
|
@@ -1,60 +1,65 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "forwardable"
|
4
|
+
|
3
5
|
module TTY
|
4
6
|
class Prompt
|
5
7
|
# Immutable collection of converters for type transformation
|
6
8
|
#
|
7
9
|
# @api private
|
8
10
|
class ConverterRegistry
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
def_delegators "@__registry", :keys
|
14
|
+
|
9
15
|
# Create a registry of conversions
|
10
16
|
#
|
11
17
|
# @param [Hash] registry
|
12
18
|
#
|
13
19
|
# @api private
|
14
20
|
def initialize(registry = {})
|
15
|
-
@
|
16
|
-
freeze
|
21
|
+
@__registry = registry.dup
|
17
22
|
end
|
18
23
|
|
19
|
-
#
|
24
|
+
# Check if conversion is available
|
20
25
|
#
|
21
|
-
# @param [
|
22
|
-
#
|
26
|
+
# @param [String] name
|
27
|
+
#
|
28
|
+
# @return [Boolean]
|
23
29
|
#
|
24
30
|
# @api public
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
if key?(name)
|
29
|
-
raise ArgumentError,
|
30
|
-
"Converter for #{name.inspect} already registered"
|
31
|
-
end
|
32
|
-
self.class.new(@_registry.merge(name => item))
|
31
|
+
def contain?(name)
|
32
|
+
conv_name = name.to_s.downcase.to_sym
|
33
|
+
@__registry.key?(conv_name)
|
33
34
|
end
|
34
35
|
|
35
|
-
#
|
36
|
+
# Register a conversion
|
36
37
|
#
|
37
|
-
# @
|
38
|
+
# @param [Symbol] name
|
39
|
+
# the converter name
|
38
40
|
#
|
39
41
|
# @api public
|
40
|
-
def
|
41
|
-
|
42
|
+
def register(*names, &block)
|
43
|
+
names.each do |name|
|
44
|
+
if contain?(name)
|
45
|
+
raise ConversionAlreadyDefined,
|
46
|
+
"converter for #{name.inspect} is already registered"
|
47
|
+
end
|
48
|
+
@__registry[name] = block
|
49
|
+
end
|
42
50
|
end
|
43
51
|
|
44
52
|
# Execute converter
|
45
53
|
#
|
46
54
|
# @api public
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
raise ArgumentError, "#{name.inspect} is not registered"
|
53
|
-
end
|
55
|
+
def [](name)
|
56
|
+
conv_name = name.to_s.downcase.to_sym
|
57
|
+
@__registry.fetch(conv_name) do
|
58
|
+
raise UnsupportedConversion,
|
59
|
+
"converter #{conv_name.inspect} is not registered"
|
54
60
|
end
|
55
|
-
converter[input]
|
56
61
|
end
|
57
|
-
alias []
|
62
|
+
alias fetch []
|
58
63
|
|
59
64
|
def inspect
|
60
65
|
@_registry.inspect
|