nuggets 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d101677594dc97521392e9f3e9633e83f408303f
4
- data.tar.gz: 5f5fcba403d1d7aee7b3a7bf7041389ee437e00d
3
+ metadata.gz: 2a1936682f72cb0230791ed908e37cacf43667cc
4
+ data.tar.gz: b5b3086d7f0edf596f76a6a2e4c2ddb8cd4a741e
5
5
  SHA512:
6
- metadata.gz: 1ea3f177acaa6e138a4fde477323284a5a3439339e206c6b01ce8f1d43aaa2458d15dfe92c45d800f268688bde54f3171ad90202ad4d20fe8911c78f9a6a81a8
7
- data.tar.gz: 402c097802cb1636c9ddb040080dfb20aa414477f47d724eab1c33b55be85b76e74d65f6a6e49f71355bbb615846da0218c527f031b76405810a62fd067465df
6
+ metadata.gz: 198004daec45f4efccfac5dddb720dc967c6662e5744e9932d91212585c5740e763d3568840baa5e18f98ea870a4b6a818b15714ff394e7c72d561d8303a6de7
7
+ data.tar.gz: fdbbe46e74251556320c14784d58231eebbdc819f884a8fc8090dd4f9fd4a6894fc931558a63546adabaed849451f5ccebff7cbdaf2ded5ecde5d637b039aee1
data/ChangeLog CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  = Revision history for nuggets
4
4
 
5
+ == 1.0.1 [2014-08-27]
6
+
7
+ * Added DATA.{consume,reposition,update}.
8
+ * Added Module#query_attr.
9
+ * Added Module#lazy_attr.
10
+ * Added Integer#to_roman.
11
+ * Added Numeric#signum_s.
12
+ * Added Array#interpolate.
13
+
5
14
  == 1.0.0 [2014-06-20]
6
15
 
7
16
  * First release under new name. Formerly known as ruby-nuggets.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to nuggets version 1.0.0.
5
+ This documentation refers to nuggets version 1.0.1.
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -45,7 +45,7 @@ expected or cause other libraries to misbehave. Use at your own risk!
45
45
 
46
46
  == LINKS
47
47
 
48
- Documentation:: https://blackwinter.github.io/nuggets/
48
+ Documentation:: https://blackwinter.github.com/nuggets
49
49
  Source code:: https://github.com/blackwinter/nuggets
50
50
  RubyGem:: https://rubygems.org/gems/nuggets
51
51
  Travis CI:: https://travis-ci.org/blackwinter/nuggets
