ruby-nuggets 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.8.5
5
+ This documentation refers to ruby-nuggets version 0.8.6
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -30,16 +30,28 @@ module Nuggets
30
30
  module LimitMixin
31
31
 
32
32
  # call-seq:
33
- # array.limit(min, max) => new_array
33
+ # array.limit(min, max) => anArray
34
34
  #
35
35
  # Returns a new array of all distinct values in _array_ limited to +min+
36
- # and +max+ (cf. Numeric#limit).
37
- def limit(min, max)
38
- map { |item| item.between(min, max) }.uniq
36
+ # and +max+ (cf. Numeric#limit). If +uniq+ is +true+, resulting duplicates
37
+ # will be removed.
38
+ def limit(min, max, uniq = true)
39
+ limited = cap(min..max)
40
+ limited.uniq! if uniq
41
+ limited
39
42
  end
40
43
 
41
44
  alias_method :between, :limit
42
45
 
46
+ def cap(max)
47
+ if max.respond_to?(:begin)
48
+ min, max = max.begin, max.end
49
+ map { |item| item.limit(min, max) }
50
+ else
51
+ map { |item| item.max(max) }
52
+ end
53
+ end
54
+
43
55
  end
44
56
  end
45
57
  end
@@ -39,13 +39,13 @@ end
39
39
  module Enumerable
40
40
 
41
41
  # call-seq:
42
- # enum.agrep(pattern[, distance]) => anArray
43
- # enum.agrep(pattern[, distance]) { |obj| ... } => anArray
42
+ # enum.agrep(pattern[, distance]) -> anArray
43
+ # enum.agrep(pattern[, distance]) { |element| ... } -> enum
44
44
  #
45
- # Returns an array of every element in _enum_ for which +pattern+ approximately
45
+ # Returns an array of all elements in _enum_ for which +pattern+ approximately
46
46
  # matches +element+ (see Amatch::Levenshtein#search). If the optional +block+
47
- # is supplied, each matching element is passed to it, and the block‘s result
48
- # is stored in the output array.
47
+ # is supplied, each matching element is passed to it, and the _enum_ itself is
48
+ # returned.
49
49
  #
50
50
  # LIMITATIONS:
51
51
  #
@@ -55,14 +55,12 @@ module Enumerable
55
55
  # - The cost for individual error types (substitution, insertion, deletion)
56
56
  # cannot be adjusted.
57
57
  def agrep(pattern, distance = 0)
58
- raise 'Amatch not available!' unless defined?(::Amatch)
59
-
60
58
  pattern = pattern.source if pattern.is_a?(::Regexp)
61
59
 
62
- amatch = ::Amatch::Levenshtein.new(pattern)
63
- matches = select { |obj| amatch.search(obj.to_s) <= distance }
60
+ am = ::Amatch::Levenshtein.new(pattern)
61
+ ma = lambda { |i| am.search(i.to_s) <= distance }
64
62
 
65
- block_given? ? matches.map { |match| yield match } : matches
63
+ block_given? ? each { |i| yield i if ma[i] } : select(&ma)
66
64
  end
67
65
 
68
66
  end
@@ -30,10 +30,11 @@ require 'nuggets/enumerable/agrep'
30
30
  class IO
31
31
 
32
32
  # call-seq:
33
- # IO.agrep(fd, pattern[, distance]) => anArray
33
+ # IO.agrep(fd, pattern[, distance]) -> anArray
34
+ # IO.agrep(fd, pattern[, distance]) { |line| ... } -> io
34
35
  #
35
- def self.agrep(fd, pattern, distance)
36
- open(fd) { |io| io.agrep(pattern, distance) }
36
+ def self.agrep(fd, pattern, distance = 0, &block)
37
+ open(fd) { |io| io.agrep(pattern, distance, &block) }
37
38
  end
38
39
 
39
40
  end
@@ -85,7 +85,7 @@ module Nuggets
85
85
  gsub!(/::/, '/')
86
86
 
87
87
  a = CAMELSCORE_ACRONYMS.values
88
- r = a.empty? ? /(?=a)b/ : Regexp.union(*a)
88
+ r = a.empty? ? /(?=a)b/ : ::Regexp.union(*a.sort_by { |v| v.length })
89
89
 
