better_input 0.1.0 → 0.1.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: 8bf1091645c1a560391ce297d8782434ef4cab23082ace9dd66130713ba444c8
4
- data.tar.gz: 887189e5e74a48ba84004c097964af27cd8558b8ee45c2a66dc3888e6fadf376
3
+ metadata.gz: 91f6acc7fd325931d683a59962503b1f3fc6dec3a829bb1752b88dcac0e27032
4
+ data.tar.gz: 17f6a25540c3643f6bf1173db8189489327286749b221accbcbee451cff4cbc3
5
5
  SHA512:
6
- metadata.gz: 38f8acda9933caa9d733c9b9e109fd39dd58b2ad75f4825cb38501eb642a46af66a6767fa1a4a990a2dc73ca76cb3608a309595f7eb84cc7baeccb3f8a2a2e79
7
- data.tar.gz: cd5675272a7f21a79c493375af3185221eada8c2b474c3570487885ae637e93dfcecf2ebe23870c319dc79d6b97524d8220d8934ed85176e45c3bb47af4ecfed
6
+ metadata.gz: 475f4143667b67e376c2188641d217fde1f3afdca6b14694c4ae51dd3ab226b805ef1753f29ac2297d0a93a681d322d35e7eca1ba242d55d9647d0fa219fbce4
7
+ data.tar.gz: c49f044e3f1799a619f0d15771a6084d9a8fb2a3ad6692cf1ab520079d124c43474a2d703e1f9ff81b2b0bd15b1f51371ae4bf0d7bf2be9607554154572b7e58
data/CHANGELOG.md CHANGED
File without changes
data/README.md CHANGED
@@ -1,31 +1,101 @@
1
- # BetterInput
1
+ # Ruby Better Input
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- 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/better_input`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Whenever I tried to make an application that would be based on a command line interface (CLI) I found myself having to do some of the things included in this gem, especially when it came to "verbose", when I wanted to display a question to the user or even check if he entered the input I expected. Tired of always coding the same function I decided to make this gem.
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ To install this gem just use:
8
+
9
+ ```shell
10
+ gem install better_input
11
+ ```
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
12
13
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+ ## Usage
15
+ Normally to handle user input in ruby ​you would do something like this:
16
+ ```ruby
17
+ puts "What is your age?"
18
+ age = gets.chomp
19
+ ```
20
+ and then if you wanted a "verbose" input you would have to do another PUTS. And it gets even worse if you want to check if the input is of the expected type:
21
+ ```ruby
22
+ puts "What is your age?"
14
23
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
24
+ age = nil
25
+ loop do
26
+ input = gets.chomp
27
+ if input.match?(/^\d+$/)
28
+ age = input.to_i
29
+ break
30
+ else
31
+ puts "Please enter a valid integer for your age."
32
+ end
33
+ end
16
34
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
35
+ puts "User response: #{age}"
36
+ ```
37
+ Although it requires other approaches like the one I did in the gem. here are some examples of how it works with inputs in ruby ​​using the gem:
38
+ ```ruby
39
+ require 'better_input'
18
40
 
19
- ## Usage
41
+ age = Bi.input("Digite um número: ", type: "int", show_response: true)
42
+ # In a single line we asked the question and stored it in a variable,
43
+ # we made sure it was the type we wanted and we even made the answer appear!
44
+ ```
45
+ ```ruby
46
+ #Another examples:
20
47
 
21
- TODO: Write usage instructions here
48
+ noquestion = Bi.input()
49
+ retype = Bi.input(show_response: true)
50
+ onlyfloat = Bi.input(type: float)
22
51
 
23
- ## Development
52
+ name = Bi.input("What is your name?")
53
+ puts "Your name is #{name}"
54
+ ```
55
+ Anyway, use your creativity, now receiving constant user input for CLI has become less laborious!
24
56
 
25
- 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.
57
+ ## Function before becoming a gem
58
+ This was more or less the base that I ended up making, I made some small improvements that you can find in the files present in this repository before turning it into a gem.
59
+ ```ruby
60
+ def input(question = nil, show_response: false, type: "string")
61
+ unless question.nil?
62
+ puts question
63
+ end
26
64
 
27
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65
+ loop do
66
+ var = gets.chomp
28
67
 
29
- ## Contributing
68
+ if type == "int"
69
+ begin
70
+ var = Integer(var)
71
+ rescue ArgumentError
72
+ puts "Err: Please, type a (integer) number."
73
+ next
74
+ end
75
+ elsif type == "float"
76
+ begin
77
+ var = Float(var)
78
+ rescue ArgumentError
79
+ puts "Err: Please, type a (float) number."
80
+ next
81
+ end
82
+ elsif type == "bool"
83
+ if var.downcase == "true"
84
+ var = true
85
+ elsif var.downcase == "false"
86
+ var = false
87
+ else
88
+ puts "Err: Please, type 'true' or 'false'."
89
+ next
90
+ end
91
+ else
92
+ var = var
93
+ end
30
94
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/better_input.
95
+ if show_response
96
+ puts "User Response: #{var}"
97
+ end
98
+ return var
99
+ end
100
+ end
101
+ ```
data/Rakefile CHANGED
File without changes
Binary file
Binary file
@@ -1,3 +1,3 @@
1
- module BetterInput
2
- VERSION = "0.1.0"
1
+ module Bi
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/better_input.rb CHANGED
@@ -1,4 +1,4 @@
1
- module BetterInput
1
+ module Bi
2
2
  def self.input(question = nil, show_response: false, type: "string")
3
3
  unless question.nil?
4
4
  puts question
@@ -31,6 +31,10 @@ module BetterInput
31
31
  puts "Err: Please, type 'true' or 'false'."
32
32
  next
33
33
  end
34
+ when "string"
35
+ var = String(var)
36
+ else
37
+ puts "Err: Undefined type."
34
38
  end
35
39
 
36
40
  if show_response
@@ -40,3 +44,4 @@ module BetterInput
40
44
  end
41
45
  end
42
46
  end
47
+
data/sig/better_input.rbs CHANGED
File without changes
metadata CHANGED
@@ -1,17 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_input
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
- - Barros FLavio
7
+ - Barros Flavio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-06 00:00:00.000000000 Z
11
+ date: 2024-11-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: When working with CLI I found myself doing things that could be summarized
14
- as this function, so I did it!
13
+ description: |
14
+ This is an improved input function for Ruby.
15
+
16
+ Using this gem, you can easily get user input with real-time type validation, ensuring that the data provided is as expected, among other things.
17
+
18
+ If you have any questions, check the documentation: https://github.com/barrosflavio/ruby_better_input
19
+
20
+ Features:
21
+ - Ask, receive and check input in a single line
22
+ - Support for types such as integer, float and boolean.
23
+ - Input validation with clear error messages.
24
+ - Intuitive and easy-to-use function.
15
25
  email:
16
26
  - flaviomarbs@gmail.com
17
27
  executables: []
@@ -21,6 +31,8 @@ files:
21
31
  - CHANGELOG.md
22
32
  - README.md
23
33
  - Rakefile
34
+ - better_input-0.1.0.gem
35
+ - better_input-0.1.1.gem
24
36
  - lib/better_input.rb
25
37
  - lib/better_input/version.rb
26
38
  - sig/better_input.rbs