@@ -0,0 +1,5 @@
1
+ require 'nuggets/array/interpolate_mixin'
2
+
3
+ class Array
4
+ include Nuggets::Array::InterpolateMixin
5
+ end
@@ -0,0 +1,54 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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 InterpolateMixin
30
+
31
+ # call-seq:
32
+ # array.interpolate([x0[, xN]]) => anArray
33
+ #
34
+ # Simple {linear interpolation}[http://en.wikipedia.org/wiki/Linear_interpolation]
35
+ # of missing values in _array_.
36
+ #
37
+ # Example:
38
+ #
39
+ # [1, nil, 3, nil, 5].interpolate #=> [1, 2, 3, 4, 5]
40
+ def interpolate(x0 = 0, xN = x0)
41
+ res, xX, interpolate = dup, [], lambda { |x|
42
+ m = (x - x0) / xX.size.succ.to_f unless xX.empty?
43
+ xX.each_with_index { |j, k| res[j] = m * k.succ + x0 }.clear
44
+ x
45
+ }
46
+
47
+ each_with_index { |x, i| x ? x0 = interpolate[x] : xX << i }
48
+ interpolate[xN] if xN
49
+ res
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ require 'nuggets/data/consume_mixin'
2
+
3
+ DATA.extend(Nuggets::Data::ConsumeMixin)
@@ -0,0 +1,43 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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
+ module Data
29
+ module ConsumeMixin
30
+
31
+ # call-seq:
32
+ # DATA.consume(*args) -> aString
33
+ #
34
+ # Read DATA and rewind.
35
+ def consume(*args)
36
+ pos, res = self.pos, read(*args)
37
+ seek(pos)
38
+ res
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ require 'nuggets/data/position_mixin'
2
+
3
+ DATA.extend(Nuggets::Data::PositionMixin)
@@ -0,0 +1,43 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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
+ module Data
29
+ module PositionMixin
30
+
31
+ attr_accessor :position
32
+
33
+ def reposition
34
+ seek(position)
35
+ end
36
+
37
+ def self.extended(base)
38
+ base.position = base.pos
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ require 'nuggets/data/update_mixin'
2
+
3
+ DATA.extend(Nuggets::Data::UpdateMixin)
@@ -0,0 +1,45 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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
+ module Data
29
+ module UpdateMixin
30
+
31
+ # call-seq:
32
+ # DATA.update(string) -> anInteger
33
+ #
34
+ # Update DATA with +string+.
35
+ def update(string)
36
+ File.open(self, 'r+') { |io|
37
+ io.seek(pos)
38
+ io.truncate(pos)
39
+ io.write(string)
40
+ }
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/integer/roman_mixin'
2
+
3
+ class Integer
4
+ include Nuggets::Integer::RomanMixin
5
+ end
@@ -0,0 +1,70 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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 Integer
29
+ module RomanMixin
30
+
31
+ NUMERAL = {
32
+ 1000 => 'M',
33
+ 500 => 'D',
34
+ 100 => 'C',
35
+ 50 => 'L',
36
+ 10 => 'X',
37
+ 5 => 'V',
38
+ 1 => 'I'
39
+ }
40
+
41
+ COMPACT = NUMERAL.values.each_cons(3).to_a.values_at(0, 2, 4).flat_map { |*a, b|
42
+ a.push(nil).each_cons(2).map { |x, y| [/#{y}#{b}{4}/, "#{b}#{x}"] }
43
+ }
44
+
45
+ # call-seq:
46
+ # to_roman(int) => aString
47
+ #
48
+ # Converts _positive_ integer +int+ to Roman numerals.
49
+ def self.to_roman(int, num = '')
50
+ NUMERAL.each { |key, val|
51
+ until int < key; int -= key; num << val; end
52
+ }
53
+
54
+ COMPACT.each { |key, val| num.gsub!(key, val) }
55
+
56
+ num
57
+ end
58
+
59
+ # call-seq:
60
+ # int.to_roman => aString
61
+ #
62
+ # Converts _int_ to Roman numerals.
63
+ def to_roman
64
+ self == 0 ? 'N' : self < 0 ?
65
+ RomanMixin.to_roman(-self, '-') : RomanMixin.to_roman(self)
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -3,7 +3,7 @@
3
3
  # #
4
4
  # nuggets -- Extending Ruby #
5
5
  # #
6
- # Copyright (C) 2007-2011 Jens Wille #
6
+ # Copyright (C) 2007-2014 Jens Wille #
7
7
  # #
8
8
  # Authors: #
9
9
  # Jens Wille <jens.wille@gmail.com> #
@@ -75,44 +75,84 @@ module Nuggets
75
75
  # IO.interact({ f => stdin }, { stdout => STDOUT, stderr => STDERR })
76
76
  # }
77
77
  # }
78
- def interact(input, output, timeout = nil, maxlen = 2 ** 16)
79
- readers, writers = {}, {}
78
+ def interact(input, output, timeout = nil, maxlen = nil)
79
+ Interaction.new(input, output, timeout, maxlen).interact
80
+ end
81
+
82
+ class Interaction
83
+
84
+ DEFAULT_MAXLEN = 2 ** 16
85
+
86
+ def initialize(input, output, timeout = nil, maxlen = nil)
87
+ @readers, @writers = {}, {}
88
+
89
+ output.each { |key, val| @readers[key] = initialize_reader(val) }
90
+ input.each { |key, val| @writers[val] = [initialize_writer(key), ''] }
91
+
92
+ @timeout, @maxlen = timeout, maxlen || DEFAULT_MAXLEN
93
+ end
94
+
95
+ attr_reader :readers, :writers
96
+
97
+ attr_accessor :timeout, :maxlen
98
+
99
+ def interact
100
+ until readers.empty? && writers.empty?
101
+ handles = select(readers.keys, writers.keys, nil, timeout) or return
80
102
 
81
- output.each { |key, val|
82
- if val.is_a?(::Proc) && !val.respond_to?(:<<)
83
- class << val; alias_method :<<, :call; end
103
+ handle_readers(handles[0])
104
+ handle_writers(handles[1])
84
105
  end
85
106
 
86
- readers[key] = val
87
- }
107
+ []
108
+ end
109
+
110
+ private
111
+
112
+ def initialize_reader(reader)
113
+ if reader.is_a?(::Proc) && !reader.respond_to?(:<<)
114
+ class << reader; alias_method :<<, :call; end
115
+ end
116
+
117
+ reader
118
+ end
88
119
 
89
- input.each { |key, val|
90
- if key.is_a?(::String)
120
+ def initialize_writer(writer)
121
+ if writer.is_a?(::String)
91
122
  require 'stringio'
92
- key = ::StringIO.new(key)
123
+ writer = ::StringIO.new(writer)
93
124
  end
94
125
 
95
- unless key.respond_to?(:read_nonblock)
96
- def key.read_nonblock(*args)
126
+ unless writer.respond_to?(:read_nonblock)
127
+ def writer.read_nonblock(*args)
97
128
  read(*args) or raise ::EOFError, 'end of string reached'
98
129
  end
99
130
  end
100
131
 
101
- writers[val] = [key, '']
102
- }
132
+ writer
133
+ end
103
134
 
104
- close = lambda { |*args|
105
- container, item, read = args
135
+ def handle_readers(handles)
136
+ handles.each { |reader| read(reader, readers[reader]) }
137
+ end
106
138
 
107
- container.delete(item)
108
- read ? item.close_read : item.close_write
109
- }
139
+ def handle_writers(handles)
140
+ handles.each { |writer|
141
+ reader, buffer = writers[writer]
142
+ read(reader, buffer, writer) or next if buffer.empty?
110
143
 
111
- read = lambda { |*args|
112
- reader, buffer, writer = args
144
+ begin
145
+ bytes = writer.write_nonblock(buffer)
146
+ rescue ::Errno::EPIPE
147
+ close(writers, writer)
148
+ end
149
+
150
+ buffer.force_encoding('BINARY').slice!(0, bytes) if bytes
151
+ }
152
+ end
113
153
 
154
+ def read(reader, buffer, writer = nil)
114
155
  container = writer ? writers : readers
115
- buffer ||= container[reader]
116
156
 
117
157
  begin
118
158
  buffer << reader.read_nonblock(maxlen)
@@ -125,33 +165,15 @@ module Nuggets
125
165
  Encoding.default_external
126
166
  ) if buffer.respond_to?(:force_encoding)
127
167
 
128
- close[container, writer || reader, !writer]
168
+ close(container, writer || reader, !writer)
129
169
  end
130
- }
131
-
132
- until readers.empty? && writers.empty?
133
- fhs = select(readers.keys, writers.keys, nil, timeout) or return
134
-
135
- fhs[0].each { |reader| read[reader] }
136
-
137
- fhs[1].each { |writer|
138
- reader, buffer = writers[writer]
139
- read[reader, buffer, writer] or next if buffer.empty?
140
-
141
- begin
142
- bytes = writer.write_nonblock(buffer)
143
- rescue ::Errno::EPIPE
144
- close[writers, writer]
145
- end
170
+ end
146
171
 
147
- if bytes
148
- buffer.force_encoding('BINARY') if buffer.respond_to?(:force_encoding)
149
- buffer.slice!(0, bytes)
150
- end
151
- }
172
+ def close(container, item, reading = nil)
173
+ container.delete(item)
174
+ reading ? item.close_read : item.close_write
152
175
  end
153
176
 
154
- []
155
177
  end
156
178
 
157
179
  end
@@ -1,44 +1,2 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # nuggets -- Extending Ruby #
5
- # #
6
- # Copyright (C) 2007-2012 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
-
29
- module LazyAttr
30
-
31
- private
32
-
33
- def lazy_attr(attr, freeze = true)
34
- class << self; self; end.class_eval { attr_reader attr }
35
-
36
- value = instance_variable_get(name = "@#{attr}") ||
37
- instance_variable_set(name, yield)
38
-
39
- freeze ? value.freeze : value
40
- end
41
-
42
- end
43
-
44
- end
1
+ require 'nuggets/object/lazy_attr'
2
+ warn "#{__FILE__}: 'nuggets/lazy_attr' is deprecated, use 'nuggets/object/lazy_attr' instead." unless ENV['NUGGETS_DEPRECATED_LAZY_ATTR']
@@ -0,0 +1,5 @@
1
+ require 'nuggets/module/lazy_attr_mixin'
2
+
3
+ class Module
4
+ include Nuggets::Module::LazyAttrMixin
5
+ end
@@ -0,0 +1,55 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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
+ require 'nuggets/object/lazy_attr'
28
+
29
+ module Nuggets
30
+ class Module
31
+ module LazyAttrMixin
32
+
33
+ def lazy_accessor(name, options = {}, &block)
34
+ attr_accessor(lazy_name(name, options))
35
+ lazy_reader(name, options, &block)
36
+ end
37
+
38
+ def lazy_reader(name, options = {}, &block)
39
+ attr = lazy_name(name, options)
40
+
41
+ define_method(name) { lazy_attr(attr,
42
+ options.fetch(:freeze, true), &block) }
43
+ end
44
+
45
+ alias_method :lazy_attr, :lazy_reader
46
+
47
+ private
48
+
49
+ def lazy_name(name, options)
50
+ options.fetch(:name) { name.to_s.sub(/\?\z/, '_p') }
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/module/query_attr_mixin'
2
+
3
+ class Module
4
+ include Nuggets::Module::QueryAttrMixin
5
+ end
@@ -0,0 +1,49 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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 Module
29
+ module QueryAttrMixin
30
+
31
+ def query_accessor(*names)
32
+ query_reader(*names).each(&method(:attr_writer))
33
+ end
34
+
35
+ def query_reader(*names)
36
+ names.each { |name|
37
+ class_eval <<-EOT, __FILE__, __LINE__ + 1
38
+ def #{name}?
39
+ !!@#{name} if instance_variable_defined?("@#{name}")
40
+ end
41
+ EOT
42
+ }
43
+ end
44
+
45
+ alias_method :query_attr, :query_reader
46
+
47
+ end
48
+ end
49
+ end
@@ -3,7 +3,7 @@
3
3
  # #
4
4
  # nuggets -- Extending Ruby #
5
5
  # #
6
- # Copyright (C) 2007-2011 Jens Wille #
6
+ # Copyright (C) 2007-2014 Jens Wille #
7
7
  # #
8
8
  # Authors: #
9
9
  # Jens Wille <jens.wille@gmail.com> #
@@ -49,4 +49,11 @@ class Numeric
49
49
  alias_method :sign, :signum
50
50
  alias_method :sgn, :signum
51
51
 
52
+ def signum_s(positive = '+', negative = '-', zero = positive)
53
+ positive? ? positive : negative? ? negative : zero
54
+ end
55
+
56
+ alias_method :sign_s, :signum_s
57
+ alias_method :sgn_s, :signum_s
58
+
52
59
  end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/object/lazy_attr_mixin'
2
+
3
+ class Object
4
+ include Nuggets::Object::LazyAttrMixin
5
+ end
@@ -0,0 +1,45 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 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 Object
29
+ module LazyAttrMixin
30
+
31
+ private
32
+
33
+ def lazy_attr(name, freeze = true, &block)
34
+ class << self; self; end.send(:attr_reader, name)
35
+
36
+ value = instance_variable_defined?(ivar = "@#{name}") ?
37
+ instance_variable_get(ivar) :
38
+ instance_variable_set(ivar, instance_eval(&block))
39
+
40
+ freeze ? value.freeze : value
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
- TINY = 0
7
+ TINY = 1
8
8
 
9
9
  class << self
10
10
 
@@ -0,0 +1,45 @@
1
+ require 'nuggets/array/interpolate'
2
+
3
+ describe_extended Array, Nuggets::Array::InterpolateMixin do
4
+
5
+ {
6
+ [] => [],
7
+ [0] => [0],
8
+ [nil] => [0],
9
+ [0, nil] => [0, 0],
10
+ [1, nil, 3] => [1, 2, 3],
11
+ [1, nil, 3, nil] => [1, 2, 3, 1.5],
12
+ [1, nil, nil, 4] => [1, 2, 3, 4],
13
+ [1, nil, 3, nil, 5] => [1, 2, 3, 4, 5],
14
+ [1, nil, nil, 4, 5] => [1, 2, 3, 4, 5],
15
+ [1, nil, nil, nil, nil, nil, nil, nil, nil, nil, 11] => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
16
+ [1, nil, nil, nil, nil, 11, nil, nil, nil, nil, 11] => [1, 3, 5, 7, 9, 11, 11, 11, 11, 11, 11],
17
+ [1, 2, 3, nil, nil, 4, 5, nil, 6, nil, nil, nil, 7, nil] => [1, 2, 3, 3.3333333333333335, 3.6666666666666665, 4, 5, 5.5, 6, 6.25, 6.5, 6.75, 7, 3.5]
18
+ }.each { |a, b| example { a.interpolate.should == b } }
19
+
20
+ {
21
+ [] => [],
22
+ [0] => [0],
23
+ [nil] => [nil],
24
+ [0, nil] => [0, nil],
25
+ [1, nil, 3] => [1, 2, 3],
26
+ [1, nil, 3, nil] => [1, 2, 3, nil],
27
+ [1, nil, nil, 4] => [1, 2, 3, 4],
28
+ [1, nil, 3, nil, 5] => [1, 2, 3, 4, 5],
29
+ [1, nil, nil, 4, 5] => [1, 2, 3, 4, 5],
30
+ [1, nil, nil, nil, nil, nil, nil, nil, nil, nil, 11] => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
31
+ [1, nil, nil, nil, nil, 11, nil, nil, nil, nil, 11] => [1, 3, 5, 7, 9, 11, 11, 11, 11, 11, 11],
32
+ [1, 2, 3, nil, nil, 4, 5, nil, 6, nil, nil, nil, 7, nil] => [1, 2, 3, 3.3333333333333335, 3.6666666666666665, 4, 5, 5.5, 6, 6.25, 6.5, 6.75, 7, nil]
33
+ }.each { |a, b| example { a.interpolate(nil).should == b } }
34
+
35
+ example { [nil].interpolate(42).should == [42] }
36
+
37
+ example { [nil].interpolate(40, 44).should == [42] }
38
+
39
+ example { [nil, nil, nil, nil, 5].interpolate.should == [1, 2, 3, 4, 5] }
40
+
41
+ example { [nil, nil, nil, nil, 5].interpolate(10).should == [9, 8, 7, 6, 5] }
42
+
43
+ example { [nil, nil, nil, nil, 5, nil, nil, nil, nil].interpolate(10, 0).should == [9, 8, 7, 6, 5, 4, 3, 2, 1] }
44
+
45
+ end
@@ -0,0 +1,36 @@
1
+ require 'nuggets/integer/roman'
2
+
3
+ describe_extended Integer, Nuggets::Integer::RomanMixin do
4
+
5
+ {
6
+ 1 => 'I',
7
+ 2 => 'II',
8
+ 3 => 'III',
9
+ 4 => 'IV',
10
+ 5 => 'V',
11
+ 6 => 'VI',
12
+ 7 => 'VII',
13
+ 8 => 'VIII',
14
+ 9 => 'IX',
15
+ 10 => 'X',
16
+ 13 => 'XIII',
17
+ 22 => 'XXII',
18
+ 40 => 'XL',
19
+ 90 => 'XC',
20
+ 207 => 'CCVII',
21
+ 1066 => 'MLXVI',
22
+ 1666 => 'MDCLXVI',
23
+ 1903 => 'MCMIII',
24
+ 1910 => 'MCMX',
25
+ 1954 => 'MCMLIV',
26
+ 1990 => 'MCMXC',
27
+ 2014 => 'MMXIV',
28
+ 3999 => 'MMMCMXCIX'
29
+ }.each { |int, num|
30
+ example { int.to_roman.should == num }
31
+ example { (-int).to_roman.should == '-' + num }
32
+ }
33
+
34
+ example { 0.to_roman.should == 'N' }
35
+
36
+ end
@@ -0,0 +1,7 @@
1
+ require 'nuggets/module/lazy_attr'
2
+
3
+ describe_extended Module, Nuggets::Module::LazyAttrMixin do
4
+
5
+ # TODO
6
+
7
+ end
@@ -0,0 +1,58 @@
1
+ require 'nuggets/module/query_attr'
2
+
3
+ describe_extended Module, Nuggets::Module::QueryAttrMixin do
4
+
5
+ shared_examples 'common' do
6
+
7
+ subject { described_class.new }
8
+
9
+ example {
10
+ expect(subject.foo?).to be_nil
11
+ }
12
+
13
+ example {
14
+ subject.instance_variable_set(:@foo, 42)
15
+ expect(subject.foo?).to equal(true)
16
+ }
17
+
18
+ example {
19
+ expect { subject.foo }.to raise_error(NoMethodError)
20
+ }
21
+
22
+ end
23
+
24
+ shared_examples 'reader' do
25
+
26
+ it_behaves_like 'common'
27
+
28
+ example {
29
+ expect { subject.foo = 42 }.to raise_error(NoMethodError)
30
+ }
31
+
32
+ end
33
+
34
+ describe(Class.new { query_attr :foo }, '#query_attr') do
35
+ it_behaves_like 'reader'
36
+ end
37
+
38
+ describe(Class.new { query_reader :foo }, '#query_reader') do
39
+ it_behaves_like 'reader'
40
+ end
41
+
42
+ describe(Class.new { query_accessor :foo }, '#query_accessor') do
43
+
44
+ it_behaves_like 'common'
45
+
46
+ example {
47
+ subject.foo = 42
48
+ expect(subject.foo?).to equal(true)
49
+ }
50
+
51
+ example {
52
+ subject.foo = nil
53
+ expect(subject.foo?).to equal(false)
54
+ }
55
+
56
+ end
57
+
58
+ end
@@ -13,4 +13,15 @@ describe Numeric, 'signum' do
13
13
  example { n.sign.should == s }
14
14
  }
