mousevc 0.0.2.pre.alpha
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +136 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/examples/example.rb +65 -0
- data/lib/mousevc/app.rb +135 -0
- data/lib/mousevc/controller.rb +56 -0
- data/lib/mousevc/error.rb +17 -0
- data/lib/mousevc/input.rb +188 -0
- data/lib/mousevc/model.rb +35 -0
- data/lib/mousevc/persistence.rb +52 -0
- data/lib/mousevc/router.rb +77 -0
- data/lib/mousevc/validation.rb +353 -0
- data/lib/mousevc/version.rb +7 -0
- data/lib/mousevc/view.rb +66 -0
- data/lib/mousevc.rb +49 -0
- data/mousevc.gemspec +33 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 965d70e8bc3b4614747881cd88769b7f9fb85110
|
4
|
+
data.tar.gz: fb52f433c36a3fd4eddcfb7348da73786b0bf3f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbe673e788f873d53c27c9e0794f50ff19dcf72294764cab4ccdb01e6a66797b3cee6d957b1beb877397742464d97b3309c1e16f253f740f7f6bbe4479f737a1
|
7
|
+
data.tar.gz: 3b86905ff2c32ff61949c1b46c0f4d74b772cfb02e2d0612d12af74aa0c864e95bfac23c690acde03987267c60b164041b31a428f568ae43f479344783e7b909
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Chris Scavello
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
# Mousevc
|
2
|
+
|
3
|
+
(`) (`)
|
4
|
+
=('o')=
|
5
|
+
m m
|
6
|
+
|
7
|
+
MousevC
|
8
|
+
V L
|
9
|
+
C I
|
10
|
+
|
11
|
+
A tiny mouse sized MVC framework to jump start command line apps. Written in Ruby.
|
12
|
+
|
13
|
+
[](http://badge.fury.io/rb/mousevc)
|
14
|
+
|
15
|
+
[View the documentation](http://www.rubydoc.info/gems/mousevc)
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'mousevc'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
```sh
|
28
|
+
$ bundle
|
29
|
+
```
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
```sh
|
34
|
+
$ gem install mousevc
|
35
|
+
```
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
1. Require Mousevc
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'mousevc'
|
43
|
+
```
|
44
|
+
|
45
|
+
1. Create a view in your views directory
|
46
|
+
|
47
|
+
```erb
|
48
|
+
<%# views/show_cheese.txt.erb %>
|
49
|
+
|
50
|
+
Hello Mousevc!
|
51
|
+
I like <%= @cheese %>!
|
52
|
+
What kind of cheese do you like?
|
53
|
+
```
|
54
|
+
|
55
|
+
1. Or just use a string!
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
view = %Q{<%# views/show_cheese.txt.erb %>\nHello Mousevc!\nI like <%= @cheese %>!\nWhat kind of cheese do you like?}
|
59
|
+
```
|
60
|
+
|
61
|
+
1. Create your default controller
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# jerry_controller.rb
|
65
|
+
|
66
|
+
class JerryController < Mousevc::Controller
|
67
|
+
def find_cheese
|
68
|
+
view = %Q{<%# views/show_cheese.txt.erb %>\nHello Mousevc!\nI like <%= @cheese %>!\nWhat kind of cheese do you like?}
|
69
|
+
cheese = @model.cheese
|
70
|
+
@view.render(view, :cheese => cheese)
|
71
|
+
Mousevc::Input.prompt
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
1. Create the corresponding model
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# jerry_model.rb
|
80
|
+
|
81
|
+
class JerryModel < Mousevc::Model
|
82
|
+
def cheese
|
83
|
+
@cheese = "Swiss cheese"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
1. Create a class that extends `Mousevc::App`
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
|
92
|
+
# jerry.rb
|
93
|
+
|
94
|
+
class Jerry < Mousevc::App
|
95
|
+
end
|
96
|
+
|
97
|
+
Jerry.new(
|
98
|
+
:controller => 'JerryController',
|
99
|
+
:model => 'JerryModel',
|
100
|
+
:action => :find_cheese,
|
101
|
+
:views => 'relative/views/directory/path'
|
102
|
+
).run
|
103
|
+
```
|
104
|
+
|
105
|
+
1. Run mouse run!
|
106
|
+
|
107
|
+
```shell
|
108
|
+
$ ruby jerry.rb
|
109
|
+
```
|
110
|
+
|
111
|
+
1. Enjoy the view!
|
112
|
+
|
113
|
+
```shell
|
114
|
+
|
115
|
+
Hello Mousevc!
|
116
|
+
I like Swiss cheese!
|
117
|
+
What kind of cheese do you like?
|
118
|
+
|
119
|
+
>
|
120
|
+
```
|
121
|
+
|
122
|
+
## Development
|
123
|
+
|
124
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
125
|
+
|
126
|
+
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).
|
127
|
+
|
128
|
+
## Contributing
|
129
|
+
|
130
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/BideoWego/mousevc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
131
|
+
|
132
|
+
|
133
|
+
## License
|
134
|
+
|
135
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
136
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mousevc"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/examples/example.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'mousevc'
|
2
|
+
|
3
|
+
# 1. Create your default controller
|
4
|
+
# --------------------------------
|
5
|
+
|
6
|
+
# jerry_controller.rb
|
7
|
+
|
8
|
+
class JerryController < Mousevc::Controller
|
9
|
+
def find_cheese
|
10
|
+
view = %Q{<%# views/show_cheese.txt.erb %>\nHello Mousevc!\nI like <%= @cheese %>!\nWhat kind of cheese do you like?}
|
11
|
+
cheese = @model.cheese
|
12
|
+
@view.render(view, :cheese => cheese)
|
13
|
+
Mousevc::Input.prompt
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# 1. Create the corresponding model
|
18
|
+
# --------------------------------
|
19
|
+
|
20
|
+
# jerry_model.rb
|
21
|
+
|
22
|
+
class JerryModel < Mousevc::Model
|
23
|
+
def cheese
|
24
|
+
@cheese = "Swiss cheese"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# 1. Create a class that extends `Mousevc::App`
|
29
|
+
# --------------------------------
|
30
|
+
|
31
|
+
# jerry.rb
|
32
|
+
|
33
|
+
class Jerry < Mousevc::App
|
34
|
+
end
|
35
|
+
|
36
|
+
Jerry.new(
|
37
|
+
:controller => 'JerryController',
|
38
|
+
:model => 'JerryModel',
|
39
|
+
:action => :find_cheese,
|
40
|
+
:views => 'relative/views/directory/path'
|
41
|
+
).run
|
42
|
+
|
43
|
+
# 1. Create a view in your views directory
|
44
|
+
# --------------------------------
|
45
|
+
|
46
|
+
# <%# views/show_cheese.txt.erb %>
|
47
|
+
|
48
|
+
# Hello Mousevc!
|
49
|
+
# I like <%= @cheese %>!
|
50
|
+
# What kind of cheese do you like?
|
51
|
+
|
52
|
+
# # 1. Run mouse run!
|
53
|
+
# --------------------------------
|
54
|
+
|
55
|
+
# $ ruby jerry.rb
|
56
|
+
|
57
|
+
# 1. Enjoy the view!
|
58
|
+
# --------------------------------
|
59
|
+
# =>
|
60
|
+
|
61
|
+
# Hello Mousevc!
|
62
|
+
# I like Swiss cheese!
|
63
|
+
# What kind of cheese do you like?
|
64
|
+
|
65
|
+
# >
|
data/lib/mousevc/app.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require_relative 'router.rb'
|
2
|
+
require_relative 'input.rb'
|
3
|
+
require_relative 'error.rb'
|
4
|
+
require_relative 'persistence.rb'
|
5
|
+
|
6
|
+
module Mousevc
|
7
|
+
|
8
|
+
## true
|
9
|
+
# The top level class of Mousevc.
|
10
|
+
# The container for all of the objects
|
11
|
+
# created within a Mousevc application.
|
12
|
+
|
13
|
+
class App
|
14
|
+
|
15
|
+
##
|
16
|
+
# @!attribute system_clear
|
17
|
+
#
|
18
|
+
# Setting this to +false+ will disable calls to +system('clear')+
|
19
|
+
# at the start of each application loop.
|
20
|
+
#
|
21
|
+
# @return [Boolean] whether or not to perform system clear
|
22
|
+
|
23
|
+
attr_accessor :system_clear
|
24
|
+
|
25
|
+
##
|
26
|
+
# @!attribute looping
|
27
|
+
#
|
28
|
+
# Set this to +true+ if you want the application to loop, defaults to +false+
|
29
|
+
#
|
30
|
+
# @return [Boolean] whether or not the application should loop
|
31
|
+
|
32
|
+
attr_accessor :looping
|
33
|
+
|
34
|
+
##
|
35
|
+
# @!attribute router
|
36
|
+
#
|
37
|
+
# Returns the current router
|
38
|
+
#
|
39
|
+
# @return [Mousevc::Router] the router
|
40
|
+
|
41
|
+
attr_reader :router
|
42
|
+
|
43
|
+
##
|
44
|
+
# Creates a new +Mousevc::App+ instance
|
45
|
+
#
|
46
|
+
# @param options [Hash] expects the following keys:
|
47
|
+
# - :controller => [String] name of default controller class
|
48
|
+
# - :model => [String] name of default model class
|
49
|
+
# - :action => [Symbol] method to call on default controller
|
50
|
+
# - :views => [String] relative path to views directory
|
51
|
+
# - :looping => [Boolean] +true+ if the application should loop, defaults to +false+
|
52
|
+
# - :system_clear => [Boolean] +true+ if output should be cleared on update, defaults to +false+
|
53
|
+
|
54
|
+
def initialize(options={})
|
55
|
+
@controller = options[:controller]
|
56
|
+
@model = options[:model]
|
57
|
+
@action = options[:action]
|
58
|
+
@views = options[:views]
|
59
|
+
@looping = options[:looping].nil? ? false : options[:looping]
|
60
|
+
@system_clear = options[:system_clear].nil? ? false : options[:system_clear]
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Runs the application
|
65
|
+
|
66
|
+
def run
|
67
|
+
reset
|
68
|
+
@looping ? listen : single
|
69
|
+
Input.clear
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Returns +true+ if +@system_clear+ is true
|
75
|
+
#
|
76
|
+
# @return [Boolean] whether or not system clearing is enabled
|
77
|
+
|
78
|
+
def system_clear?
|
79
|
+
@system_clear
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Returns +true+ if +@looping+ is set to +true+
|
84
|
+
|
85
|
+
def looping?
|
86
|
+
@looping
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
##
|
92
|
+
# Instantiates the router instance.
|
93
|
+
# Passes the router instance the initialization options.
|
94
|
+
|
95
|
+
def reset
|
96
|
+
@router = Router.new(
|
97
|
+
:controller => @controller,
|
98
|
+
:action => @action,
|
99
|
+
:model => @model,
|
100
|
+
:views => @views
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
# Runs the application without looping automatically
|
106
|
+
|
107
|
+
def single
|
108
|
+
clear_view
|
109
|
+
@router.route
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# Runs the application loop.
|
114
|
+
# Clears the system view each iteration.
|
115
|
+
# Calls route on the router instance.
|
116
|
+
#
|
117
|
+
# If the user is trying to reset or quit the application responds accordingly.
|
118
|
+
# Clears Input class variables before exit.
|
119
|
+
|
120
|
+
def listen
|
121
|
+
begin
|
122
|
+
clear_view
|
123
|
+
@router.route unless Input.quit?
|
124
|
+
reset if Input.reset?
|
125
|
+
end until Input.quit?
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# Executes a system clear if system clearing is enabled
|
130
|
+
|
131
|
+
def clear_view
|
132
|
+
Kernel.system('clear') if system_clear?
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Mousevc
|
2
|
+
|
3
|
+
##
|
4
|
+
# Base controller class in Mousevc.
|
5
|
+
# Handles communication with the model.
|
6
|
+
# Decides which view to output.
|
7
|
+
|
8
|
+
class Controller
|
9
|
+
|
10
|
+
##
|
11
|
+
# @!attribute model
|
12
|
+
#
|
13
|
+
# An instance of the +Mousevc::Model+ class.
|
14
|
+
# This will be an instance of the model class passed to the application during initialization.
|
15
|
+
# It is also possible to change this via +@router.model+
|
16
|
+
#
|
17
|
+
# @return [Mousevc::Model] the controller's model
|
18
|
+
|
19
|
+
attr_accessor :model
|
20
|
+
|
21
|
+
##
|
22
|
+
# @!attribute view [r]
|
23
|
+
#
|
24
|
+
# An instance of the +Mousevc::View+ class
|
25
|
+
#
|
26
|
+
# @return [Mousevc::View] the view instance
|
27
|
+
|
28
|
+
attr_reader :view
|
29
|
+
|
30
|
+
##
|
31
|
+
# Creates a new +Mousevc::Controller+ instance
|
32
|
+
#
|
33
|
+
# @note Controllers should not be instantiated directly. Pass the name of the controller you wish you instantiate to the router via +@router.controller+ along with a model and action. This allows the router to do the work for you on the next application execution or loop.
|
34
|
+
#
|
35
|
+
# @param options [Hash] expects the following keys:
|
36
|
+
# - :model => [Mousevc::Model] a reference to the controller's model instance
|
37
|
+
# - :view => [Mousevc::View] a reference to the view instance
|
38
|
+
# - :router => [Mousevc::Router] a reference to the router instance
|
39
|
+
|
40
|
+
def initialize(options={})
|
41
|
+
@model = options[:model]
|
42
|
+
@view = options[:view]
|
43
|
+
@router = options[:router]
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
##
|
49
|
+
# Outputs the default Mousevc welcome
|
50
|
+
|
51
|
+
def hello_mousevc
|
52
|
+
puts Mousevc.art
|
53
|
+
Input.prompt
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mousevc
|
2
|
+
|
3
|
+
##
|
4
|
+
# A namespaced Mousevc specific extension of Ruby's +StandardError+.
|
5
|
+
|
6
|
+
class Error < StandardError
|
7
|
+
|
8
|
+
##
|
9
|
+
# Create a new +Mousevc::Error+ instance
|
10
|
+
#
|
11
|
+
# @param message [String] the error message
|
12
|
+
|
13
|
+
def initialize(message)
|
14
|
+
super(message)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
module Mousevc
|
2
|
+
|
3
|
+
##
|
4
|
+
# A globally accessible class to allow access to user input and provide input specific notices.
|
5
|
+
|
6
|
+
class Input
|
7
|
+
|
8
|
+
##
|
9
|
+
# @!attribute notice
|
10
|
+
# @return [String] the notice message
|
11
|
+
|
12
|
+
@@notice = nil
|
13
|
+
|
14
|
+
##
|
15
|
+
# @!attribute data [r]
|
16
|
+
# @return [String] the user input data retrieved via +Input.prompt+
|
17
|
+
|
18
|
+
@@data = nil
|
19
|
+
|
20
|
+
##
|
21
|
+
# @!attribute prompts [r]
|
22
|
+
# @return [Hash] a hash of prompts available for use in +Input.prompt+
|
23
|
+
|
24
|
+
@@prompts = {:default => '>'}
|
25
|
+
|
26
|
+
##
|
27
|
+
# @!attribute appearance
|
28
|
+
# @return [Symbol] the symbol name of the prompt appearance to use from +Input.prompts+
|
29
|
+
|
30
|
+
@@appearance = nil
|
31
|
+
|
32
|
+
##
|
33
|
+
# @return [Symbol] the symbol name of the current prompt appearance
|
34
|
+
|
35
|
+
def self.appearance
|
36
|
+
@@appearance
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Set the default appearance for the prompt
|
41
|
+
#
|
42
|
+
# @param value [Symbol] the symbol name of the appearance to use from +Input.prompts+
|
43
|
+
|
44
|
+
def self.appearance=(value)
|
45
|
+
@@appearance = value
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Clears all or a list of class variables by setting them to +nil+
|
50
|
+
#
|
51
|
+
# @param args [Symbol] a symbol list of class variables to clear
|
52
|
+
|
53
|
+
def self.clear(*args)
|
54
|
+
if args.empty?
|
55
|
+
args = [
|
56
|
+
:notice,
|
57
|
+
:data,
|
58
|
+
:prompts,
|
59
|
+
:appearance
|
60
|
+
]
|
61
|
+
end
|
62
|
+
to_defaults(args)
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# @note Calling this method will stop all execution until the user submits input
|
67
|
+
#
|
68
|
+
# Prompts the user for input using +gets.strip+
|
69
|
+
# Once input is submitted it will be available via +Input.data+
|
70
|
+
#
|
71
|
+
# @param appearance [Symbol] the symbol name of the prompt the use from +Input.prompts+
|
72
|
+
# - If an appearance is not specified it uses +:default+
|
73
|
+
|
74
|
+
def self.prompt(appearance=nil)
|
75
|
+
appearance = pick(appearance)
|
76
|
+
print "\n#{appearance} "
|
77
|
+
@@data = $stdin.gets.to_s.strip
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# @return [Hash] a hash of the currently available prompt appearances
|
82
|
+
|
83
|
+
def self.prompts
|
84
|
+
@@prompts
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# Get the notice message
|
89
|
+
#
|
90
|
+
# @return [String] the notice message
|
91
|
+
|
92
|
+
def self.notice
|
93
|
+
@@notice
|
94
|
+
end
|
95
|
+
|
96
|
+
##
|
97
|
+
# Set the notice message. This is a good place to put error messages corresponding to user input
|
98
|
+
#
|
99
|
+
# @param value [String] the notice message
|
100
|
+
|
101
|
+
def self.notice=(value)
|
102
|
+
@@notice = value
|
103
|
+
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# Get the current value of user input retrieved via +Input.prompt+
|
107
|
+
#
|
108
|
+
# @return [String] the data
|
109
|
+
|
110
|
+
def self.data
|
111
|
+
@@data
|
112
|
+
end
|
113
|
+
|
114
|
+
##
|
115
|
+
# Check if a notice message exists
|
116
|
+
#
|
117
|
+
# @return [Boolean] false if notice message is empty
|
118
|
+
|
119
|
+
def self.notice?
|
120
|
+
! @@notice.to_s.empty?
|
121
|
+
end
|
122
|
+
|
123
|
+
##
|
124
|
+
# Check if the user is trying to reset the application
|
125
|
+
#
|
126
|
+
# @return [Boolean] true if +Input.data+ is +r+ or +reset+
|
127
|
+
|
128
|
+
def self.reset?
|
129
|
+
['r', 'reset'].include?(@@data)
|
130
|
+
end
|
131
|
+
|
132
|
+
##
|
133
|
+
# Check if the user is trying to quit the application
|
134
|
+
#
|
135
|
+
# @return [Boolean] true if +Input.data+ is +q+, +quit+, or +exit+
|
136
|
+
|
137
|
+
def self.quit?
|
138
|
+
['q', 'quit', 'exit'].include?(@@data)
|
139
|
+
end
|
140
|
+
|
141
|
+
##
|
142
|
+
# Check if the user is tring to clear the input data
|
143
|
+
#
|
144
|
+
# @return [Boolean] true if +Input.data+ is +c+ or +clear+
|
145
|
+
|
146
|
+
def self.clear?
|
147
|
+
['c', 'clear'].include?(@@data)
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
##
|
153
|
+
# Sets class variables to their default values
|
154
|
+
#
|
155
|
+
# @param attributes [Array<Symbol>] array of symbols names for attributes to reset
|
156
|
+
|
157
|
+
def self.to_defaults(attributes)
|
158
|
+
attributes.each do |attribute|
|
159
|
+
case attribute
|
160
|
+
when :prompts
|
161
|
+
@@prompts.clear
|
162
|
+
@@prompts = {:default => '>'}
|
163
|
+
else
|
164
|
+
class_variable_set("@@#{attribute}", nil)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
##
|
170
|
+
# Checks if a specific appearance was passed or set as a default.
|
171
|
+
# Returns that appearance or the default if none was set.
|
172
|
+
#
|
173
|
+
# @return [String] the appearance of the prompt
|
174
|
+
|
175
|
+
def self.pick(appearance=nil)
|
176
|
+
if appearance
|
177
|
+
appearance = @@prompts[appearance]
|
178
|
+
elsif @@appearance
|
179
|
+
appearance = @@prompts[@@appearance]
|
180
|
+
else
|
181
|
+
appearance = @@prompts[:default]
|
182
|
+
end
|
183
|
+
appearance
|
184
|
+
end
|
185
|
+
|
186
|
+
clear
|
187
|
+
end
|
188
|
+
end
|