my_scripts 0.0.7 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -7,25 +7,85 @@
7
7
  A collection of simple scripts/commands used to save time and avoid memorizing/
8
8
  retyping chains of boilerplate console commands and their arguments. Packaged
9
9
  as a gem to make it easily available everywhere without the need to set up
10
- paths, environments and all such nonsense...
10
+ paths, environments and all such nonsense... It is also not OS-specific, so
11
+ (in theory) scripts should work on Mac, *nix and Windows.
11
12
 
12
13
  == FEATURES/PROBLEMS:
13
14
 
14
- This project is quite new, so it's probably not ready for prime time just yet...
15
+ OK, I confess: I wanted to write scripts but had neither time nor desire to learn
16
+ all the esoterics of bash, cmd/powershell and mac scripting. So, I set out to do
17
+ all my scripting in Ruby. I also wanted my scripts to be easily testable, that's
18
+ why I created a simple scripting framework instead of stuffing each script in a
19
+ separate executable Ruby file as most people do.
20
+
21
+ These scripts are very much targeted to my own specific needs, but if you find them
22
+ useful or need additional features, I'll be happy to generalize. You can also use this
23
+ gem as a basis for writing your own simple scripts that are easy to test and maintain.
24
+
25
+ Ah, yes - this gem will work only with Ruby 1.9 and later. I use advanced string encoding
26
+ features in my scripts, so 1.8 is not an option for me, and providing backward compatibility
27
+ looks like too much pain. Ruby 1.9 is the way of the future, so let's move forward!
28
+
29
+ == INSTALL:
30
+
31
+ $ sudo gem install my_scripts
15
32
 
16
33
  == SYNOPSIS:
34
+ === Creating your own scripts
35
+
36
+ Put your source file inside lib/my_scripts. It will be auto-loaded. Use MyScripts
37
+ module as a top level namespace. Subclass Script and redefine run method to do all
38
+ actual work:
39
+
40
+ #-------- lib/my_shiny_script.rb----------
41
+ module MyScript
42
+ class MyShinyScript < Script
43
+ def run
44
+ # here you do all actual work for your script
45
+ # you have following instance vars at your disposal:
46
+ # @name - your script name,
47
+ # @argv - your ARGV Array of command line arguments,
48
+ # @cli - CLI runner (holds references to stdin and stdout)
49
+ ...
50
+ end
51
+ end
52
+ end
53
+ #-------
54
+
55
+ Put your executable into bin directory, with something like this:
56
+
57
+ #-------- bin/my_shiny_script----------
58
+ #!/usr/bin/env ruby
59
+ require File.dirname(__FILE__) + '/../lib/my_scripts'
60
+ MyScripts::CLI.run :my_shiny_script, ARGV
61
+ #-------
62
+
63
+ Run 'rake install' from project directory, and enjoy greatness of your new script!
64
+
65
+ OK, so you're not advanced enough to follow even these simple instructions. No problemo.
66
+ I have dummy script skeleton in lib/dummy.rb that is ready for your use. Just put your
67
+ script code inside run method (if you use ARGV, change it to @argv). Done! You can immediately
68
+ run your script anywhere in the system using the following command:
69
+
70
+ $ dummy Whatever arguments your code expects and processes
71
+
72
+ === Using existing scripts
73
+
74
+ $ jew project_name Summary or description goes here
75
+
76
+ This script uses Jeweler to create new project skeleton, local git repo and
77
+ initiate remote repo on github. No need to enclose your description in quotes.
78
+
79
+ $ gitto [BUMP] Commit message goes here
17
80
 
18
81
  Save the result of all your project-related work with one command. It adds all
19
82
  new files to git VCS, commits all changes with a timestamped message, opionally
20
83
  bumps up version and pushes to remote VCS repository(-ies). If first arg is a
21
84
  number, it is treated as bump directive: 1 - bump:patch, 10 - bump:minor,
22
- 100 - bump:major. Otherwise all arguments are treated as part of commit message:
23
-
24
- $ gitcp [BUMP] Commit message goes here
85
+ 100 - bump:major. Otherwise all arguments are treated as part of commit message.
86
+ Use inside project dir, preferably at top level.
25
87
 
26
- == INSTALL:
27
-
28
- $ sudo gem install my_scripts
88
+ ...
29
89
 
30
90
  == LICENSE:
31
91
 