15
15
 
16
+ {
17
+ 123 => '+',
18
+ -123 => '-',
19
+ 0 => '+',
20
+ 0.001 => '+',
21
+ 1.23 => '+',
22
+ -12.3 => '-'
23
+ }.each { |n, s|
24
+ example { n.sign_s.should == s }
25
+ }
26
+
16
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.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-20 00:00:00.000000000 Z
11
+ date: 2014-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -113,6 +113,8 @@ files:
113
113
  - lib/nuggets/array/histogram.rb
114
114
  - lib/nuggets/array/histogram_mixin.rb
115
115
  - lib/nuggets/array/in_order.rb
116
+ - lib/nuggets/array/interpolate.rb
117
+ - lib/nuggets/array/interpolate_mixin.rb
116
118
  - lib/nuggets/array/limit.rb
117
119
  - lib/nuggets/array/limit_mixin.rb
118
120
  - lib/nuggets/array/mean.rb
@@ -135,6 +137,12 @@ files:
135
137
  - lib/nuggets/array/variance.rb
136
138
  - lib/nuggets/array/variance_mixin.rb
137
139
  - lib/nuggets/content_type.rb
140
+ - lib/nuggets/data/consume.rb
141
+ - lib/nuggets/data/consume_mixin.rb
142
+ - lib/nuggets/data/position.rb
143
+ - lib/nuggets/data/position_mixin.rb
144
+ - lib/nuggets/data/update.rb
145
+ - lib/nuggets/data/update_mixin.rb
138
146
  - lib/nuggets/dotted_decimal.rb
