menuboy 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6daaa783e679485f80b584a273146322315d12cf
4
+ data.tar.gz: d1d1cc315345f157d3639011da1cb9cdfc9099bc
5
+ SHA512:
6
+ metadata.gz: 308cbb2003854e7403cc49da964fc14b841472b8a38deff70a856aa7e8da54775b7766ec3bd6543167c4464f37ac22f5aacce3d4553db6df25ddbcdec5a974e8
7
+ data.tar.gz: 1d4cb5b6672ba7a116203a0c58b34a55b205db540b39381e2b26bb013782710442141a88ab8dc56645e7e48ef3fab0952af343f499134a53c2721edf431ca4b4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in menuboy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Keyvan Fatehi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Menuboy
2
+
3
+ Ultrasimple command-line menu/submenu DSL
4
+
5
+ WARNING: I didn't use TDD to develop this -- it is an extraction out of
6
+ a custom script I developed for the non-backend-coders to use for
7
+ managing our node.js/mongodb/redis stack in a super simple way. You can
8
+ see the basic idea of that in test/example.rb, which was used to develop
9
+ menuboy.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'menuboy'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install menuboy
24
+
25
+ ## Usage
26
+
27
+ Say you write a menuboy script like this:
28
+
29
+ ```ruby
30
+ require 'menuboy'
31
+
32
+ include Menuboy::DSL
33
+
34
+ mainmenu "Main Menu" do
35
+ option "say cheese!" do
36
+ puts "CHEESE!"
37
+ end
38
+
39
+ submenu "animal sounds" do
40
+ submenu "duck sounds" do
41
+ option "loud quack" do
42
+ puts "QUACK!!!"
43
+ end
44
+
45
+ option "soft quack" do
46
+ puts "quack!"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ ```
52
+
53
+ You can then run the script and have such an interaction:
54
+
55
+ ```
56
+ $prompt> ruby simple.rb
57
+ 1 - say cheese!
58
+ 2 - animal sounds
59
+ q - quit / go back
60
+ (Main Menu)> 1
61
+ CHEESE!
62
+ (Main Menu)> 2
63
+ 1 - duck sounds
64
+ q - quit / go back
65
+ (animal sounds)> h
66
+ 1 - duck sounds
67
+ q - quit / go back
68
+ (animal sounds)> 1
69
+ 1 - loud quack
70
+ 2 - soft quack
71
+ q - quit / go back
72
+ (duck sounds)> 1
73
+ QUACK!!!
74
+ (duck sounds)> 2
75
+ quack!
76
+ (duck sounds)> q
77
+ (animal sounds)> h
78
+ 1 - duck sounds
79
+ q - quit / go back
80
+ (animal sounds)> q
81
+ (Main Menu)> q
82
+ $prompt>
83
+ ```
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Menuboy
2
+ VERSION = "0.0.1"
3
+ end
data/lib/menuboy.rb ADDED
@@ -0,0 +1,82 @@
1
+ require "menuboy/version"
2
+
3
+ module Menuboy
4
+ module DSL
5
+ def option name, &block
6
+ opt_count = @target.options.count
7
+ key = (opt_count+1).to_s
8
+ if key.size > 1
9
+ raise StandardError, "You can only define 9 options per menu"
10
+ end
11
+ opt = Option.new(key, name, block)
12
+ @target.options.push opt
13
+ end
14
+
15
+ def submenu name="Submenu"
16
+ option(name) do
17
+ @target = Menu.new(name)
18
+ yield
19
+ @target.start
20
+ end
21
+ end
22
+
23
+ def mainmenu(name="Main menu")
24
+ if @mainmenu
25
+ raise StandardError, "You can only define one main menu"
26
+ else
27
+ @mainmenu = Menu.new(name)
28
+ @target = @mainmenu
29
+ yield
30
+ @mainmenu.start
31
+ end
32
+ end
33
+ end
34
+
35
+ class Option < Struct.new(:key, :name, :proc)
36
+ end
37
+
38
+ class Menu
39
+ include DSL
40
+ attr_accessor :name, :options
41
+
42
+ CTRL_C = "\u0003"
43
+
44
+ def initialize name
45
+ self.name = name
46
+ self.options = []
47
+ end
48
+
49
+ def print_help
50
+ self.options.each do |opt|
51
+ puts "#{opt.key} - #{opt.name}"
52
+ end
53
+ puts "p - jump into a Pry repl" if defined? Pry
54
+ puts "q - quit / go back"
55
+ end
56
+
57
+ def option_match? input
58
+ matches = self.options.select{|o| o.key == input}
59
+ matches.length == 1 ? matches.last : false
60
+ end
61
+
62
+ def start
63
+ print_help
64
+ loop do
65
+ print "(#{@name})> "
66
+ input = STDIN.getch
67
+ puts input
68
+ if input == CTRL_C || input == "q"
69
+ break
70
+ elsif input == "h"
71
+ print_help
72
+ elsif input == "p" && defined? Pry
73
+ binding.pry
74
+ elsif option = option_match?(input)
75
+ option.proc.call
76
+ else
77
+ puts "Unassigned input"
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
data/menuboy.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'menuboy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "menuboy"
8
+ spec.version = Menuboy::VERSION
9
+ spec.authors = ["Keyvan Fatehi"]
10
+ spec.email = ["keyvanfatehi@gmail.com"]
11
+ spec.description = %q{Simple CLI-based menu building DSL}
12
+ spec.summary = %q{Simple CLI-based menu building DSL}
13
+ spec.homepage = "https://github.com/keyvanfatehi/menuboy"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
data/test/example.rb ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pry'
3
+ require 'menuboy'
4
+
5
+ @current_db = "identity"
6
+
7
+ include Menuboy::DSL
8
+
9
+ mainmenu "Main Menu" do
10
+ option "start the stack" do
11
+ if @started
12
+ puts "Already started"
13
+ else
14
+ @started = true
15
+ puts "Started!"
16
+ end
17
+ end
18
+
19
+ submenu "database options" do
20
+ puts "using #{@current_db}"
21
+
22
+ submenu "change database" do
23
+ option "identity" do
24
+ @current_db = "identity"
25
+ puts "using identity"
26
+ end
27
+
28
+ option "storage" do
29
+ @current_db = "storage"
30
+ puts "using storage"
31
+ end
32
+ end
33
+
34
+ option "drop database" do
35
+ puts "#{@current_db} dropped!"
36
+ end
37
+
38
+ option "seed database" do
39
+ puts "#{@current_db} seeded!"
40
+ end
41
+ end
42
+ end
data/test/simpler.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'menuboy'
2
+
3
+ include Menuboy::DSL
4
+
5
+ mainmenu "Main Menu" do
6
+ option "say cheese!" do
7
+ puts "CHEESE!"
8
+ end
9
+
10
+ submenu "animal sounds" do
11
+ submenu "duck sounds" do
12
+ option "loud quack" do
13
+ puts "QUACK!!!"
14
+ end
15
+
16
+ option "soft quack" do
17
+ puts "quack!"
18
+ end
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: menuboy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Keyvan Fatehi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Simple CLI-based menu building DSL
42
+ email:
43
+ - keyvanfatehi@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/menuboy.rb
54
+ - lib/menuboy/version.rb
55
+ - menuboy.gemspec
56
+ - test/example.rb
57
+ - test/simpler.rb
58
+ homepage: https://github.com/keyvanfatehi/menuboy
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.6
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Simple CLI-based menu building DSL
82
+ test_files:
83
+ - test/example.rb
84
+ - test/simpler.rb