io-nosey 0.2.0 → 0.3.0

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: 111214e56dcc9cf9b55891ba716b6e4266866266b8ec0507ad94051ae45f62a2
4
- data.tar.gz: 96cbbe6e5110a70ad544cec13b8c2021e8b1d02405cf1c15fe83bcdf20cb456d
3
+ metadata.gz: 7c3371b02205823ed3d7a181198354c87c16841f3ef51f100b5e3274dbb3e653
4
+ data.tar.gz: 1bf808b096e9c92388f98bb5b751aee16addfebd19aa7ce042381aa101b07ba3
5
5
  SHA512:
6
- metadata.gz: 19c76e2ecf1674db08bfdb888f3f8e6ab8a4012d0baa33be5c79d3701176cbead262fb7825db309b26141497eccaf6d7e4c2507d3e053b47e2e2717e589560a0
7
- data.tar.gz: ca9fbafa3bbd7af04bbe2eee47fc83fa0bd6436236c01e878625f0e9fa5f7c31f5ceadbfda05c60d3c3be09545d0ca7bbae019e696c90e1e9acd97c07d13903d
6
+ metadata.gz: 4d7795f82026327fa492fa2693e503279bf01e33a05cef6c06d2bd0b5e6fa69438e702fd01f6954d5fb12e4b0f89a6e1ac5798a8a2548b21869110f12cada086
7
+ data.tar.gz: 0015f37c5bb9a12d26461bad236c9ba140fb69162455990dae23956c6ff916dedd0bfbae73696d4b2e3bc8e7af39b2a30933f80eb6a17c27a86c339791a0d7dc
data/README.md CHANGED
@@ -21,32 +21,37 @@ Try below scenario on REPL. Or execute this repository's [./bin/console](bin/con
21
21
 
22
22
  ```ruby
23
23
  require 'io/nosey'
24
- include IO::Nosey
24
+
25
+ parker = IO::Nosey::Parker.new
25
26
 
26
27
  # input validation
27
- ask 'What\'s your name?("first_name last_name"): ', input: /\A\w+ \w+\z/
28
+ parker.ask('What\'s your name?("first_name last_name"): ', input: /\A\w+ \w+\z/)
28
29
 
29
30
  # mix the validations and procedures
30
- ask 'How old are you?: ', input: /\A(\d+)\z/,
31
- parse: ->s{Integer s},
32
- return: 10..100
31
+ parker.ask('How old are you?: ',
32
+ input: /\A(\d+)\z/,
33
+ parse: ->s{Integer s},
34
+ return: 10..100
35
+ )
33
36
 
34
37
  # default value
35
- ask "What's your favorite?: ", default: 'ruby'
38
+ parker.ask("What's your favorite?: ", default: 'ruby')
36
39
 
37
40
  # yes or no
38
- agree? 'Do you like this API?: '
41
+ parker.agree?('Do you like this API?: ')
39
42
 
40
- # for password
41
- ask "What's your password?: ", echo: false
43
+ # for password and secrets
44
+ parker.ask("What's your password?: ", echo: false)
42
45
 
43
46
  # choose one from collections
44
- choose('Which Five do you like?: ', 5 => '5 is a Integer',
45
- 'five' => '"five" is a String',
46
- :FIVE => ':FIVE is a Symbol')
47
+ parker.choose('Which Five do you like?: ',
48
+ 5 => '5 is a Integer',
49
+ 'five' => '"five" is a String',
50
+ :FIVE => ':FIVE is a Symbol'
51
+ )
47
52
 
48
53
  # multi line mode
49
- ask 'Write your poem and exit ctrl+d :) ', multi_line: true
54
+ parker.ask('Write your poem and exit ctrl+d :) ', multi_line: true)
50
55
  ```
51
56
 
52
57
  ## Features
data/lib/io/nosey.rb CHANGED
@@ -3,20 +3,16 @@
3
3
 
4
4
  # Copyright (C) 2011 Kenichi Kamiya
5
5
 
6
- require 'forwardable'
7
6
  require 'eqq'
8
- require 'validation'
9
7
  require 'optionalargument'
10
- require_relative 'nosey/version'
11
- require_relative 'nosey/noseyparker'
12
8
 