139
147
  - lib/nuggets/enumerable/agrep.rb
140
148
  - lib/nuggets/enumerable/all_any_extended.rb
@@ -177,6 +185,8 @@ files:
177
185
  - lib/nuggets/integer/length_mixin.rb
178
186
  - lib/nuggets/integer/map.rb
179
187
  - lib/nuggets/integer/map_mixin.rb
188
+ - lib/nuggets/integer/roman.rb
189
+ - lib/nuggets/integer/roman_mixin.rb
180
190
  - lib/nuggets/integer/to_binary_s.rb
181
191
  - lib/nuggets/io/agrep.rb
182
192
  - lib/nuggets/io/interact.rb
@@ -190,6 +200,10 @@ files:
190
200
  - lib/nuggets/log_parser.rb
191
201
  - lib/nuggets/log_parser/apache.rb
192
202
  - lib/nuggets/log_parser/rails.rb
203
+ - lib/nuggets/module/lazy_attr.rb
204
+ - lib/nuggets/module/lazy_attr_mixin.rb
205
+ - lib/nuggets/module/query_attr.rb
206
+ - lib/nuggets/module/query_attr_mixin.rb
193
207
  - lib/nuggets/net/success.rb
194
208
  - lib/nuggets/numeric/between.rb
195
209
  - lib/nuggets/numeric/duration.rb
