nuggets 1.3.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89f2d47d5e501cd0800f31bd572cdd1c20c6e98c
4
- data.tar.gz: c69f1ac6fc489329f61e96fb419c63f5180bf5c4
3
+ metadata.gz: b8bc4223cf7e497022aa29613c8fddce9a757b69
4
+ data.tar.gz: 3f1f0159203157d4fbcded7cf3dc5aeb25972df2
5
5
  SHA512:
6
- metadata.gz: 24d1c8c645c8d9ef912e30a82dea599744d986ea5254947cfea0598457f9943328f239cb59264fb43dfab9148f2627a40ca7648cdd9b7a756c1aa66acac32233
7
- data.tar.gz: c503ac3652caedcf02c85353a4098ddfaa26a933af2fd5ac73ac3e53be5c71022cbf2498bd48b3654247db36c05b710b5dfae762343347dffbfc6248fd5be9be
6
+ metadata.gz: aedaaac17d00e186d8219ba67495fc1b5f73033251cff451b8f38e5e454cee49a4eac56922c2ae2b34c2ff3d87ba1bf922285a512f879e197552ea7219084733
7
+ data.tar.gz: 99ed4373f5c96cd6a0f42a0ee55e4ee5a8d99f166245fb2ff6f54330dfbeac503f021868bda9569bf8149b05aea31cb3ec7c6855c30a5509fbbabeff9a03678d
data/ChangeLog CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  = Revision history for nuggets
4
4
 
5
+ == 1.4.0 [2015-10-02]
6
+
7
+ * Added File.open_file.
8
+ * Added Array#extract_options.
9
+
5
10
  == 1.3.0 [2015-07-07]
6
11
 
7
12
  * Added <tt>Hash.{identity,array}</tt>.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to nuggets version 1.3.0.
5
+ This documentation refers to nuggets version 1.4.0.
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -0,0 +1,5 @@
1
+ require 'nuggets/array/extract_options_mixin'
2
+
3
+ class Array
4
+ include Nuggets::Array::ExtractOptionsMixin
5
+ end
@@ -0,0 +1,49 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2015 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class Array
29
+ module ExtractOptionsMixin
30
+
31
+ # call-seq:
32
+ # array.extract_options([default]) -> aHash or +default+
33
+ #
34
+ # Returns options hash from _array_ or +default+.
35
+ def extract_options(default = {})
36
+ last.is_a?(::Hash) ? last : default
37
+ end
38
+
39
+ # call-seq:
40
+ # array.extract_options!([default]) -> aHash or +default+
41
+ #
42
+ # Extracts options hash from _array_ or returns +default+.
43
+ def extract_options!(default = {})
44
+ last.is_a?(::Hash) ? pop : default
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/file/open_file_mixin'
2
+
3
+ class File
4
+ extend Nuggets::File::OpenFileMixin
5
+ end
@@ -0,0 +1,71 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2015 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class File
29
+ module OpenFileMixin
30
+
31
+ # call-seq:
32
+ # File.open_file(filename[, options[, mode]]) -> anIO
33
+ # File.open_file(filename[, options[, mode]]) { |io| ... } => anObject
34
+ #
35
+ # Supported options are +mode+ (defaults to +r+) and +encoding+ (defaults
36
+ # to the default external encoding).
37
+ #
38
+ # If +filename+ is +-+, sets +io+ to +STDOUT+ when in write mode or +STDIN+
39
+ # otherwise. Puts +io+ into binary mode if requested. Sets +io+'s encoding.
40
+ #
41
+ # If +filename+ ends with +.gz+ or +.gzip+, uses Zlib for reading or writing
42
+ # the file.
43
+ #
44
+ # Otherwise defers to ::open.
45
+ #
46
+ # Yields +io+ to the given block or returns it when no block given.
47
+ def open_file(filename, options = {}, mode = 'r', &block)
48
+ mode = options.fetch(:mode, mode); writing = mode =~ /w/
49
+
50
+ encoding = options.fetch(:encoding, ::Encoding.default_external)
51
+
52
+ case filename
53
+ when '-'
54
+ io = writing ? $stdout : $stdin
55
+ io.binmode if mode =~ /b/
56
+ io.set_encoding(encoding)
57
+
58
+ block ? block[io] : io
59
+ when /\.gz(?:ip)?\z/i
60
+ require 'zlib'
61
+
62
+ klass = writing ? ::Zlib::GzipWriter : ::Zlib::GzipReader
63
+ klass.open(filename, encoding: encoding, &block)
64
+ else
65
+ open(filename, mode, encoding: encoding, &block)
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -43,14 +43,15 @@ class Numeric
43
43
  #
44
44
  # Returns the sign of _num_.
45
45
  def signum
46
- positive? ? 1 : negative? ? -1 : 0
46
+ # http://weblog.jamisbuck.org/2015/8/5/reducing-a-number-to-its-sign.html
47
+ self <=> 0
47
48
  end
48
49
 
49
50
  alias_method :sign, :signum
50
51
  alias_method :sgn, :signum
51
52
 
52
53
  def signum_s(positive = '+', negative = '-', zero = positive)
53
- positive? ? positive : negative? ? negative : zero
54
+ [zero, positive, negative][signum]
54
55
  end
55
56
 
56
57
  alias_method :sign_s, :signum_s
@@ -3,7 +3,7 @@ module Nuggets
3
3
  module Version
4
4
 
5
5
  MAJOR = 1