90
90
  gsub!(/(?:([A-Za-z])|(\d)|^)(#{r})(?=\b|[^a-z])/) {
91
91
  "#{$1 || $2}#{'_' if $1}#{$3.downcase}"
@@ -103,11 +103,11 @@ module Nuggets
103
103
  # str.constantize(base = Object) => anObject
104
104
  #
105
105
  # Returns the constant pointed to by _str_, relative to +base+.
106
- def constantize(base = Object)
106
+ def constantize(base = ::Object)
107
107
  names = split('::')
108
108
  return if names.empty?
109
109
 
110
- const = names.first.empty? ? (names.shift; Object) : base
110
+ const = names.first.empty? ? (names.shift; ::Object) : base
111
111
  names.each { |name| const = const.const_get(name) }
112
112
  const
113
113
  end
@@ -158,7 +158,11 @@ module Util
158
158
  end
159
159
 
160
160
  def load_config(file = options[:config] || defaults[:config])
161
- @config = ::YAML.load_file(file) if ::File.readable?(file)
161
+ if ::File.readable?(file)
162
+ @config = ::YAML.load_file(file)
163
+ elsif file != defaults[:config]
164
+ quit "No such file: #{file}"
165
+ end
162
166
  end
163
167
 
164
168
  def merge_config(args = [config, defaults])
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 8
7
- TINY = 5
7
+ TINY = 6
8
8
 
9
9
  class << self
10
10
 
@@ -5,32 +5,60 @@ describe Array, 'when extended by', Nuggets::Array::LimitMixin do
5
5
 
6
6
  it { Array.ancestors.should include(Nuggets::Array::LimitMixin) }
7
7
 
8
- example do
9
- [].limit(0, 1).should == []
10
- end
8
+ describe '#limit' do
11
9
 
12
- example do
13
- [1].limit(0, 1).should == [1]
14
- end
10
+ example do
11
+ [].limit(0, 1).should == []
12
+ end
15
13
 
16
- example do
17
- [1, 1, 1].limit(0, 1).should == [1]
18
- end
14
+ example do
15
+ [1].limit(0, 1).should == [1]
16
+ end
19
17
 
20
- example do
21
- [1, 2, 3].limit(0, 1).should == [1]
22
- end
18
+ example do
19
+ [1, 1, 1].limit(0, 1).should == [1]
20
+ end
23
21
 
24
- example do
25
- [1, 2, 3].limit(0, 2).should == [1, 2]
26
- end
22
+ example do
23
+ [1, 2, 3].limit(0, 1).should == [1]
24
+ end
25
+
26
+ example do
27
+ [1, 2, 3].limit(0, 2).should == [1, 2]
28
+ end
29
+
30
+ example do
31
+ [-3, -2, -1].limit(0, 1).should == [0]
32
+ end
33
+
34
+ example do
35
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].limit(-2, 2).should == [1, -2, 2, 0]
36
+ end
27
37
 
28
- example do
29
- [-3, -2, -1].limit(0, 1).should == [0]
30
38
  end
31
39
 
32
- example do
33
- [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].limit(-2, 2).should == [1, -2, 2, 0]
40
+ describe '#cap' do
41
+
42
+ example do
43
+ [1, 1, 1].cap(1).should == [1, 1, 1]
44
+ end
45
+
46
+ example do
47
+ [1, 2, 3].cap(1).should == [1, 1, 1]
48
+ end
49
+
50
+ example do
51
+ [1, 2, 3].cap(2).should == [1, 2, 2]
52
+ end
53
+
54
+ example do
55
+ [-3, -2, -1].cap(0).should == [-3, -2, -1]
56
+ end
57
+
58
+ example do
59
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].cap(-2..2).should == [1, -2, 1, 2, 2, -2, 0, 2, 1, 2, 1, 0, 2]
60
+ end
61
+
34
62
  end
35
63
 
36
64
  end
@@ -6,8 +6,8 @@ describe String, 'when extended by', Nuggets::String::CamelscoreMixin do
6
6
 
7
7
  def self.with_acronyms(acronyms, &block)
8
8
  describe "with acronyms #{acronyms.inspect}" do
9
- before :all do String::CAMELSCORE_ACRONYMS.replace(acronyms) end
10
- after :all do String::CAMELSCORE_ACRONYMS.clear end
9
+ before :all do ::String::CAMELSCORE_ACRONYMS.replace(acronyms) end
10
+ after :all do ::String::CAMELSCORE_ACRONYMS.clear end
11
11
 
12
12
  instance_eval(&block)
13
13
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 53
4
+ hash: 51
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 5
10
- version: 0.8.5
9
+ - 6
10
+ version: 0.8.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jens Wille
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-27 00:00:00 Z
18
+ date: 2012-04-20 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Some extensions to the Ruby programming language.
@@ -206,14 +206,14 @@ licenses: []
206
206
 
207
207
  post_install_message:
208
208
  rdoc_options:
209
- - --line-numbers
210
209
  - --main
211
210
  - README
212
- - --all
213
211
  - --charset
214
212
  - UTF-8
215
213
  - --title
216
- - ruby-nuggets Application documentation (v0.8.5)
214
+ - ruby-nuggets Application documentation (v0.8.6)
215
+ - --all
216
+ - --line-numbers
217
217
  require_paths:
218
218
  - lib
219
219
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -237,7 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
237
  requirements: []
238
238
 
239
239
  rubyforge_project: prometheus
240
- rubygems_version: 1.8.15
240
+ rubygems_version: 1.8.23
241
241
  signing_key:
242
242
  specification_version: 3
243
243
  summary: Some extensions to the Ruby programming language.