Onion 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. data/Rakefile +13 -1
  2. data/lib/onion.rb +23 -21
  3. metadata +2 -2
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ require 'rake/testtask'
21
21
 
22
22
  spec = Gem::Specification.new do |s|
23
23
  s.name = 'Onion'
24
- s.version = '0.0.1'
24
+ s.version = '0.0.2'
25
25
  s.has_rdoc = true
26
26
  s.extra_rdoc_files = ['README', 'LICENSE']
27
27
  s.summary = 'Onion, or Array Onion, allows peeling an array like an onion.'
@@ -59,3 +59,15 @@ end
59
59
  Rake::TestTask.new do |t|
60
60
  t.test_files = FileList['test/**/*.rb']
61
61
  end
62
+
63
+ desc("Count lines of code")
64
+ task(:cloc) do
65
+ sh("cloc */* --exclude-dir=nbproject,doc,coverage --by-file")
66
+ puts("\tDone counting lines of code.")
67
+ end
68
+
69
+ desc("Calculate coverage (RCov)")
70
+ task(:rcov) do
71
+ `rcov *\\*.rb`
72
+ puts( "\tDone performing code coverage. See the coverage folder")
73
+ end
@@ -24,43 +24,45 @@
24
24
  class Array
25
25
  # Returns the maximum depth in an array with or without nested arrays.
26
26
  def depth
27
- depth = 1
27
+ depth = 0
28
28
 
29
29
  each do |element|
30
30
  if element.kind_of?(Array)
31
31
  n = element.depth
32
- depth += n if (n >= depth)
32
+ depth = n if (n > depth)
33
33
  end
34
34
  end
35
35
 
36
- depth
36
+ depth + 1
37
37
  end
38
38
 
39
39
  # Peels until the given specific depth is reached. It uses Array#depth to
40
40
  # determine how many layers to peel.
41
41
  def peel_until(depth = 0)
42
- unless depth == 0
43
- n = self.depth - depth
44
- if n > 0
45
- self.peel(n)
46
- else
47
- self
48
- end
49
- end
42
+ (depth < 2) ? self.flatten :
43
+ (depth = self.depth - depth) > 0 ? self.peel(depth) : self
50
44
  end
51
45
 
52
46
  # Peels layers of an array according to the given depth.
53
47
  def peel(depth = 1)
54
- result = []
55
-
56
- raise RangeError, "Negative peeling depth", caller if depth < 0
57
-
58
- each do |element|
59
- element.kind_of?(Array) ?
60
- result += element.peel(depth - 1) :
61
- result << element
62
- end unless depth <= 0
48
+ if ((depth < 0) || (depth >= self.depth))
49
+ self.flatten
50
+ elsif (depth > 0)
51
+ result = []
52
+ each do |element|
53
+ element.kind_of?(Array) ?
54
+ result += element.peel(depth - 1) :
55
+ result << element # rcov wrongfully report it as uncovered.
56
+ end
57
+ result
58
+ else
59
+ self
60
+ end
61
+ end
63
62
 
64
- result.empty? ? self : result
63
+ # Peels layers of an array according to the given depth and performs
64
+ # permanent change in the array.
65
+ def peel!(depth = 1)
66
+ replace peel(depth)
65
67
  end
66
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Onion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Trudel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-28 00:00:00 -04:00
12
+ date: 2009-04-16 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15