6
- MINOR = 3
6
+ MINOR = 4
7
7
  TINY = 0
8
8
 
9
9
  class << self
@@ -0,0 +1,47 @@
1
+ require 'nuggets/array/extract_options'
2
+
3
+ describe_extended Array, Nuggets::Array::ExtractOptionsMixin do
4
+
5
+ example { expect([].extract_options).to eq({}) }
6
+
7
+ example { expect([].extract_options(42)).to eq(42) }
8
+
9
+ example { expect([].extract_options(nil)).to eq(nil) }
10
+
11
+ example { expect([42].extract_options).to eq({}) }
12
+
13
+ example { expect([foo: 42].extract_options).to eq(foo: 42) }
14
+
15
+ example { expect([23, foo: 42].extract_options).to eq(foo: 42) }
16
+
17
+ example { expect([{ foo: 42 }, 23].extract_options).to eq({}) }
18
+
19
+ example { expect([23, 42, {}].extract_options).to eq({}) }
20
+
21
+ example { expect([23, 42, {}].extract_options(nil)).to eq({}) }
22
+
23
+ example { expect([23, 42].extract_options).to eq({}) }
24
+
25
+ example { expect([23, 42].extract_options(nil)).to eq(nil) }
26
+
27
+ example {
28
+ a = [23, foo: 42]
29
+
30
+ expect(a.extract_options).to eq(foo: 42)
31
+ expect(a).to eq([23, foo: 42])
32
+
33
+ expect(a.extract_options!).to eq(foo: 42)
34
+ expect(a).to eq([23])
35
+ }
36
+
37
+ example {
38
+ a = [{ foo: 42 }, 23]
39
+
40
+ expect(a.extract_options).to eq({})
41
+ expect(a).to eq([{ foo: 42 }, 23])
42
+
43
+ expect(a.extract_options!).to eq({})
44
+ expect(a).to eq([{ foo: 42 }, 23])
45
+ }
46
+
47
+ end
@@ -6,22 +6,22 @@ describe Numeric, 'signum' do
6
6
  123 => 1,
7
7
  -123 => -1,
8
8
  0 => 0,
9
+ 1 => 1,
10
+ -1 => -1,
9
11
  0.001 => 1,
10
12
  1.23 => 1,
11
13
  -12.3 => -1
12
- }.each { |n, s|
13
- example { n.sign.should == s }
14
- }
14
+ }.each { |n, s| example { n.sign.should == s } }
15
15
 
16
16
  {
17
17
  123 => '+',
18
18
  -123 => '-',
19
19
  0 => '+',
20
+ 1 => '+',
21
+ -1 => '-',
20
22
  0.001 => '+',
21
23
  1.23 => '+',
22
24
  -12.3 => '-'
23
- }.each { |n, s|
24
- example { n.sign_s.should == s }
25
- }
25
+ }.each { |n, s| example { n.sign_s.should == s } }
26
26
 
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-07 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -47,7 +47,7 @@ dependencies:
47
47
  version: '0.8'
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
- version: 0.8.1
50
+ version: 0.8.3
51
51
  type: :development
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
@@ -57,7 +57,7 @@ dependencies:
57
57
  version: '0.8'
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
- version: 0.8.1
60
+ version: 0.8.3
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rake
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -110,6 +110,8 @@ files:
110
110
  - lib/nuggets/array/combination.rb
111
111
  - lib/nuggets/array/correlation.rb
112
112
  - lib/nuggets/array/correlation_mixin.rb
113
+ - lib/nuggets/array/extract_options.rb
114
+ - lib/nuggets/array/extract_options_mixin.rb
113
115
  - lib/nuggets/array/flatten_once.rb
114
116
  - lib/nuggets/array/flush.rb
115
117
  - lib/nuggets/array/flush_mixin.rb
@@ -162,6 +164,8 @@ files:
162
164
  - lib/nuggets/env/user_home_mixin.rb
163
165
  - lib/nuggets/file/ext.rb
164
166
  - lib/nuggets/file/ext_mixin.rb
167
+ - lib/nuggets/file/open_file.rb
168
+ - lib/nuggets/file/open_file_mixin.rb
165
169
  - lib/nuggets/file/replace.rb
166
170
  - lib/nuggets/file/replace_mixin.rb
167
171
  - lib/nuggets/file/sub.rb
@@ -273,6 +277,7 @@ files:
273
277
  - spec/nuggets/array/boost_spec.rb
274
278
  - spec/nuggets/array/combination_spec.rb
275
279
  - spec/nuggets/array/correlation_spec.rb
280
+ - spec/nuggets/array/extract_options_spec.rb
276
281
  - spec/nuggets/array/flatten_once_spec.rb
277
282
  - spec/nuggets/array/flush_spec.rb
278
283
  - spec/nuggets/array/format_spec.rb
@@ -349,13 +354,14 @@ licenses:
349
354
  metadata: {}
350
355
  post_install_message: |2+
351
356
 
352
- nuggets-1.3.0 [2015-07-07]:
357
+ nuggets-1.4.0 [2015-10-02]:
353
358
 
354
- * Added <tt>Hash.{identity,array}</tt>.
359
+ * Added File.open_file.
360
+ * Added Array#extract_options.
355
361
 
356
362
  rdoc_options:
357
363
  - "--title"
358
- - nuggets Application documentation (v1.3.0)
364
+ - nuggets Application documentation (v1.4.0)
359
365
  - "--charset"
360
366
  - UTF-8
361
367
  - "--line-numbers"