nrser 0.0.10 → 0.0.11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 700ca721f5a9c6e1b301a42b06cca6ee01aab576
4
- data.tar.gz: ce5fb2e55d3ad6d279858e3ccbdc252d29097bdf
3
+ metadata.gz: 4fb0f908c7143a3d04705284022e9bf269ae148c
4
+ data.tar.gz: e70e72f42055266a81019e6faae143b88dff6cbd
5
5
  SHA512:
6
- metadata.gz: 6da44b20707d9df1ead93f5b58c2c27ddbdbbeda37a60235a81e752ed8d40b1c3130705596ab0e141a01b534db383ed4f3f597f751b7f3a7b2c00048137fcad6
7
- data.tar.gz: 5404fe037bdc814a4ad7e171b8b5070c587173b6906be09e8ef5630d09d07820bffef42ff31ca2ce16c74336e6f5e3fa64e72ab3032898446f95b7e6ace84549
6
+ metadata.gz: 194b3069a1468b6d763cfbeb0b47d4a0e12857483fb2b311a9afe72ce51406877ce76e67ba57ad1ef54977148b602d32dd9f2225d021713097d2b1b49133e7c6
7
+ data.tar.gz: a92470823133fa31f9c154bee199828f754f7be31350a00e748331a8c5ba4cb62a8b85d7907306a790ee6c82fcbe02be4475ffdb7c523a40ff201658f00dbd7b
@@ -0,0 +1,40 @@
1
+ module NRSER
2
+ module KernelRefinements
3
+ def tpl *args
4
+ NRSER::template *args
5
+ end
6
+ end
7
+
8
+ refine Object do
9
+ include KernelRefinements
10
+
11
+ def pipe
12
+ yield self
13
+ end
14
+
15
+ end
16
+
17
+ refine String do
18
+ def unblock
19
+ NRSER.unblock self
20
+ end
21
+
22
+ def dedent
23
+ NRSER.dedent self
24
+ end
25
+
26
+ def indent *args
27
+ NRSER.indent self, *args
28
+ end
29
+
30
+ def truncate *args
31
+ NRSER.truncate self, *args
32
+ end
33
+ end # refine String
34
+
35
+ refine Exception do
36
+ def format
37
+ NRSER.format_exception self
38
+ end
39
+ end
40
+ end # NRSER
data/lib/nrser/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module NRSER
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
data/lib/nrser.rb CHANGED
@@ -1,47 +1,6 @@
1
1
  require "nrser/version"
2
2
 
3
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 *args
29
- NRSER.indent self, *args
30
- end
31
-
32
- def truncate *args
33
- NRSER.truncate self, *args
34
- end
35
- end # refine String
36
-
37
- refine Exception do
38
- def format
39
- NRSER.format_exception self
40
- end
41
- end
42
-
43
- using NRSER
44
-
45
4
  def self.unblock str
46
5
  parts = str.split(/\n\s+/)
47
6
  if m = parts[0].match(/^\s+/)
@@ -97,6 +56,11 @@ module NRSER
97
56
  filter_repeated_blank_lines ERB.new(dedent(str)).result(bnd)
98
57
  end # template
99
58
 
59
+ # alias
60
+ def self.tpl bnd, str
61
+ template bnd, str
62
+ end # tpl
63
+
100
64
  def self.format_exception e
101
65
  "#{ e.message } (#{ e.class }):\n #{ e.backtrace.join("\n ") }"
102
66
  end
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- using NRSER
4
-
5
3
  describe "NRSER.format_exception" do
