nrser 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +6 -0
- data/lib/nrser.rb +114 -0
- data/lib/nrser/version.rb +3 -0
- data/nrser.gemspec +23 -0
- data/spec/nrser/common_prefix_spec.rb +30 -0
- data/spec/nrser/dedent_spec.rb +36 -0
- data/spec/nrser/format_exception_spec.rb +25 -0
- data/spec/nrser/indent_spec.rb +37 -0
- data/spec/nrser/template_spec.rb +32 -0
- data/spec/nrser_spec.rb +9 -0
- data/spec/spec_helper.rb +2 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4421226e39e63c2cca9d4c141a63e794db05427f
|
4
|
+
data.tar.gz: d743e1a89bab47750aece9d6b56fd00e85e151f6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d785674fa973059b39fd67bf9ef2ff8052b89b2c70e74b016b1a8fe659693d2690b20076713a0d9da80c5a520753fce5ae896943d081f5f961e7d374dc5190f1
|
7
|
+
data.tar.gz: ec7b5f087c9ccb8d8da45dfe55cfd592edbed12f7bb9c32685595e52c4a64db45df01404c4e7cd5ccb539b51a64968952e482f5fb2091a1e1135f3a01efbbf38
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
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
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
tmp/
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 nrser
|
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,35 @@
|
|
1
|
+
# NRSER Ruby lib
|
2
|
+
|
3
|
+
basic ruby utils i use in a lot of stuff.
|
4
|
+
|
5
|
+
these tools are hastily written or copied from other sources.
|
6
|
+
|
7
|
+
they are not fast.
|
8
|
+
|
9
|
+
they are not optimized.
|
10
|
+
|
11
|
+
they are largely untested.
|
12
|
+
|
13
|
+
proceed with caution.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'nrser'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install nrser
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( https://github.com/nrser/nrser-ruby/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/nrser.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require "nrser/version"
|
2
|
+
|
3
|
+
module NRSER
|
4
|
+
module KernelRefinements
|
5
|
+
def tpl *args
|
6
|
+
NRSER::template *args
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
refine Object do
|
11
|
+
include KernelRefinements
|
12
|
+
|
13
|
+
def pipe
|
14
|
+
yield self
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
refine String do
|
20
|
+
def unblock
|
21
|
+
NRSER.unblock self
|
22
|
+
end
|
23
|
+
|
24
|
+
def dedent
|
25
|
+
NRSER.dedent self
|
26
|
+
end
|
27
|
+
|
28
|
+
def indent
|
29
|
+
NRSER.indent self
|
30
|
+
end
|
31
|
+
end # refine String
|
32
|
+
|
33
|
+
refine Exception do
|
34
|
+
def format
|
35
|
+
NRSER.format_exception self
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
using NRSER
|
40
|
+
|
41
|
+
def self.unblock str
|
42
|
+
parts = str.split(/\n\s+/)
|
43
|
+
if m = parts[0].match(/^\s+/)
|
44
|
+
parts[0] = parts[0][m.end(0)..-1]
|
45
|
+
end
|
46
|
+
if m = parts[-1].match(/\s+$/)
|
47
|
+
parts[-1] = parts[-1][0..m.begin(0)]
|
48
|
+
end
|
49
|
+
parts.join ' '
|
50
|
+
end # unblock
|
51
|
+
|
52
|
+
def self.common_prefix strings
|
53
|
+
raise ArgumentError.new("argument can't be empty") if strings.empty?
|
54
|
+
sorted = strings.sort.reject {|line| line == "\n"}
|
55
|
+
i = 0
|
56
|
+
while sorted.first[i] == sorted.last[i] &&
|
57
|
+
i < [sorted.first.length, sorted.last.length].min
|
58
|
+
i = i + 1
|
59
|
+
end
|
60
|
+
strings.first[0...i]
|
61
|
+
end # common_prefix
|
62
|
+
|
63
|
+
def self.dedent str
|
64
|
+
return str if str.empty?
|
65
|
+
lines = str.lines
|
66
|
+
indent = common_prefix(lines).match(/^\s*/)[0]
|
67
|
+
return str if indent.empty?
|
68
|
+
lines.map {|line|
|
69
|
+
line = line[indent.length..line.length] if line.start_with? indent
|
70
|
+
}.join
|
71
|
+
end # dedent
|
72
|
+
|
73
|
+
def self.filter_repeated_blank_lines str
|
74
|
+
out = []
|
75
|
+
lines = str.lines
|
76
|
+
skipping = false
|
77
|
+
str.lines.each do |line|
|
78
|
+
if line =~ /^\s*$/
|
79
|
+
unless skipping
|
80
|
+
out << line
|
81
|
+
end
|
82
|
+
skipping = true
|
83
|
+
else
|
84
|
+
skipping = false
|
85
|
+
out << line
|
86
|
+
end
|
87
|
+
end
|
88
|
+
out.join
|
89
|
+
end # filter_repeated_blank_lines
|
90
|
+
|
91
|
+
def self.template bnd, str
|
92
|
+
require 'erb'
|
93
|
+
filter_repeated_blank_lines ERB.new(dedent(str)).result(bnd)
|
94
|
+
end # template
|
95
|
+
|
96
|
+
def self.format_exception e
|
97
|
+
msg = "#{ e.message } (#{ e.class }):\n #{ e.backtrace.join("\n ") }"
|
98
|
+
File.open('./tmp/out.txt', 'w') do |f|
|
99
|
+
f.puts msg
|
100
|
+
end
|
101
|
+
msg
|
102
|
+
end
|
103
|
+
|
104
|
+
# adapted from acrive_support 4.2.0
|
105
|
+
#
|
106
|
+
# <https://github.com/rails/rails/blob/7847a19f476fb9bee287681586d872ea43785e53/activesupport/lib/active_support/core_ext/string/indent.rb>
|
107
|
+
#
|
108
|
+
def self.indent str, amount = 2, indent_string=nil, indent_empty_lines=false
|
109
|
+
indent_string = indent_string || str[/^[ \t]/] || ' '
|
110
|
+
re = indent_empty_lines ? /^/ : /^(?!$)/
|
111
|
+
str.gsub(re, indent_string * amount)
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
data/nrser.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 'nrser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nrser"
|
8
|
+
spec.version = NRSER::VERSION
|
9
|
+
spec.authors = ["nrser"]
|
10
|
+
spec.email = ["neil@neilsouza.com"]
|
11
|
+
spec.summary = %q{basic ruby utils i use in a lot of stuff.}
|
12
|
+
spec.homepage = "https://github.com/nrser/nrser-ruby"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "NRSER.common_prefix" do
|
4
|
+
it "raises an error if argument is empty" do
|
5
|
+
expect{ NRSER.common_prefix [] }.to raise_error ArgumentError
|
6
|
+
end
|
7
|
+
|
8
|
+
it "works on a single string" do
|
9
|
+
expect( NRSER.common_prefix [''] ).to eq ''
|
10
|
+
expect( NRSER.common_prefix ['aaa'] ).to eq 'aaa'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works on a simple example" do
|
14
|
+
expect( NRSER.common_prefix ['aaa', 'acb', 'abc'] ).to eq 'a'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "works when the strings are all the same" do
|
18
|
+
expect( NRSER.common_prefix ['aaa', 'aaa', 'aaa'] ).to eq 'aaa'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "finds indents" do
|
22
|
+
expect(
|
23
|
+
NRSER.common_prefix <<-BLOCK.lines
|
24
|
+
def f x
|
25
|
+
x * x
|
26
|
+
end
|
27
|
+
BLOCK
|
28
|
+
).to eq " "
|
29
|
+
end
|
30
|
+
end # common_prefix
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "NRSER.dedent" do
|
4
|
+
it "removes indents" do
|
5
|
+
expect(
|
6
|
+
NRSER.dedent <<-BLOCK
|
7
|
+
def f x
|
8
|
+
x * x
|
9
|
+
end
|
10
|
+
BLOCK
|
11
|
+
).to eq <<-BLOCK
|
12
|
+
def f x
|
13
|
+
x * x
|
14
|
+
end
|
15
|
+
BLOCK
|
16
|
+
end
|
17
|
+
|
18
|
+
it "ignores things it that aren't indented" do
|
19
|
+
expect(
|
20
|
+
NRSER.dedent <<-BLOCK
|
21
|
+
def f x
|
22
|
+
x * x
|
23
|
+
end
|
24
|
+
BLOCK
|
25
|
+
).to eq <<-BLOCK
|
26
|
+
def f x
|
27
|
+
x * x
|
28
|
+
end
|
29
|
+
BLOCK
|
30
|
+
end
|
31
|
+
|
32
|
+
it "handles edge cases" do
|
33
|
+
expect( NRSER.dedent '' ).to eq ''
|
34
|
+
expect( NRSER.dedent 'blah' ).to eq 'blah'
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
using NRSER
|
4
|
+
|
5
|
+
describe "NRSER.format_exception" do
|
6
|
+
let(:error) {
|
7
|
+
begin
|
8
|
+
raise StandardError.new "blah blah blah"
|
9
|
+
rescue Exception => e
|
10
|
+
e
|
11
|
+
end
|
12
|
+
}
|
13
|
+
|
14
|
+
it "formats a raised error" do
|
15
|
+
str = NRSER.format_exception error
|
16
|
+
expect( str ).to start_with "blah blah blah (StandardError):"
|
17
|
+
expect( str.lines.drop(1) ).to all( start_with ' ' )
|
18
|
+
end
|
19
|
+
|
20
|
+
it "refines Exception" do
|
21
|
+
str = error.format
|
22
|
+
expect( str ).to start_with "blah blah blah (StandardError):"
|
23
|
+
expect( str.lines.drop(1) ).to all( start_with ' ' )
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
using NRSER
|
4
|
+
|
5
|
+
describe "NRSER.indent" do
|
6
|
+
it "indents a block" do
|
7
|
+
expect(
|
8
|
+
NRSER.indent <<-BLOCK
|
9
|
+
def f x
|
10
|
+
x * x
|
11
|
+
end
|
12
|
+
BLOCK
|
13
|
+
).to eq <<-BLOCK
|
14
|
+
def f x
|
15
|
+
x * x
|
16
|
+
end
|
17
|
+
BLOCK
|
18
|
+
end
|
19
|
+
|
20
|
+
it "indents a single line string" do
|
21
|
+
expect( NRSER.indent "blah" ).to eq " blah"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "refines String to add indent" do
|
25
|
+
expect(
|
26
|
+
<<-BLOCK.indent
|
27
|
+
def f x
|
28
|
+
x * x
|
29
|
+
end
|
30
|
+
BLOCK
|
31
|
+
).to eq <<-BLOCK
|
32
|
+
def f x
|
33
|
+
x * x
|
34
|
+
end
|
35
|
+
BLOCK
|
36
|
+
end
|
37
|
+
end # indent
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
using NRSER
|
4
|
+
|
5
|
+
describe 'NRSER.template' do
|
6
|
+
it "refines tpl in object like it was defined in Kernel" do
|
7
|
+
expect(
|
8
|
+
tpl binding, <<-BLOCK
|
9
|
+
hey
|
10
|
+
BLOCK
|
11
|
+
).to eq <<-BLOCK.dedent
|
12
|
+
hey
|
13
|
+
BLOCK
|
14
|
+
end
|
15
|
+
|
16
|
+
it "processes a simple template" do
|
17
|
+
x = 1
|
18
|
+
|
19
|
+
expect(
|
20
|
+
tpl binding, <<-BLOCK
|
21
|
+
x is <%= x %>
|
22
|
+
BLOCK
|
23
|
+
).to eq <<-BLOCK.dedent
|
24
|
+
x is 1
|
25
|
+
BLOCK
|
26
|
+
end
|
27
|
+
|
28
|
+
it "handles edge cases" do
|
29
|
+
expect( tpl binding, '' ).to eq ''
|
30
|
+
expect( tpl binding, 'blah' ).to eq 'blah'
|
31
|
+
end
|
32
|
+
end # template
|
data/spec/nrser_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nrser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nrser
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-16 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: 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
|
+
description:
|
56
|
+
email:
|
57
|
+
- neil@neilsouza.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/nrser.rb
|
70
|
+
- lib/nrser/version.rb
|
71
|
+
- nrser.gemspec
|
72
|
+
- spec/nrser/common_prefix_spec.rb
|
73
|
+
- spec/nrser/dedent_spec.rb
|
74
|
+
- spec/nrser/format_exception_spec.rb
|
75
|
+
- spec/nrser/indent_spec.rb
|
76
|
+
- spec/nrser/template_spec.rb
|
77
|
+
- spec/nrser_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- tmp/.gitkeep
|
80
|
+
homepage: https://github.com/nrser/nrser-ruby
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: basic ruby utils i use in a lot of stuff.
|
104
|
+
test_files:
|
105
|
+
- spec/nrser/common_prefix_spec.rb
|
106
|
+
- spec/nrser/dedent_spec.rb
|
107
|
+
- spec/nrser/format_exception_spec.rb
|
108
|
+
- spec/nrser/indent_spec.rb
|
109
|
+
- spec/nrser/template_spec.rb
|
110
|
+
- spec/nrser_spec.rb
|
111
|
+
- spec/spec_helper.rb
|