@@ -202,6 +216,8 @@ files:
202
216
  - lib/nuggets/object/boolean_mixin.rb
203
217
  - lib/nuggets/object/eigenclass.rb
204
218
  - lib/nuggets/object/ghost_class.rb
219
+ - lib/nuggets/object/lazy_attr.rb
220
+ - lib/nuggets/object/lazy_attr_mixin.rb
205
221
  - lib/nuggets/object/metaclass.rb
206
222
  - lib/nuggets/object/msend.rb
207
223
  - lib/nuggets/object/msend_mixin.rb
@@ -250,6 +266,7 @@ files:
250
266
  - spec/nuggets/array/hashify_spec.rb
251
267
  - spec/nuggets/array/histogram_spec.rb
252
268
  - spec/nuggets/array/in_order_spec.rb
269
+ - spec/nuggets/array/interpolate_spec.rb
253
270
  - spec/nuggets/array/limit_spec.rb
254
271
  - spec/nuggets/array/mean_spec.rb
255
272
  - spec/nuggets/array/median_spec.rb
@@ -284,7 +301,10 @@ files:
284
301
  - spec/nuggets/integer/factorial_spec.rb
285
302
  - spec/nuggets/integer/length_spec.rb
286
303
  - spec/nuggets/integer/map_spec.rb