6
4
  let(:error) {
7
5
  begin
@@ -17,9 +15,4 @@ describe "NRSER.format_exception" do
17
15
  expect( str.lines.drop(1) ).to all( start_with ' ' )
18
16
  end
19
17
 
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
18
  end
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- using NRSER
4
-
5
3
  describe "NRSER.indent" do
6
4
  it "indents a block" do
7
5
  expect(
@@ -21,31 +19,4 @@ BLOCK
21
19
  expect( NRSER.indent "blah" ).to eq " blah"
22
20
  end
23
21
 
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
-
38
- it "accepts arguments to the refinement of String" do
39
- expect(
40
- <<-BLOCK.indent(4)
41
- def f x
42
- x * x
43
- end
44
- BLOCK
45
- ).to eq <<-BLOCK
46
- def f x
47
- x * x
48
- end
49
- BLOCK
50
- end
51
22
  end # indent
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'nrser/refinements'
3
+
4
+ using NRSER
5
+
6
+ describe "NRSER.format_exception" do
7
+ let(:error) {
8
+ begin
9
+ raise StandardError.new "blah blah blah"
10
+ rescue Exception => e
11
+ e
12
+ end
13
+ }
14
+
15
+ it "refines Exception" do
16
+ str = error.format
17
+ expect( str ).to start_with "blah blah blah (StandardError):"
18
+ expect( str.lines.drop(1) ).to all( start_with ' ' )
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'nrser/refinements'
3
+
4
+ using NRSER
5
+
6
+ describe "NRSER.indent" do
7
+ it "refines String to add indent" do
8
+ expect(
9
+ <<-BLOCK.indent
10
+ def f x
11
+ x * x
12
+ end
13
+ BLOCK
14
+ ).to eq <<-BLOCK
15
+ def f x
16
+ x * x
17
+ end
18
+ BLOCK
19
+ end
20
+
21
+ it "accepts arguments to the refinement of String" do
22
+ expect(
23
+ <<-BLOCK.indent(4)
24
+ def f x
25
+ x * x
26
+ end
27
+ BLOCK
28
+ ).to eq <<-BLOCK
29
+ def f x
30
+ x * x
31
+ end
32
+ BLOCK
33
+ end
34
+ end # indent
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'nrser/refinements'
3
+
4
+ using NRSER
5
+
6
+ describe 'NRSER.template' do
7
+ it "refines tpl in object like it was defined in Kernel" do
8
+ expect(
9
+ tpl binding, <<-BLOCK
10
+ hey
11
+ BLOCK
12
+ ).to eq <<-BLOCK.dedent
13
+ hey
14
+ BLOCK
15
+ end
16
+
17
+ it "processes a simple template" do
18
+ x = 1
19
+
20
+ expect(
21
+ tpl binding, <<-BLOCK
22
+ x is <%= x %>
23
+ BLOCK
24
+ ).to eq <<-BLOCK.dedent
25
+ x is 1
26
+ BLOCK
27
+ end
28
+
29
+ it "handles edge cases" do
30
+ expect( tpl binding, '' ).to eq ''
31
+ expect( tpl binding, 'blah' ).to eq 'blah'
32
+ end
33
+ end # template
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'nrser/refinements'
3
+
4
+ using NRSER
5
+
6
+ describe "NRSER.truncate" do
7
+ it "refines String" do
8
+ expect( "blah blah blah blah".truncate 10 ).to eq "blah bl..."
9
+ end
10
+ end # indent
@@ -1,32 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
- using NRSER
4
-
5
3
  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
4
  it "processes a simple template" do
17
5
  x = 1
18
6
 
19
7
  expect(
20
- tpl binding, <<-BLOCK
8
+ NRSER.template binding, <<-BLOCK
21
9
  x is <%= x %>
22
10
  BLOCK
23
- ).to eq <<-BLOCK.dedent
11
+ ).to eq NRSER.dedent <<-BLOCK
24
12
  x is 1
25
13
  BLOCK
26
14
  end
27
15
 
28
16
  it "handles edge cases" do
29
- expect( tpl binding, '' ).to eq ''
30
- expect( tpl binding, 'blah' ).to eq 'blah'
17
+ expect( NRSER.template binding, '' ).to eq ''
18
+ expect( NRSER.template binding, 'blah' ).to eq 'blah'
31
19
  end
32
20
  end # template
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- using NRSER
4
-
5
3
  describe "NRSER.truncate" do
6
4
  it "truncates a string that's longer than length" do
7
5
  expect( NRSER.truncate "blah blah blah blah", 10 ).to eq "blah bl..."
@@ -10,8 +8,4 @@ describe "NRSER.truncate" do
10
8
  it "leaves a string alone that's shorter or equal to lenght" do
11
9
  expect( NRSER.truncate "blah", 10 ).to eq "blah"
12
10
  end
13
-
14
- it "refines String" do
15
- expect( "blah blah blah blah".truncate 10 ).to eq "blah bl..."
16
- end
17
11
  end # indent
data/spec/nrser_spec.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- using NRSER
4
-
5
3
  describe NRSER do
6
4
  it 'has a version number' do
7
5
  expect(NRSER::VERSION).not_to be nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nrser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-13 00:00:00.000000000 Z
11
+ date: 2015-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,16 +67,17 @@ files:
67
67
  - README.md
68
68
  - Rakefile
69
69
  - lib/nrser.rb
70
- - lib/nrser/exec.rb
70
+ - lib/nrser/refinements.rb
71
71
  - lib/nrser/version.rb
72
72
  - nrser.gemspec
73
73
  - spec/nrser/common_prefix_spec.rb
74
74
  - spec/nrser/dedent_spec.rb
75
- - spec/nrser/exec/result_spec.rb
76
- - spec/nrser/exec/run_spec.rb
77
- - spec/nrser/exec/sub_spec.rb
78
75
  - spec/nrser/format_exception_spec.rb
79
76
  - spec/nrser/indent_spec.rb
77
+ - spec/nrser/refinements/format_exception_spec.rb
78
+ - spec/nrser/refinements/indent_spec.rb
79
+ - spec/nrser/refinements/template_spec.rb
80
+ - spec/nrser/refinements/truncate_spec.rb
80
81
  - spec/nrser/template_spec.rb
81
82
  - spec/nrser/truncate_spec.rb
82
83
  - spec/nrser_spec.rb
@@ -109,11 +110,12 @@ summary: basic ruby utils i use in a lot of stuff.
109
110
  test_files:
110
111
  - spec/nrser/common_prefix_spec.rb
111
112
  - spec/nrser/dedent_spec.rb
112
- - spec/nrser/exec/result_spec.rb
113
- - spec/nrser/exec/run_spec.rb
114
- - spec/nrser/exec/sub_spec.rb
115
113
  - spec/nrser/format_exception_spec.rb
116
114
  - spec/nrser/indent_spec.rb
115
+ - spec/nrser/refinements/format_exception_spec.rb
116
+ - spec/nrser/refinements/indent_spec.rb
117
+ - spec/nrser/refinements/template_spec.rb
118
+ - spec/nrser/refinements/truncate_spec.rb
117
119
  - spec/nrser/template_spec.rb
118
120
  - spec/nrser/truncate_spec.rb
119
121
  - spec/nrser_spec.rb
data/lib/nrser/exec.rb DELETED
@@ -1,90 +0,0 @@
1
- require 'shellwords'
2
-
3
- require 'nrser'
4
-
5
- using NRSER
6
-
7
- module NRSER::Exec
8
- class Result
9
- attr_reader :cmd, :exitstatus, :output
10
-
11
- def initialize cmd, exitstatus, output
12
- @cmd = cmd
13
- @exitstatus = exitstatus
14
- @output = output
15
- end
16
-
17
- def raise_error
18
- raise SystemCallError.new <<-BLOCK.unblock, @exitstatus
19
- cmd `#{ @cmd }` failed with status #{ @exitstatus }
20
- and output #{ @output.inspect }
21
- BLOCK
22
- end
23
-
24
- def check_error
25
- raise_error unless success?
26
- end
27
-
28
- def success?
29
- @exitstatus == 0
30
- end
31
-
32
- def failure?
33
- ! success?
34
- end
35
- end
36
-
37
- # substitute stuff into a shell command after escaping with
38
- # `Shellwords.escape`.
39
- #
40
- # arguments after the first may be multiple values that will
41
- # be treated like a positional list for substitution, or a single
42
- # hash that will be treated like a key substitution.
43
- #
44
- # any substitution value that is an Array will be treated like a list of
45
- # path segments and joined with `File.join`.
46
- def self.sub command, subs
47
- quoted = case subs
48
- when Hash
49
- Hash[
50
- subs.map do |key, sub|
51
- sub = File.join(*sub) if sub.is_a? Array
52
- # shellwords in 1.9.3 can't handle symbols
53
- sub = sub.to_s if sub.is_a? Symbol
54
- [key, Shellwords.escape(sub)]
55
- end
56
- ]
57
- when Array
58
- subs.map do |sub|
59
- sub = File.join(*sub) if sub.is_a? Array
60
- # shellwords in 1.9.3 can't handle symbols
61
- sub = sub.to_s if sub.is_a? Symbol
62
- Shellwords.escape sub
63
- end
64
- else
65
- raise "should be Hash or Array: #{ subs.inspect }"
66
- end
67
- command % quoted
68
- end # ::sub
69
-
70
- def self.run cmd, subs = nil
71
- cmd = sub(cmd, subs) unless subs.nil?
72
- output = `#{ cmd } 2>&1`
73
- exitstatus = $?.exitstatus
74
-
75
- if exitstatus == 0
76
- return output
77
- else
78
- raise SystemCallError.new <<-BLOCK.unblock, exitstatus
79
- hey - cmd `#{ cmd }` failed with status #{ $?.exitstatus }
80
- and output #{ output.inspect }
81
- BLOCK
82
- end
83
- end # ::run
84
-
85
- def self.result cmd, subs = nil
86
- cmd = sub(cmd, subs) unless subs.nil?
87
- output = `#{ cmd } 2>&1`
88
- Result.new cmd, $?.exitstatus, output
89
- end # ::result
90
- end
@@ -1,77 +0,0 @@
1
- require 'shellwords'
2
-
3
- require 'spec_helper'
4
-
5
- require 'nrser/exec'
6
-
7
- describe "NRSER::Exec.result" do
8
- context "successful command" do
9
- msg = "hey!"
10
-
11
- let(:result) {
12
- NRSER::Exec.result "echo %{msg}", msg: msg
13
- }
14
-
15
- it "should have exitstatus 0" do
16
- expect( result.exitstatus ).to eq 0
17
- end
18
-
19
- it "should return true for #success?" do
20
- expect( result.success? ).to be true
21
- end
22
-
23
- it "should return false for #failure?" do
24
- expect( result.failure? ).to be false
25
- end
26
-
27
- it "should not raise an error on #check_error" do
28
- expect{ result.check_error }.not_to raise_error
29
- end
30
-
31
- it "should raise Errno::NOERROR on #raise_error" do
32
- expect{ result.raise_error }.to raise_error Errno::NOERROR
33
- end
34
-
35
- it "should have output 'hey!\\n'" do
36
- expect( result.output ).to eq "hey!\n"
37
- end
38
-
39
- it %{should have cmd "echo #{ Shellwords.escape(msg) }"} do
40
- expect( result.cmd ).to eq "echo #{ Shellwords.escape(msg) }"
41
- end
42
- end
43
-
44
- context "failed command" do
45
- let(:result) {
46
- NRSER::Exec.result "false"
47
- }
48
-
49
- it "should not have exitstatus 0" do
50
- expect( result.exitstatus ).not_to eq 0
51
- end
52
-
53
- it "should return false for #success?" do
54
- expect( result.success? ).to be false
55
- end
56
-
57
- it "should return true for #failure?" do
58
- expect( result.failure? ).to be true
59
- end
60
-
61
- it "should raise an error on #check_error" do
62
- expect{ result.check_error }.to raise_error Errno::EPERM
63
- end
64
-
65
- it "should raise Errno::EPERM on #raise_error" do
66
- expect{ result.raise_error }.to raise_error Errno::EPERM
67
- end
68
-
69
- it "should have output ''" do
70
- expect( result.output ).to eq ""
71
- end
72
-
73
- it "should have cmd 'false'" do
74
- expect( result.cmd ).to eq "false"
75
- end
76
- end
77
- end # describe result
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'nrser/exec'
4
-
5
- describe "NRSER::Exec.run" do
6
-
7
- it "subs a string with spaces" do
8
- expect(
9
- NRSER::Exec.run "echo %{msg}", msg: "hey there"
10
- ).to eq "hey there\n"
11
- end
12
-
13
- it "subs a string with $" do
14
- expect(
15
- NRSER::Exec.run "echo %{msg}", msg: "$HOME"
16
- ).to eq "$HOME\n"
17
- end
18
-
19
- it "handles commands that error" do
20
- expect{ NRSER::Exec.run "false" }.to raise_error SystemCallError
21
- end
22
-
23
- end # describe run
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'nrser/exec'
4
-
5
- describe "NRSER::Exec.sub" do
6
-
7
- it "runs a string with spaces" do
8
- expect(
9
- NRSER::Exec.sub "echo %{msg}", msg: "hey there"
10
- ).to eq 'echo hey\ there'
11
- end
12
-
13
- it "runs a string with $" do
14
- expect(
15
- NRSER::Exec.sub "echo %{msg}", msg: "$HOME"
16
- ).to eq 'echo \$HOME'
17
- end
18
-
19
- end # describe sub