13
9
  class IO
14
10
  module Nosey
15
- extend Forwardable
16
- private_class_method(*Forwardable.instance_methods(false))
17
-
18
- NOSEY_PARKER = NoseyParker.new
19
-
20
- def_delegators :NOSEY_PARKER, :ask, :agree?, :choose
11
+ def self.parker(**kw_args, &block)
12
+ Parker.new(**kw_args).instance_exec(&block)
13
+ end
21
14
  end
22
15
  end
16
+
17
+ require_relative 'nosey/parker'
18
+ require_relative 'nosey/version'
@@ -3,27 +3,32 @@
3
3
 
4
4
  class IO
5
5
  module Nosey
6
- class NoseyParker
7
- extend Forwardable
8
- private_class_method(*Forwardable.instance_methods(false))
9
- include Validation
10
- include Eqq::Buildable
11
- include Validation::Adjustment
12
-
13
- class InvalidInputError < InvalidError; end
6
+ class Parker
7
+ class Error < StandardError; end
8
+ class InvalidInputError < Error; end
9
+
10
+ def self.adjustable?(object)
11
+ case object
12
+ when Proc
13
+ object.arity == 1
14
+ else
15
+ if object.respond_to?(:to_proc)
16
+ object.to_proc.arity == 1
17
+ else
18
+ false
19
+ end
20
+ end
21
+ end
14
22
 
15
23
  # @param input [IO, StringIO]
16
24
  # @param output [IO, StringIO]
17
- def initialize(input=$stdin, output=$stdout)
25
+ def initialize(input: $stdin, output: $stdout)
18
26
  @input, @output = input, output
19
27
  end
20
28
 
21
- def_delegators :@input, :gets, :getc, :getch, :read, :noecho, :raw, :winsize
22
- def_delegators :@output, :print, :puts, :flush, :<<
23
-
24
29
  AskOpts = OptionalArgument.define {
25
30
  opt(:input, condition: Regexp)
26
- opt(:parse, aliases: [:parser], condition: ->v { Validation::Adjustment.adjustable?(v) })
31
+ opt(:parse, aliases: [:parser], condition: ->v { Parker.adjustable?(v) })
27
32
  opt(:return, condition: ->v { Eqq.valid?(v) })
28
33
  opt(:default, condition: CAN(:to_str))
29
34
  opt(:echo, condition: BOOLEAN(), default: true)
@@ -32,36 +37,28 @@ class IO
32
37
  }
33
38
 
34
39
  # @param prompt [String]
35
- # @param options [Hash]
36
- # @option options [Regexp] :input
37
- # @option options [Proc] :parse (also :parser)
38
- # @option options [Proc, Method, #===] :return
39
- # @option options [String, #to_str] :default
40
- # @option options [Boolean] :echo
41
- # @option options [String, #to_str] :error
42
- # @option options [Boolean] :multi_line
43
40
  def ask(prompt, **kw_args)
44
41
  opts = AskOpts.parse(kw_args)
45
42
 
46
- print(prompt)
43
+ @output.print(prompt)
47
44
 
48
45
  if opts.default?
49
- print("(default: #{opts.default})")
46
+ @output.print("(default: #{opts.default})")
50
47
  end
51
48
 
52
49
  if opts.multi_line
53
50
  if opts.echo
54
- input = read
51
+ input = @input.read
55
52
  else
56
- input = noecho(&:read)
57
- puts
53
+ input = @input.noecho(&:read)
54
+ @output.puts
58
55
  end
59
56
  else
60
57
  if opts.echo
61
- input = gets.chomp
58
+ input = @input.gets.chomp
62
59
  else
63
- input = noecho(&:gets).chomp
64
- puts
60
+ input = @input.noecho(&:gets).chomp
61
+ @output.puts
65
62
  end
66
63
  end
67
64
 
@@ -69,7 +66,7 @@ class IO
69
66
  input = opts.default
70
67
  end
71
68
 
72
- if opts.input? && !_valid?(opts.input, input)
69
+ if opts.input? && !valid?(opts.input, input)
73
70
  raise InvalidInputError, opts.error
