buff-extensions 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,4 +6,6 @@ end
6
6
 
7
7
  require_relative 'extensions/boolean'
8
8
  require_relative 'extensions/hash'
9
+ require_relative 'extensions/kernel'
9
10
  require_relative 'extensions/object'
11
+ require_relative 'extensions/string'
@@ -0,0 +1 @@
1
+ require_relative 'kernel/reporting'
@@ -0,0 +1,119 @@
1
+ require 'rbconfig'
2
+ require 'tempfile'
3
+
4
+ module Buff
5
+ module Extensions::Kernel
6
+ # Borrowd and modified from
7
+ # {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/kernel/reporting.rb}
8
+ module Reporting
9
+ # Sets $VERBOSE to nil for the duration of the block and back to its original
10
+ # value afterwards.
11
+ #
12
+ # silence_warnings do
13
+ # value = noisy_call # no warning voiced
14
+ # end
15
+ #
16
+ # noisy_call # warning voiced
17
+ def silence_warnings
18
+ with_warnings(nil) { yield }
19
+ end
20
+
21
+ # Sets $VERBOSE to +true+ for the duration of the block and back to its
22
+ # original value afterwards.
23
+ def enable_warnings
24
+ with_warnings(true) { yield }
25
+ end
26
+
27
+ # Sets $VERBOSE for the duration of the block and back to its original
28
+ # value afterwards.
29
+ def with_warnings(flag)
30
+ old_verbose, $VERBOSE = $VERBOSE, flag
31
+ yield
32
+ ensure
33
+ $VERBOSE = old_verbose
34
+ end
35
+
36
+ # For compatibility
37
+ def silence_stderr #:nodoc:
38
+ silence_stream(STDERR) { yield }
39
+ end
40
+
41
+ # Silences any stream for the duration of the block.
42
+ #
43
+ # silence_stream(STDOUT) do
44
+ # puts 'This will never be seen'
45
+ # end
46
+ #
47
+ # puts 'But this will'
48
+ def silence_stream(stream)
49
+ old_stream = stream.dup
50
+ stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
51
+ stream.sync = true
52
+ yield
53
+ ensure
54
+ stream.reopen(old_stream)
55
+ end
56
+
57
+ # Blocks and ignores any exception passed as argument if raised within the block.
58
+ #
59
+ # suppress(ZeroDivisionError) do
60
+ # 1/0
61
+ # puts 'This code is NOT reached'
62
+ # end
63
+ #
64
+ # puts 'This code gets executed and nothing related to ZeroDivisionError was seen'
65
+ def suppress(*exception_classes)
66
+ yield
67
+ rescue Exception => e
68
+ raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
69
+ end
70
+
71
+ # Captures the given stream and returns it:
72
+ #
73
+ # stream = capture(:stdout) { puts 'notice' }
74
+ # stream # => "notice\n"
75
+ #
76
+ # stream = capture(:stderr) { warn 'error' }
77
+ # stream # => "error\n"
78
+ #
79
+ # even for subprocesses:
80
+ #
81
+ # stream = capture(:stdout) { system('echo notice') }
82
+ # stream # => "notice\n"
83
+ #
84
+ # stream = capture(:stderr) { system('echo error 1>&2') }
85
+ # stream # => "error\n"
86
+ def capture(stream)
87
+ stream = stream.to_s
88
+ captured_stream = Tempfile.new(stream)
89
+ stream_io = eval("$#{stream}")
90
+ origin_stream = stream_io.dup
91
+ stream_io.reopen(captured_stream)
92
+
93
+ yield
94
+
95
+ stream_io.rewind
96
+ return captured_stream.read
97
+ ensure
98
+ captured_stream.unlink
99
+ stream_io.reopen(origin_stream)
100
+ end
101
+ alias :silence :capture
102
+
103
+ # Silences both STDOUT and STDERR, even for subprocesses.
104
+ #
105
+ # quietly { system 'bundle install' }
106
+ def quietly
107
+ silence_stream(STDOUT) do
108
+ silence_stream(STDERR) do
109
+ yield
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ module Kernel
118
+ include Buff::Extensions::Kernel::Reporting
119
+ end
@@ -0,0 +1 @@
1
+ require_relative 'string/inflections'
@@ -0,0 +1,20 @@
1
+ module Buff
2
+ module Extensions::String
3
+ module Inflections
4
+ def underscore
5
+ word = self.dup
6
+ word.gsub!('::', '/')
7
+ word.gsub!(/(?:([A-Za-z\d])|^)((?=a)b)(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
8
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
9
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
10
+ word.tr!("-", "_")
11
+ word.downcase!
12
+ word
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class String
19
+ include Buff::Extensions::String::Inflections
20
+ end
@@ -1,5 +1,5 @@
1
1
  module Buff
2
2
  module Extensions
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buff-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -196,8 +196,12 @@ files:
196
196
  - lib/buff/extensions/hash/key_transforms.rb
197
197
  - lib/buff/extensions/hash/reverse_merge.rb
198
198
  - lib/buff/extensions/hash/slice.rb
199
+ - lib/buff/extensions/kernel.rb
200
+ - lib/buff/extensions/kernel/reporting.rb
199
201
  - lib/buff/extensions/object.rb
200
202
  - lib/buff/extensions/object/blank.rb
203
+ - lib/buff/extensions/string.rb
204
+ - lib/buff/extensions/string/inflections.rb
201
205
  - lib/buff/extensions/version.rb
202
206
  - spec/buff/extensions/hash/dotted_paths_spec.rb
203
207
  - spec/spec_helper.rb
@@ -222,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
226
  version: '0'
223
227
  segments:
224
228
  - 0
225
- hash: 802397933752407242
229
+ hash: 1537856956160117241
226
230
  requirements: []
227
231
  rubyforge_project:
228
232
  rubygems_version: 1.8.23