easy_repl 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 154ddd2d3a29b8ecc78ee38f814c6bbe729a5a6b
4
+ data.tar.gz: 4b8aef184bd7704e2919a3d386d21e1cd588e4df
5
+ SHA512:
6
+ metadata.gz: 2402148318a7fbb6ffb553e816574043980f0bb8b0113cbb71260f678c371febf46cbe2635ac88fdccbbb9b717f73c8a3774609f66523aa07d37268a83eb2997
7
+ data.tar.gz: 222955fe253831f03f4aeb6ed71062faa9a90facfd6e3ce06d15d94aa71239b952cb02dfe187a60de8b1899a769f39f88fc151948b769ff10159367c7cc5eac9
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+
19
+ .easyrepl_history
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ easy_repl
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_repl.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Christopher Erin
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,109 @@
1
+ # EasyRepl
2
+
3
+ A Repl (Read Eval Print Loop) library.
4
+
5
+ ## Features
6
+
7
+ * Ease of use. Just pass a block to the start method to process the user's input.
8
+ * Command History
9
+ * Exit Command
10
+ * Reload Command
11
+ * Setup and Teardown lifecycle methods
12
+ * After Input and Before Input lifecylce methods
13
+
14
+ ## Usage
15
+
16
+ ```ruby
17
+ require 'easy_repl'
18
+
19
+ #start an echoing repl
20
+ EasyRepl.start
21
+
22
+ # > a
23
+ # a
24
+ # > b
25
+ # b
26
+ # > exit
27
+
28
+ #start a repl that reverses input
29
+ EasyRepl.start do |input|
30
+ puts input.reverse
31
+ end
32
+
33
+ # > abc
34
+ # cba
35
+ # > def
36
+ # fed
37
+ # > exit
38
+
39
+ #create a repl class with a life cycle
40
+ class MyRepl
41
+ include EasyRepl::Repl
42
+
43
+ def setup
44
+ puts "LOADING RESOURCES"
45
+ end
46
+
47
+ def before_input
48
+ puts "before_input"
49
+ end
50
+
51
+ def process_input(input)
52
+ puts input.reverse
53
+ end
54
+
55
+ def after_input
56
+ puts "after_input"
57
+ end
58
+
59
+ def teardown
60
+ puts "CLOSING RESOURCES"
61
+ end
62
+
63
+ end
64
+
65
+ MyRepl.new.start
66
+
67
+ # LOADING RESOURCES
68
+ # before_input
69
+ # > abc
70
+ # after_input
71
+ # cba
72
+ # before_input
73
+ # > reload
74
+ # after_input
75
+ # CLOSING RESOURCES
76
+ # LOADING RESOURCES
77
+ # before_input
78
+ # > exit
79
+ # after_input
80
+ # CLOSING RESOURCES
81
+
82
+
83
+ # rename the history file before starting the repl
84
+ EasyRepl.history_file = ".myrepl_history"
85
+ EasyRepl.start
86
+ ```
87
+
88
+ ## Installation
89
+
90
+ Add this line to your application's Gemfile:
91
+
92
+ gem 'easy_repl'
93
+
94
+ And then execute:
95
+
96
+ $ bundle
97
+
98
+ Or install it yourself as:
99
+
100
+ $ gem install easy_repl
101
+
102
+
103
+ ## Contributing
104
+
105
+ 1. Fork it
106
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
107
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
108
+ 4. Push to the branch (`git push origin my-new-feature`)
109
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/easy_repl.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_repl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_repl"
8
+ spec.version = EasyRepl::VERSION
9
+ spec.authors = ["Christopher Erin"]
10
+ spec.email = ["chris.erin@gmail.com"]
11
+ spec.description = %q{easy repl}
12
+ spec.summary = %q{easy repl}
13
+ spec.homepage = "https://github.com/chriserin/easy_repl"
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
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,15 @@
1
+ module EasyRepl
2
+ module Commands
3
+ class Exit
4
+ extend Matchable
5
+
6
+ def self.cmd_txt
7
+ "exit"
8
+ end
9
+
10
+ def self.run
11
+ throw :exit_repl, :exit
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module EasyRepl
2
+ module Commands
3
+ module Matchable
4
+ def matches(input)
5
+ input =~ /^#{cmd_txt}/
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module EasyRepl
2
+ module Commands
3
+ class Reload
4
+ extend Matchable
5
+
6
+ def self.cmd_txt
7
+ "reload"
8
+ end
9
+
10
+ def self.run
11
+ throw :exit_repl, :reload
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,70 @@
1
+ require 'irb/locale'
2
+ require 'irb/input-method'
3
+ require 'irb/ext/save-history'
4
+
5
+ module IRB
6
+ def self.conf
7
+ {
8
+ LC_MESSAGES: Locale.new,
9
+ SAVE_HISTORY: 100,
10
+ HISTORY_FILE: EasyRepl.history_file || '.easyrepl_history',
11
+ AT_EXIT: []
12
+ }
13
+ end
14
+ end
15
+
16
+ module EasyRepl
17
+ module Repl
18
+ def start
19
+ IRB::HistorySavingAbility.extend(IRB::HistorySavingAbility) unless IRB::HistorySavingAbility === IRB::HistorySavingAbility
20
+ loop do
21
+ setup if respond_to? :setup
22
+ begin
23
+ exit_value = catch(:exit_repl) do
24
+ loop do
25
+ begin
26
+ before_input if respond_to? :before_input
27
+ if block_given?
28
+ yield EasyRepl.gets
29
+ elsif respond_to? :process_input
30
+ process_input(EasyRepl.gets)
31
+ else
32
+ puts EasyRepl.gets
33
+ end
34
+ ensure
35
+ after_input if respond_to? :after_input
36
+ end
37
+ end
38
+ end
39
+ return if exit_value == :exit
40
+ ensure
41
+ teardown if respond_to? :teardown
42
+ end
43
+ end
44
+ ensure
45
+ IRB::HistorySavingAbility.save_history
46
+ end
47
+
48
+ def gets
49
+ input = prompt.gets
50
+ command = commands.find {|c| c.matches(input)}
51
+ if command
52
+ command.run
53
+ return ""
54
+ else
55
+ return input
56
+ end
57
+ end
58
+
59
+ private
60
+ def commands
61
+ @commands ||= [EasyRepl::Commands::Exit, EasyRepl::Commands::Reload]
62
+ end
63
+
64
+ def prompt
65
+ @io ||= IRB::ReadlineInputMethod.new.tap do |new_io|
66
+ new_io.prompt = "> "
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module EasyRepl
2
+ VERSION = "0.0.2"
3
+ end
data/lib/easy_repl.rb ADDED
@@ -0,0 +1,19 @@
1
+
2
+
3
+ module EasyRepl
4
+ attr_accessor :history_file
5
+ def self.history_file=(value)
6
+ @history_file = value
7
+ end
8
+
9
+ def self.history_file
10
+ @history_file
11
+ end
12
+
13
+ require 'easy_repl/repl'
14
+ require 'easy_repl/commands/matchable'
15
+ require 'easy_repl/commands/exit'
16
+ require 'easy_repl/commands/reload'
17
+ require "easy_repl/version"
18
+ extend EasyRepl::Repl
19
+ end
@@ -0,0 +1,126 @@
1
+ require './test/support'
2
+ require './lib/easy_repl'
3
+ require 'ostruct'
4
+
5
+ describe EasyRepl do
6
+ it "starts and exits" do
7
+ out = ''
8
+ EasyRepl.stub :prompt, OpenStruct.new(:gets => "exit") do
9
+ out, err = capture_io do
10
+ EasyRepl.start
11
+ end
12
+ end
13
+ assert_includes out, ""
14
+ end
15
+
16
+ it "easy repl accepts a block" do
17
+ out = ''
18
+ seq = Support::CommandSequence.new "XXX", "exit"
19
+ EasyRepl.stub :prompt, seq do
20
+ out, err = capture_io do
21
+ EasyRepl.start do |input|
22
+ puts input
23
+ end
24
+ end
25
+ end
26
+ assert_equal out, "XXX\n"
27
+ end
28
+
29
+ it "echos all inputs before exit" do
30
+ out = ''
31
+ seq = Support::CommandSequence.new "abc", "def", "ghi", "exit"
32
+ EasyRepl.stub :prompt, seq do
33
+ out, err = capture_io do
34
+ EasyRepl.start
35
+ end
36
+ end
37
+ assert_equal "abc\ndef\nghi\n", out
38
+ end
39
+
40
+ it "accepts a block that echos reversed input" do
41
+ out = ''
42
+ seq = Support::CommandSequence.new "abc", "def", "ghi", "exit"
43
+ EasyRepl.stub :prompt, seq do
44
+ out, err = capture_io do
45
+ EasyRepl.start do |input|
46
+ puts input.reverse
47
+ end
48
+ end
49
+ end
50
+ assert_equal "cba\nfed\nihg\n", out
51
+ end
52
+
53
+ describe "setup is defined" do
54
+ it "calls setup before asking for input" do
55
+ out = ''
56
+ seq = Support::CommandSequence.new "exit"
57
+ EasyRepl.stub :prompt, seq do
58
+ out, err = capture_io do
59
+ Support::SetupEasyRepl.start
60
+ end
61
+ end
62
+ assert_equal "SETUP\n", out
63
+ end
64
+
65
+ it "calls setup when the reload command is entered" do
66
+ out = ''
67
+ seq = Support::CommandSequence.new "reload", "exit"
68
+ EasyRepl.stub :prompt, seq do
69
+ out, err = capture_io do
70
+ Support::SetupEasyRepl.start
71
+ end
72
+ end
73
+ assert_equal "SETUP\nSETUP\n", out
74
+ end
75
+ end
76
+
77
+ describe "teardown is defined" do
78
+ it "calls teardown before exiting" do
79
+ out = ''
80
+ seq = Support::CommandSequence.new "exit"
81
+ EasyRepl.stub :prompt, seq do
82
+ out, err = capture_io do
83
+ Support::TeardownEasyRepl.start
84
+ end
85
+ end
86
+ assert_equal "TEARDOWN\n", out
87
+ end
88
+
89
+ it "calls teardown when the reload command is entered" do
90
+ out = ''
91
+ seq = Support::CommandSequence.new "reload", "exit"
92
+ EasyRepl.stub :prompt, seq do
93
+ out, err = capture_io do
94
+ Support::TeardownEasyRepl.start
95
+ end
96
+ end
97
+ assert_equal "TEARDOWN\nTEARDOWN\n", out
98
+ end
99
+ end
100
+
101
+ describe "before input is defined" do
102
+ it "calls before input before every command" do
103
+ out = ''
104
+ seq = Support::CommandSequence.new "XXX", "exit"
105
+ EasyRepl.stub :prompt, seq do
106
+ out, err = capture_io do
107
+ Support::BeforeInputEasyRepl.start
108
+ end
109
+ end
110
+ assert_equal "BEFOREINPUT\nXXX\nBEFOREINPUT\n", out
111
+ end
112
+ end
113
+
114
+ describe "after input is defined" do
115
+ it "calls after input after every command" do
116
+ out = ''
117
+ seq = Support::CommandSequence.new "XXX", "exit"
118
+ EasyRepl.stub :prompt, seq do
119
+ out, err = capture_io do
120
+ Support::AfterInputEasyRepl.start
121
+ end
122
+ end
123
+ assert_equal "XXX\nAFTERINPUT\nAFTERINPUT\n", out
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,11 @@
1
+ require './lib/easy_repl'
2
+
3
+ module Support
4
+ class AfterInputEasyRepl
5
+ extend EasyRepl::Repl
6
+
7
+ def self.after_input
8
+ puts "AFTERINPUT"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require './lib/easy_repl'
2
+
3
+ module Support
4
+ class BeforeInputEasyRepl
5
+ extend EasyRepl::Repl
6
+
7
+ def self.before_input
8
+ puts "BEFOREINPUT"
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,11 @@
1
+ module Support
2
+ class CommandSequence
3
+ def initialize(*args)
4
+ @commands = args
5
+ end
6
+
7
+ def gets
8
+ @commands.shift
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require './lib/easy_repl'
2
+
3
+ module Support
4
+ class SetupEasyRepl
5
+ extend EasyRepl::Repl
6
+
7
+ def self.setup
8
+ puts "SETUP"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require './lib/easy_repl'
2
+
3
+ module Support
4
+ class TeardownEasyRepl
5
+ extend EasyRepl::Repl
6
+
7
+ def self.teardown
8
+ puts "TEARDOWN"
9
+ end
10
+ end
11
+ end
data/test/support.rb ADDED
@@ -0,0 +1,8 @@
1
+ gem 'minitest'
2
+ require 'minitest/autorun'
3
+ require 'minitest/spec'
4
+ require './test/support/command_sequence'
5
+ require './test/support/setup_easy_repl'
6
+ require './test/support/teardown_easy_repl'
7
+ require './test/support/before_input_easy_repl'
8
+ require './test/support/after_input_easy_repl'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_repl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Erin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-05 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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: easy repl
56
+ email:
57
+ - chris.erin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .ruby-gemset
64
+ - .ruby-version
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - easy_repl.gemspec
70
+ - lib/easy_repl.rb
71
+ - lib/easy_repl/commands/exit.rb
72
+ - lib/easy_repl/commands/matchable.rb
73
+ - lib/easy_repl/commands/reload.rb
74
+ - lib/easy_repl/repl.rb
75
+ - lib/easy_repl/version.rb
76
+ - test/easy_repl_test.rb
77
+ - test/support.rb
78
+ - test/support/after_input_easy_repl.rb
79
+ - test/support/before_input_easy_repl.rb
80
+ - test/support/command_sequence.rb
81
+ - test/support/setup_easy_repl.rb
82
+ - test/support/teardown_easy_repl.rb
83
+ homepage: https://github.com/chriserin/easy_repl
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.1.11
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: easy repl
107
+ test_files:
108
+ - test/easy_repl_test.rb
109
+ - test/support.rb
110
+ - test/support/after_input_easy_repl.rb
111
+ - test/support/before_input_easy_repl.rb
112
+ - test/support/command_sequence.rb
113
+ - test/support/setup_easy_repl.rb
114
+ - test/support/teardown_easy_repl.rb