74
71
  end
75
72
 
@@ -77,22 +74,22 @@ class IO
77
74
  input = opts.parse.call(input)
78
75
  end
79
76
 
80
- if opts.return? && !_valid?(opts.return, input)
77
+ if opts.return? && !valid?(opts.return, input)
81
78
  raise InvalidInputError, opts.error
82
79
  end
83
80
 
84
81
  input
85
- rescue InvalidError
86
- puts $!.message unless $!.message.empty?
82
+ rescue InvalidInputError
83
+ @output.puts $!.message unless $!.message.empty?
87
84
  retry
88
85
  end
89
86
 
90
87
  # @param prompt [String]
91
88
  def agree?(prompt)
92
- print("#{prompt} [y or n]")
89
+ @output.print("#{prompt} [y or n]")
93
90
 
94
- input = getch
95
- print("\n")
91
+ input = @input.getch
92
+ @output.print("\n")
96
93
 
97
94
  case input
98
95
  when 'n', 'N'
@@ -107,35 +104,45 @@ class IO
107
104
  end
108
105
 
109
106
  # @param prompt [String]
110
- # @param choices [Hash, #each_pair] key: value, value: description
107
+ # @param choices [Hash{Object => String}] key: value, value: description
111
108
  # @return a member of choices
112
109
  def choose(prompt, choices)
113
- raise ArgumentError unless valid_choices?(choices
114
- )
110
+ raise ArgumentError if choices.empty?
115
111
 
116
- puts prompt
117
- puts [:index, :value, :description].join("\t")
112
+ @output.puts prompt
113
+ @output.puts [:index, :value, :description].join("\t")
118
114
 
119
115
  pairs = {}
120
116
  index = 1
121
117
  choices.each_pair do |value, description|
122
- puts [index, value, description].join("\t")
118
+ @output.puts [index, value, description].join("\t")
123
119
  pairs[index] = value
124
120
  index += 1
125
121
  end
126
122
 
127
123
  number = ask('Select index:',
128
124
  input: /\A(\d+)\z/,
129
- parse: PARSE(Integer),
130
- return: AND(Integer, 1..(index - 1)))
125
+ parse: ->v { Integer(v) },
126
+ return: Eqq.AND(Integer, 1..(index - 1)))
131
127
 
132
128
  pairs[number]
133
129
  end
134
130
 
135
131
  private
136
132
 
137
- def valid_choices?(choices)
138
- choices.keys.length >= 1
133
+ # @param [Proc, Method, #===] pattern
134
+ # @param [Object] value
135
+ def valid?(pattern, value)
136
+ !!(
137
+ case pattern
138
+ when Proc
139
+ instance_exec(value, &pattern)
140
+ when Method
141
+ pattern.call(value)
142
+ else
143
+ pattern === value
144
+ end
145
+ )
139
146
  end
140
147
  end
141
148
  end
@@ -3,6 +3,6 @@
3
3
 
4
4
  class IO
5
5
  module Nosey
6
- VERSION = '0.2.0'.freeze
6
+ VERSION = '0.3.0'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-nosey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenichi Kamiya
@@ -44,26 +44,6 @@ dependencies:
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: 0.1.0
47
- - !ruby/object:Gem::Dependency
48
- name: validation
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: 0.3.0
54
- - - "<"
55
- - !ruby/object:Gem::Version
56
- version: 0.4.0
57
- type: :runtime
58
- prerelease: false
59
- version_requirements: !ruby/object:Gem::Requirement
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: 0.3.0
64
- - - "<"
65
- - !ruby/object:Gem::Version
66
- version: 0.4.0
67
47
  - !ruby/object:Gem::Dependency
68
48
  name: optionalargument
69
49
  requirement: !ruby/object:Gem::Requirement
@@ -94,7 +74,7 @@ files:
94
74
  - MIT-LICENSE.txt
95
75
  - README.md
96
76
  - lib/io/nosey.rb
97
- - lib/io/nosey/noseyparker.rb
77
+ - lib/io/nosey/parker.rb
98
78
  - lib/io/nosey/version.rb
99
79
  homepage: https://github.com/kachick/io-nosey
100
80
  licenses: