my_utilities 1.2.0
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 +17 -0
- data/.travis.yml +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/lib/my_utilities.rb +135 -0
- data/lib/my_utilities/version.rb +3 -0
- data/my_utilities.gemspec +26 -0
- data/spec/logger_spec.rb +75 -0
- data/spec/spec_helper.rb +8 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 44d1fd2cb533d01414e41413e15b0d4cd29ff7d1
|
4
|
+
data.tar.gz: f243196f1fabbdba3114d2790943f4801eb3d859
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f02837bbb7c37bbb7ee68f0b3fb6cbfcbe6889d60dab132830de7af7cf9d2e9f543ab34d41e9a83a7331114b3db42e74beebb4dc90075acd5caa60cd41a61022
|
7
|
+
data.tar.gz: 4d1d066c36d9c455a976c041f187fdf8982efc55f9b080405c42bcb84446d60d6a83ef2bbcbff84dee9b9e90ec8a8088593e2e54c1a2ace12450ca3aba0edd9c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Sameer Siruguri
|
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,29 @@
|
|
1
|
+
# MyUtilities
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'my_utilities'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install my_utilities
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/my_utilities.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require "my_utilities/version"
|
2
|
+
|
3
|
+
module MyUtilities
|
4
|
+
# MyUtilities class variables
|
5
|
+
@help_string=""
|
6
|
+
@used_single_options = []
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :help_string
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.print_help_and_exit
|
13
|
+
puts <<EOS
|
14
|
+
#{__FILE__} [options]
|
15
|
+
#{@help_string}
|
16
|
+
EOS
|
17
|
+
|
18
|
+
exit -1
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.error_exit(msg)
|
22
|
+
puts "ERROR: #{msg}"
|
23
|
+
exit -1
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.process_cli(opt_map)
|
27
|
+
return [] if Hash != opt_map.class or opt_map.empty?
|
28
|
+
return_array = []
|
29
|
+
|
30
|
+
opt_array = []
|
31
|
+
key_of = {}
|
32
|
+
opt_map.each do |k, v|
|
33
|
+
# If one of the keys is not a symbol, let's abort.
|
34
|
+
return [] unless k.is_a? Symbol
|
35
|
+
|
36
|
+
|
37
|
+
matches = /^\-(.)(:\-\-(.*)?)/.match v
|
38
|
+
single_opt = long_opt = nil
|
39
|
+
|
40
|
+
# We will generate options regardless of whether the input string is valid.
|
41
|
+
if !matches.nil?
|
42
|
+
single_opt = matches[1]
|
43
|
+
long_opt = matches[3]
|
44
|
+
end
|
45
|
+
|
46
|
+
gen_opt1, gen_opt2 = generate_option_strings
|
47
|
+
single_opt ||= gen_opt1
|
48
|
+
long_opt ||= gen_opt2
|
49
|
+
key_of["--#{long_opt}"]=k
|
50
|
+
|
51
|
+
opt_array << ["--#{long_opt}," "-#{single_opt}", GetoptLong::OPTIONAL_ARGUMENT]
|
52
|
+
}
|
53
|
+
|
54
|
+
opts = GetoptLong.new opt_array
|
55
|
+
|
56
|
+
opts.each do |opt, arg|
|
57
|
+
return_array[key_of[opt]]=arg
|
58
|
+
end
|
59
|
+
|
60
|
+
return_array
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
class Logger
|
67
|
+
DEBUG=4
|
68
|
+
INFO=3
|
69
|
+
WARN=2
|
70
|
+
ERROR=1
|
71
|
+
FATAL=0
|
72
|
+
|
73
|
+
def initialize(level=FATAL)
|
74
|
+
@mesg_level = level.to_i
|
75
|
+
end
|
76
|
+
|
77
|
+
def debug(mesg)
|
78
|
+
self.puts mesg, DEBUG
|
79
|
+
end
|
80
|
+
|
81
|
+
def fatal(mesg)
|
82
|
+
self.puts mesg, FATAL
|
83
|
+
end
|
84
|
+
|
85
|
+
def puts(mesg, level=DEBUG)
|
86
|
+
if @mesg_level >= level
|
87
|
+
super mesg
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
module MyDir
|
93
|
+
def self.included base
|
94
|
+
base.extend MyDirMethods
|
95
|
+
end
|
96
|
+
module MyDirMethods
|
97
|
+
def parent_dir_match(rx, dir=".")
|
98
|
+
|
99
|
+
# nil if this isn't a directory we're starting from
|
100
|
+
return nil if !Dir.exists? dir
|
101
|
+
|
102
|
+
return dir if !(Dir.entries(dir).select { |x| rx.match x and File.file? File.join(dir, x) }.empty?)
|
103
|
+
|
104
|
+
# Don't recurse if the dir's parent is the dir
|
105
|
+
return parent_dir_match(rx, File.join(dir, "..")) if(File.expand_path(dir) != '/')
|
106
|
+
nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
def self.generate_option_strings
|
113
|
+
a=generate_single_option
|
114
|
+
return [a, "#{a}_long_option"]
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.generate_single_option
|
118
|
+
# This will fail terribly after 62 incorrectly specified options
|
119
|
+
([a..z]+[A..Z]+[0..9]).each do |opt|
|
120
|
+
unless @used_single_options.include? opt
|
121
|
+
@used_single_options << opt
|
122
|
+
return opt
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
return 'a'
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
class Dir
|
132
|
+
include MyUtilities::MyDir
|
133
|
+
end
|
134
|
+
|
135
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'my_utilities/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "my_utilities"
|
8
|
+
spec.version = MyUtilities::VERSION
|
9
|
+
spec.authors = ["Sameer Siruguri"]
|
10
|
+
spec.email = ["sameers.public@gmail.com"]
|
11
|
+
spec.description = %q{Some common utilities I've created for my Ruby work}
|
12
|
+
spec.summary = %q{These functions do a bunch of cool things, like log error messages, find a parent folder with a specific file in it, and so on.}
|
13
|
+
spec.homepage = ""
|
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{^(spec)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'getoptlong'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
data/spec/logger_spec.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative "../lib/my_utilities"
|
3
|
+
|
4
|
+
describe MyUtilities do
|
5
|
+
describe "::print_help_and_exit" do
|
6
|
+
before do
|
7
|
+
# Set the help string
|
8
|
+
@help_string = 'this is the help string'
|
9
|
+
MyUtilities.help_string = @help_string
|
10
|
+
end
|
11
|
+
|
12
|
+
it "prints out the help string" do
|
13
|
+
full_help_string = "#{__FILE__} [options]\n#{@help_string}\n"
|
14
|
+
STDOUT.should_receive(:puts).with(/#{@help_string}/)
|
15
|
+
lambda { MyUtilities::print_help_and_exit }.should raise_error(SystemExit)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe MyUtilities::Logger do
|
20
|
+
before do
|
21
|
+
@exp_mesg="Fatal, die now!"
|
22
|
+
end
|
23
|
+
|
24
|
+
before do
|
25
|
+
@logger_default=MyUtilities::Logger.new
|
26
|
+
@logger_by_level = MyUtilities::Logger.new(MyUtilities::Logger::WARN)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "only shows fatal message by default" do
|
30
|
+
STDOUT.should_receive(:puts).with(@exp_mesg)
|
31
|
+
@logger_default.puts(@exp_mesg, MyUtilities::Logger::FATAL)
|
32
|
+
end
|
33
|
+
|
34
|
+
context "level is init'ed (to WARN)" do
|
35
|
+
it "won't print debug messages" do
|
36
|
+
STDOUT.should_not_receive(:puts)
|
37
|
+
@logger_by_level.puts(@exp_mesg, MyUtilities::Logger::DEBUG)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "will print WARN messages" do
|
41
|
+
STDOUT.should_receive(:puts).with(@exp_mesg)
|
42
|
+
@logger_by_level.puts(@exp_mesg, MyUtilities::Logger::WARN)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "will print fatal messages" do
|
46
|
+
STDOUT.should_receive(:puts).with(@exp_mesg)
|
47
|
+
@logger_by_level.puts(@exp_mesg, MyUtilities::Logger::FATAL)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it "only shows debug messages if no level is given and debug level is init'ed" do
|
52
|
+
|
53
|
+
@logger_debug = MyUtilities::Logger.new(MyUtilities::Logger::DEBUG)
|
54
|
+
|
55
|
+
STDOUT.should_receive(:puts).with(@exp_mesg)
|
56
|
+
@logger_debug.puts(@exp_mesg)
|
57
|
+
|
58
|
+
STDOUT.should_not_receive(:puts)
|
59
|
+
@logger_by_level.puts(@exp_mesg)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe MyUtilities::MyDir do
|
64
|
+
before do
|
65
|
+
Dir.chdir 'spec/tmp/test_parent_if/level1' unless /level1/.match Dir.pwd
|
66
|
+
end
|
67
|
+
it "should work positive" do
|
68
|
+
Dir.parent_dir_match(Regexp.new('findthis')).should eq('./..')
|
69
|
+
Dir.parent_dir_match(Regexp.new('findthis'), '../..').should eq('../..')
|
70
|
+
end
|
71
|
+
it "should not work negative" do
|
72
|
+
Dir.parent_dir_match(Regexp.new('cantfindthis')).should be_nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: my_utilities
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sameer Siruguri
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: getoptlong
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Some common utilities I've created for my Ruby work
|
70
|
+
email:
|
71
|
+
- sameers.public@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/my_utilities.rb
|
83
|
+
- lib/my_utilities/version.rb
|
84
|
+
- my_utilities.gemspec
|
85
|
+
- spec/logger_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: ''
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.1.11
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: These functions do a bunch of cool things, like log error messages, find
|
111
|
+
a parent folder with a specific file in it, and so on.
|
112
|
+
test_files:
|
113
|
+
- spec/logger_spec.rb
|
114
|
+
- spec/spec_helper.rb
|