data/Rakefile CHANGED
@@ -5,8 +5,8 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "my_scripts"
8
- gem.summary = "A collection of ascripts"
9
- gem.description = "Collection of simple scripts (mostly dev-related)"
8
+ gem.summary = "Simple framework for writing command-line scripts"
9
+ gem.description = "Simple framework for writing command-line scripts and collection of my own scripts (mostly dev-related)"
10
10
  gem.email = "arvitallian@gmail.com"
11
11
  gem.homepage = "http://github.com/arvicco/my_scripts"
12
12
  gem.authors = ["arvicco"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.9
data/bin/{gitcp → citi} RENAMED
@@ -2,4 +2,4 @@
2
2
 
3
3
  require File.dirname(__FILE__) + '/../lib/my_scripts'
4
4
 
5
- MyScripts::CLI.run :gitcp, ARGV
5
+ MyScripts::CLI.run :citi, ARGV
data/bin/dummy ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.dirname(__FILE__) + '/../lib/my_scripts'
4
+
5
+ MyScripts::CLI.run :dummy, ARGV
data/bin/gitto ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.dirname(__FILE__) + '/../lib/my_scripts'
4
+
5
+ MyScripts::CLI.run :gitto, ARGV
data/bin/recode.rb ADDED
@@ -0,0 +1,59 @@
1
+ # encoding: UTF-8
2
+ INFILE_NAME = 'ACCT_101.QIF'
3
+ OUTFILE_NAME = 'TEST.QIF'
4
+ INFILE_ENCODING = 'CP1251:UTF-8' # Encoding pair of log file ('File external:Ruby internal')
5
+ OUTFILE_ENCODING = 'CP1252:UTF-8' # Encoding pair of log file ('File external:Ruby internal')
6
+ #STDOUT_ENCODING = 'CP866:UTF-8' # Encoding pair of stdout ('Stdout external:Ruby internal')
7
+ REPLACES = {
8
+ /Pokupka Masterkard .*(Detskiy Mir).*/ => "Detski Mir\nLChildcare",
9
+ /Pokupka Masterkard .*(Medicina).*/ => "AO Medicina\nLMedical:Medicine",
10
+ /Pokupka Masterkard .*(Pharmacy).*/ => "\\1\nLMedical:Medicine",
11
+ /Pokupka Masterkard .*(Alye Parusa).*/ => "\\1\nLGroceries",
12
+ /Pokupka Masterkard .*(Perekrestok).*/ => "\\1\nLGroceries",
13
+ /Pokupka Masterkard .*(Ile De Beaute).*/ => "\\1\nLPersonal Care",
14
+ /Pokupka Masterkard .*(Beeline).*/ => "\\1\nLCommunications:Telephone",
15
+ /Pokupka Masterkard (.+) (Moscow Ru.+|Moskva Ru.+)/ => "\\1\nM\\2\nLHousehold",
16
+ /Vkhodyashchij Platezh(.+Bank Moskvy)/ => "Incoming transfer\nM\\1\nL[BM New Prestige]",
17
+ /Oplata Dolg Kr Karta(.+)/ => "Transfer to Credit card\nM\\1\nL[Citi MC]",
18
+ /Snyatie Nalichnykh(.+)/ => "Cash withdrawal\nM\\1\nL[Cash RUB]",
19
+ /Vznos Nalichnykh(.+)/ => "Cash deposit\nM\\1\nL[Cash RUB]",
20
+ /Kom Za Obsluzhivanie/ => "Citibank\nMService fee\nLFinance:Bank Charge",
21
+ 'Universam '=> '',
22
+ 'Supermarket '=> ''
23
+ }
24
+
25
+ class String
26
+ TRANSLIT_RUSSIAN = 'АБВГДЕЁЗИЙКЛМНОПРСТУФЪЫЬЭабвгдеёзийклмнопрстуфъыьэ'
27
+ TRANSLIT_LATIN = "ABVGDEEZIJKLMNOPRSTUF\"Y'Eabvgdeezijklmnoprstuf\"y'e"
28
+ TRANSLIT_DOUBLES = {'Ж'=>'ZH', 'Х'=>'KH', 'Ц'=>'TS', 'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHCH', 'Ю'=>'YU', 'Я'=>'YA',
29
+ 'ж'=>'zh', 'х'=>'kh', 'ц'=>'ts', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'shch', 'ю'=>'yu', 'я'=>'ya'}
30
+
31
+ def translit!
32
+ TRANSLIT_DOUBLES.each {|key, value| self.gsub!(key, value)}
33
+ self.tr!(TRANSLIT_RUSSIAN, TRANSLIT_LATIN)
34
+ self
35
+ end
36
+ end
37
+
38
+ #$stdout.set_encoding(STDOUT_ENCODING, :undef=>:replace)
39
+ File.open(OUTFILE_NAME, 'w:'+ (OUTFILE_ENCODING), :undef => :replace) do |outfile|
40
+ File.open(INFILE_NAME, 'r:'+ (INFILE_ENCODING), :undef => :replace).each_line do |line|
41
+ puts line, '-----------'
42
+ case line.chars.first
43
+ when 'D' # Date field - convert to MM/DD/YYYY format expected by Quicken
44
+ line.gsub! /(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3'
45
+ when 'P' # Payee field
46
+ # Convert payee from Russian to translit
47
+ line.translit!
48
+ # Capitalize each word and remove extra whitespaces (leaves first char intact)
49
+ line[1..-1] = line[1..-1].scan(/\w+/).map{|word| word.capitalize}.join(' ')
50
+ # Preprocess Payee field making pre-defined replacements
51
+ REPLACES.each {|key, value| line.gsub!(key, value)}
52
+ when 'M' # Memo field - drop if empty
53
+ line.clear if line.rstrip == 'M'
54
+ end
55
+
56
+ outfile.puts line unless line.empty?
57
+ puts line, '-----------'
58
+ end
59
+ end
@@ -0,0 +1,56 @@
1
+ module MyScripts
2
+ # This script converts given QIF file (with downloaded citibank statement)
3
+ # into Quicken 2008 compatible format, and outputs it into new file for
4
+ # import into Quicken
5
+ #
6
+ class Citi < Script
7
+ INFILE_ENCODING = 'CP1251:UTF-8' # Encoding pair of input file ('File external:Ruby internal')
8
+ OUTFILE_ENCODING = 'CP1252:UTF-8' # Encoding pair of output file ('File external:Ruby internal')
9
+
10
+ REPLACES = {
11
+ /Pokupka Masterkard .*(Detskiy Mir).*/ => "Detski Mir\nLChildcare",
12
+ /Pokupka Masterkard .*(Medicina).*/ => "AO Medicina\nLMedical:Medicine",
13
+ /Pokupka Masterkard .*(Pharmacy).*/ => "\\1\nLMedical:Medicine",
14
+ /Pokupka Masterkard .*(Alye Parusa).*/ => "\\1\nLGroceries",
15
+ /Pokupka Masterkard .*(Perekrestok).*/ => "\\1\nLGroceries",
16
+ /Pokupka Masterkard .*(Ile De Beaute).*/ => "\\1\nLPersonal Care",
17
+ /Pokupka Masterkard .*(Beeline).*/ => "\\1\nLCommunications:Telephone",
18
+ /Pokupka Masterkard (.+) (Moscow Ru.+|Moskva Ru.+)/ => "\\1\nM\\2\nLHousehold",
19
+ /Vkhodyashchij Platezh(.+Bank Moskvy)/ => "Incoming transfer\nM\\1\nL[BM New Prestige]",
20
+ /Oplata Dolg Kr Karta(.+)/ => "Transfer to Credit card\nM\\1\nL[Citi MC]",
21
+ /Snyatie Nalichnykh(.+)/ => "Cash withdrawal\nM\\1\nL[Cash RUB]",
22
+ /Vznos Nalichnykh(.+)/ => "Cash deposit\nM\\1\nL[Cash RUB]",
23
+ /Kom Za Obsluzhivanie/ => "Citibank\nMService fee\nLFinance:Bank Charge",
24
+ 'Universam '=> '',
25
+ 'Supermarket '=> ''
26
+ }
27
+
28
+ def run
29
+ usage "in_file.qif [out_file.qif]" if @argv.empty?
30
+
31
+ in_file = @argv.first
32
+
33
+ # If 2nd Arg is given, it is out_file name, otherwise derive from in_file name
34
+ out_file = @argv[1] ? @argv[1] : in_file.sub(/\.qif|$/, '_out.qif')
35
+
36
+ # All the other args lumped into message, or default message
37
+ message = @argv.empty? ? 'Commit' : @argv.join(' ')
38
+ # Timestamp added to message
39
+ message += " #{Time.now.to_s[0..-6]}"
40
+
41
+ puts "Committing (versionup =#{bump}) with message: #{message}"
42
+
43
+ system %Q[git add --all]
44
+ case bump
45
+ when 1..9
46
+ system %Q[rake version:bump:patch]
47
+ when 10..99
48
+ system %Q[rake version:bump:minor]
49
+ when 10..99
50
+ system %Q[rake version:bump:major]
51
+ end
52
+ system %Q[git commit -a -m "#{message}" --author arvicco]
53
+ system %Q[git push]
54
+ end
55
+ end
56
+ end
@@ -1,24 +1,39 @@
1
1
  module MyScripts
2
+ # This class encapsulates Command Line Interface for running scripts. It can be instantiated
3
+ # with stubbed stdin and stdout if you want to run tests on your scripts
2
4
  class CLI
5
+ class ScriptNameError < NameError # :nodoc:
6
+ def initialize(message=nil)
7
+ message ? super(message) : super
8
+ end
9
+ end
3
10
 
4
11
  attr_accessor :stdout, :stdin
5
12
 
13
+ # Instantiates new CLI(command line interface) and runs script with given name (token)
14
+ # and argv inside new CLI instance
6
15
  def self.run( script_name, argv )
7
16
  new.run script_name, argv
8
17
  end
9
18
 
19
+ # Creates new command line interface
10
20
  def initialize( stdin=$stdin, stdout=$stdout )
11
21
  @stdin = stdin
12
22
  @stdout = stdout
13
23
  end
14
24
 
25
+ # Runs a script with given name (token) and argv inside this CLI instance
15
26
  def run( script_name, argv )
16
- script = "MyScripts::#{script_name.capitalize}".to_class
27
+ script = script_class_name(script_name).to_class
28
+ raise ScriptNameError.new("Script #{script_class_name(script_name)} not found") unless script
29
+
17
30
  script.new(script_name, argv, self).run
18
- rescue => e
19
- @stdout.puts e.backtrace
20
- @stdout.puts e.message
21
- exit 1
31
+ end
32
+
33
+ private
34
+
35
+ def script_class_name(script_name)
36
+ "MyScripts::#{script_name.to_s.camel_case}"
22
37
  end
23
38
  end
24
39
  end
@@ -0,0 +1,17 @@
1
+ module MyScripts
2
+ # Dummy script skeleton that is ready for your usage. Just put your script code
3
+ # inside run method (if you use ARGV, change it to @argv). Done!
4
+ # You can immediately run your script anywhere using following command:
5
+ # $ dummy Whatever arguments your code expects and processes
6
+ #
7
+ class Dummy < Script
8
+ def run
9
+ # here you do all actual work for your script
10
+ # you have following instance vars at your disposal:
11
+ # @name - your script name,
12
+ # @argv - your ARGV,
13
+ # @cli - CLI runner (holds references to stdin and stdout)
14
+ #...
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,18 @@
1
+ # encoding: UTF-8
2
+
1
3
  class String
4
+ TRANSLIT_CYRILLIC = 'АБВГДЕЁЗИЙКЛМНОПРСТУФЪЫЬЭабвгдеёзийклмнопрстуфъыьэ'
5
+ TRANSLIT_LATIN = "ABVGDEEZIJKLMNOPRSTUF\"Y'Eabvgdeezijklmnoprstuf\"y'e"
6
+ TRANSLIT_DOUBLES = {'Ж'=>'ZH', 'Х'=>'KH', 'Ц'=>'TS', 'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHCH', 'Ю'=>'YU', 'Я'=>'YA',
7
+ 'ж'=>'zh', 'х'=>'kh', 'ц'=>'ts', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'shch', 'ю'=>'yu', 'я'=>'ya'}
8
+
9
+ # Performs basic transliteration from Cyrillic to Latin characters and standard character combinations
10
+ def translit!
11
+ TRANSLIT_DOUBLES.each {|key, value| self.gsub!(key, value)}
12
+ self.tr!(TRANSLIT_CYRILLIC, TRANSLIT_LATIN)
13
+ self
14
+ end
15
+
2
16
  # Turns string into appropriate class constant, returns nil if class not found
3
17
  def to_class
4
18
  klass = self.split("::").inject(Kernel) do |namespace, const|
@@ -8,4 +22,22 @@ class String
8
22
  rescue NameError
9
23
  nil
10
24
  end
25
+
26
+ # Turns string into snake_case
27
+ def snake_case
28
+ gsub(/([a-z])([A-Z0-9])/, '\1_\2' ).downcase
29
+ end
30
+
31
+ # Turns string into CamelCase
32
+ def camel_case
33
+ if self.include? '_'
34
+ self.split('_').map{|e| e.capitalize}.join
35
+ else
36
+ unless self =~ (/^[A-Z]/)
37
+ self.capitalize
38
+ else
39
+ self
40
+ end
41
+ end
42
+ end
11
43
  end
@@ -1,7 +1,11 @@
1
1
  module MyScripts
2
- class Gitcp < Script
2
+ # This script wraps up all work done on project in current directory,
3
+ # adds all new(unversioned) files to git, commits all work done so far,
4
+ # optionally bumps project version and pushes changes to remote repo
5
+ #
6
+ class Gitto < Script
3
7
  def run
4
- usage "[bump] Commit message goes here" if @argv.empty?
8
+ usage "[bump: 1 - patch, 10 - minor, 100 - major] Commit message goes here" if @argv.empty?
5
9
 
6
10
  # If first Arg is a number, it indicates version bump
7
11
  bump = @argv[0].to_i > 0 ? @argv.shift.to_i : 0
@@ -14,7 +18,6 @@ module MyScripts
14
18
  puts "Committing (versionup =#{bump}) with message: #{message}"
15
19
 
16
20
  system %Q[git add --all]
17
- system %Q[git commit -a -m "#{message}" --author arvicco]
18
21
  case bump
19
22
  when 1..9
20
23
  system %Q[rake version:bump:patch]
@@ -23,6 +26,7 @@ module MyScripts
23
26
  when 10..99
24
27
  system %Q[rake version:bump:major]
25
28
  end
29
+ system %Q[git commit -a -m "#{message}" --author arvicco]
26
30
  system %Q[git push]
27
31
  end
28
32
  end
@@ -1,4 +1,7 @@
1
1
  module MyScripts
2
+ # This script uses Jeweler to create new project skeleton, local git repo and
3
+ # initiate remote repo on github
4
+ #
2
5
  class Jew < Script
3
6
  def run
4
7
  usage "project_name Summary or description goes here" if @argv.empty?
@@ -1,5 +1,8 @@
1
1
  module MyScripts
2
2
  class Mybones < Script
3
+ # This script uses Mr.Bones gem to create new project skeleton, local git repo and
4
+ # initiate remote repo on github
5
+ #
3
6
  def run
4
7
  usage "project_name Summary or description goes here" if @argv.empty?
5
8
 
@@ -1,4 +1,5 @@
1
1
  module MyScripts
2
+ # Starts and controls rabbitmq server
2
3
  class Rabbit < Script
3
4
  def run
4
5
  case @argv.shift
@@ -1,11 +1,16 @@
1
1
  module MyScripts
2
- class Script # Base class for all scripts
2
+ # Base class for all scripts. Subclass it and override run method with actual
3
+ # work your script will be doing
4
+ class Script
3
5
  def initialize( name, argv, cli )
4
6
  @name = name
5
7
  @argv = argv
6
8
  @cli = cli
7
9
  end
8
10
 
11
+ def run
12
+ end
13
+
9
14
  def puts *args
10
15
  @cli.stdout.puts *args
11
16
  end
data/lib/my_scripts.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # Top level namespace
1
2
  module MyScripts
3
+
4
+ # Used to auto-require all the source files located in lib/my_scripts
2
5
  def self.require_libs( filename, filemask )
3
6
  file = ::File.expand_path(::File.join(::File.dirname(filename), filemask.gsub(/(?<!.rb)$/,'.rb')))
4
7
  require file if File.exist?(file) && !File.directory?(file)
data/my_scripts.gemspec CHANGED
@@ -5,14 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{my_scripts}
8
- s.version = "0.0.7"
8
+ s.version = "0.0.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["arvicco"]
12
- s.date = %q{2010-01-22}
13
- s.description = %q{Collection of simple scripts (mostly dev-related)}
12
+ s.date = %q{2010-02-12}
13
+ s.description = %q{Simple framework for writing command-line scripts and collection of my own scripts (mostly dev-related)}
14
14
  s.email = %q{arvitallian@gmail.com}
15
- s.executables = ["gitcp", "jew", "mybones", "rabbit"]
15
+ s.executables = ["citi", "dummy", "gitto", "jew", "mybones", "rabbit", "recode.rb"]
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE",
18
18
  "README.rdoc"
@@ -24,23 +24,29 @@ Gem::Specification.new do |s|
24
24
  "README.rdoc",
25
25
  "Rakefile",
26
26
  "VERSION",
27
- "bin/gitcp",
27
+ "bin/citi",
28
+ "bin/dummy",
29
+ "bin/gitto",
28
30
  "bin/jew",
29
31
  "bin/mybones",
30
32
  "bin/rabbit",
33
+ "bin/recode.rb",
31
34
  "features/my_scripts.feature",
32
35
  "features/step_definitions/my_scripts_steps.rb",
33
36
  "features/support/env.rb",
34
37
  "lib/my_scripts.rb",
38
+ "lib/my_scripts/citi.rb",
35
39
  "lib/my_scripts/cli.rb",
40
+ "lib/my_scripts/dummy.rb",
36
41
  "lib/my_scripts/extensions.rb",
37
- "lib/my_scripts/gitcp.rb",
42
+ "lib/my_scripts/gitto.rb",
38
43
  "lib/my_scripts/jew.rb",
39
44
  "lib/my_scripts/mybones.rb",
40
45
  "lib/my_scripts/rabbit.rb",
41
46
  "lib/my_scripts/script.rb",
42
47
  "my_scripts.gemspec",
43
48
  "spec/my_scripts/extensions_spec.rb",
49
+ "spec/my_scripts_spec.rb",
44
50
  "spec/spec.opts",
45
51
  "spec/spec_helper.rb"
46
52
  ]
@@ -48,9 +54,10 @@ Gem::Specification.new do |s|
48
54
  s.rdoc_options = ["--charset=UTF-8"]
49
55
  s.require_paths = ["lib"]
50
56
  s.rubygems_version = %q{1.3.5}
51
- s.summary = %q{A collection of ascripts}
57
+ s.summary = %q{Simple framework for writing command-line scripts}
52
58
  s.test_files = [
53
59
  "spec/my_scripts/extensions_spec.rb",
60
+ "spec/my_scripts_spec.rb",
54
61
  "spec/spec_helper.rb"
55
62
  ]
56
63
 
@@ -1,5 +1,4 @@
1
- require File.expand_path(
2
- File.join(File.dirname(__FILE__), '..', 'spec_helper'))
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
3
2
 
4
3
  module MyScriptsTest
5
4
  module A
@@ -10,6 +9,26 @@ module MyScriptsTest
10
9
  end
11
10
 
12
11
  describe String do
12
+ context '#snake_case' do
13
+ it 'transforms CamelCase strings' do
14
+ 'GetCharWidth32'.snake_case.should == 'get_char_width_32'
15
+ end
16
+
17
+ it 'leaves snake_case strings intact' do
18
+ 'keybd_event'.snake_case.should == 'keybd_event'
19
+ end
20
+ end
21
+
22
+ context '#camel_case' do
23
+ it 'transforms underscore strings to CamelCase' do
24
+ 'get_char_width_32'.camel_case.should == 'GetCharWidth32'
25
+ end
26
+
27
+ it 'leaves CamelCase strings intact' do
28
+ 'GetCharWidth32'.camel_case.should == 'GetCharWidth32'
29
+ end
30
+ end
31
+
13
32
  context '#to_class' do
14
33
  it 'converts string into appropriate Class constant' do
15
34
  "Fixnum".to_class.should == Fixnum
@@ -0,0 +1,82 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ module MyScriptsTest
4
+
5
+ # creates new CLI object with mock stdin and stdout,
6
+ # stdin optionally preloaded with fake user input
7
+ def create_cli( opts={} )
8
+ @stdout = options[:stdout] || mock('stdout').as_null_object
9
+ @stdin = options[:stdin] || mock('stdin')
10
+ @stdin.stub(:gets).and_return(*options[:input]) if options[:input]
11
+ @cli = MyScripts::CLI.new @stdin, @stdout
12
+ end
13
+
14
+ # sets expectation for stdout to receive strictly ordered sequence of exact messages
15
+ def stdout_should_receive(*messages)
16
+ messages.each do |message|
17
+ @stdout.should_receive(:puts).with(message).once.ordered
18
+ end
19
+ end
20
+
21
+ # sets expectation for stdout to receive message(s) containing all of the patterns (unordered)
22
+ def stdout_should_include(*patterns)
23
+ patterns.each do |pattern|
24
+ re = Regexp === pattern ? pattern : Regexp.new(Regexp.escape(pattern))
25
+ @stdout.should_receive(:puts).with(re).at_least(:once)
26
+ end
27
+ end
28
+
29
+ describe 'Script Execution' do
30
+
31
+ context 'trying to execute undefined script' do
32
+ it 'fails' do
33
+ cli = create_cli
34
+ proc{cli.run( :undefined, [])}.should raise_error "Script MyScripts::Undefined not found"
35
+ end
36
+ end
37
+
38
+ context 'executing pre-defined scripts' do
39
+ MyScripts.module_eval "
40
+ # Stub script that just puts 'OK' to stdout
41
+ class Scriptest < Script
42
+ def run
43
+ puts 'OK'
44
+ 1
45
+ end
46
+ end"
47
+
48
+ it 'executes pre-defined script without args' do
49
+ cli = create_cli
50
+ stdout_should_receive('OK')
51
+ cli.run( :scriptest, [])
52
+ end
53
+
54
+ it 'executes pre-defined script with args' do
55
+ cli = create_cli
56
+ stdout_should_receive('OK')
57
+ cli.run( :scriptest, [1, 2, 3, :four, 'five'])
58
+ end
59
+
60
+ it 'returns return value of Script#run() when running pre-defined script' do
61
+ cli = create_cli
62
+ cli.run( :scriptest, []).should == 1
63
+ end
64
+ end
65
+
66
+ context 'executing scripts with snake_case names' do
67
+ MyScripts.module_eval "
68
+ # Stub script that just puts 'OK' to stdout
69
+ class SnakeScript < Script
70
+ def run
71
+ puts 'OK'
72
+ end
73
+ end"
74
+
75
+ it 'executes scripts with snake_case name' do
76
+ cli = create_cli
77
+ stdout_should_receive('OK')
78
+ cli.run( :snake_script, [])
79
+ end
80
+ end
81
+ end
82
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: my_scripts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - arvicco
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-22 00:00:00 +03:00
12
+ date: 2010-02-12 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,13 +32,16 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: "0"
34
34
  version:
35
- description: Collection of simple scripts (mostly dev-related)
35
+ description: Simple framework for writing command-line scripts and collection of my own scripts (mostly dev-related)
36
36
  email: arvitallian@gmail.com
37
37
  executables:
38
- - gitcp
38
+ - citi
39
+ - dummy
40
+ - gitto
39
41
  - jew
40
42
  - mybones
41
43
  - rabbit
44
+ - recode.rb
42
45
  extensions: []
43
46
 
44
47
  extra_rdoc_files:
@@ -51,23 +54,29 @@ files:
51
54
  - README.rdoc
52
55
  - Rakefile
53
56
  - VERSION
54
- - bin/gitcp
57
+ - bin/citi
58
+ - bin/dummy
59
+ - bin/gitto
55
60
  - bin/jew
56
61
  - bin/mybones
57
62
  - bin/rabbit
63
+ - bin/recode.rb
58
64
  - features/my_scripts.feature
59
65
  - features/step_definitions/my_scripts_steps.rb
60
66
  - features/support/env.rb
61
67
  - lib/my_scripts.rb
68
+ - lib/my_scripts/citi.rb
62
69
  - lib/my_scripts/cli.rb
70
+ - lib/my_scripts/dummy.rb
63
71
  - lib/my_scripts/extensions.rb
64
- - lib/my_scripts/gitcp.rb
72
+ - lib/my_scripts/gitto.rb
65
73
  - lib/my_scripts/jew.rb
66
74
  - lib/my_scripts/mybones.rb
67
75
  - lib/my_scripts/rabbit.rb
68
76
  - lib/my_scripts/script.rb
69
77
  - my_scripts.gemspec
70
78
  - spec/my_scripts/extensions_spec.rb
79
+ - spec/my_scripts_spec.rb
71
80
  - spec/spec.opts
72
81
  - spec/spec_helper.rb
73
82
  has_rdoc: true
@@ -97,7 +106,8 @@ rubyforge_project:
97
106
  rubygems_version: 1.3.5
98
107
  signing_key:
99
108
  specification_version: 3
100
- summary: A collection of ascripts
109
+ summary: Simple framework for writing command-line scripts
101
110
  test_files:
102
111
  - spec/my_scripts/extensions_spec.rb
112
+ - spec/my_scripts_spec.rb
103
113
  - spec/spec_helper.rb