304
+ - spec/nuggets/integer/roman_spec.rb
287
305
  - spec/nuggets/integer/to_binary_s_spec.rb
306
+ - spec/nuggets/module/lazy_attr_spec.rb
307
+ - spec/nuggets/module/query_attr_spec.rb
288
308
  - spec/nuggets/numeric/duration_spec.rb
289
309
  - spec/nuggets/numeric/limit_spec.rb
290
310
  - spec/nuggets/numeric/signum_spec.rb
@@ -315,13 +335,18 @@ licenses:
315
335
  metadata: {}
316
336
  post_install_message: |2+
317
337
 
318
- nuggets-1.0.0 [2014-06-20]:
338
+ nuggets-1.0.1 [2014-08-27]:
319
339
 
320
- * First release under new name. Formerly known as ruby-nuggets.
340
+ * Added DATA.{consume,reposition,update}.
341
+ * Added Module#query_attr.
342
+ * Added Module#lazy_attr.
343
+ * Added Integer#to_roman.
344
+ * Added Numeric#signum_s.
345
+ * Added Array#interpolate.
321
346
 
322
347
  rdoc_options:
323
348
  - "--title"
324
- - nuggets Application documentation (v1.0.0)
349
+ - nuggets Application documentation (v1.0.1)
325
350
  - "--charset"
326
351
  - UTF-8
327
352
  - "--line-numbers"
@@ -342,7 +367,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
342
367
  version: '0'
343
368
  requirements: []
344
369
  rubyforge_project:
345
- rubygems_version: 2.3.0
370
+ rubygems_version: 2.4.1
346
371
  signing_key:
347
372
  specification_version: 4
348
373
  summary: Extending Ruby.