davidrichards-just_enumerable_stats 0.0.2 → 0.0.3

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.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 0
4
- :patch: 2
4
+ :patch: 3
@@ -1,3 +1,7 @@
1
+ # Need the FixedRange
2
+ $:.unshift File.dirname(__FILE__)
3
+ require 'fixed_range'
4
+
1
5
  # Borrowed this from my own gem, sirb
2
6
 
3
7
  class Object
@@ -200,6 +204,28 @@ module Enumerable
200
204
  end
201
205
  protected :iterate_midway
202
206
 
207
+ # Takes the range_class and returns its map.
208
+ # Example:
209
+ # require 'mathn'
210
+ # a = [1,2,3]
211
+ # a
212
+ # range_class = FixedRange, a.min, a.max, 1/4
213
+ # a.categories
214
+ # => [1, 5/4, 3/2, 7/4, 2, 9/4, 5/2, 11/4, 3]
215
+ # For non-numeric values, returns a unique set,
216
+ # ordered if possible.
217
+ def categories
218
+ if self.is_numeric?
219
+ self.range_instance.map
220
+ else
221
+ self.uniq.sort rescue self.uniq
222
+ end
223
+ end
224
+
225
+ def is_numeric?
226
+ self.all? {|e| e.is_a?(Numeric)}
227
+ end
228
+
203
229
  # Just an array of [min, max] to comply with R uses of the work. Use
204
230
  # range_as_range if you want a real Range.
205
231
  def range(&block)
@@ -207,9 +233,13 @@ module Enumerable
207
233
  end
208
234
 
209
235
  # Useful for setting a real range class (FixedRange).
210
- def range_class=(klass)
236
+ def set_range_class(klass, *args)
211
237
  @range_class = klass
238
+ @range_class_args = args
239
+ self.range_class
212
240
  end
241
+
242
+ attr_reader :range_class_args
213
243
 
214
244
  # When creating a range, what class will it be? Defaults to Range, but
215
245
  # other classes are sometimes useful.
@@ -219,8 +249,13 @@ module Enumerable
219
249
 
220
250
  # Actually instantiates the range, instead of producing a min and max array.
221
251
  def range_as_range(&block)
222
- range_class.new(min(&block), max(&block))
252
+ if @range_class_args and not @range_class_args.empty?
253
+ self.range_class.new(*@range_class_args)
254
+ else
255
+ self.range_class.new(min(&block), max(&block))
256
+ end
223
257
  end
258
+ alias :range_instance :range_as_range
224
259
 
225
260
  # I don't pass the block to the sort, because a sort block needs to look
226
261
  # something like: {|x,y| x <=> y}. To get around this, set the default
@@ -199,6 +199,28 @@ module JustEnumerableStats #:nodoc:
199
199
  end
200
200
  protected :iterate_midway
201
201
 
202
+ # Takes the range_class and returns its map.
203
+ # Example:
204
+ # require 'mathn'
205
+ # a = [1,2,3]
206
+ # a
207
+ # range_class = FixedRange, a.min, a.max, 1/4
208
+ # a.categories
209
+ # => [1, 5/4, 3/2, 7/4, 2, 9/4, 5/2, 11/4, 3]
210
+ # For non-numeric values, returns a unique set,
211
+ # ordered if possible.
212
+ def categories
213
+ if self.is_numeric?
214
+ self.range_instance.map
215
+ else
216
+ self.uniq.sort rescue self.uniq
217
+ end
218
+ end
219
+
220
+ def is_numeric?
221
+ self.all? {|e| e.is_a?(Numeric)}
222
+ end
223
+
202
224
  # Just an array of [min, max] to comply with R uses of the work. Use
203
225
  # range_as_range if you want a real Range.
204
226
  def range(&block)
@@ -206,10 +228,14 @@ module JustEnumerableStats #:nodoc:
206
228
  end
207
229
 
208
230
  # Useful for setting a real range class (FixedRange).
209
- def range_class=(klass)
231
+ def set_range_class(klass, *args)
210
232
  @range_class = klass
233
+ @range_class_args = args
234
+ self.range_class
211
235
  end
212
236
 
237
+ attr_reader :range_class_args
238
+
213
239
  # When creating a range, what class will it be? Defaults to Range, but
214
240
  # other classes are sometimes useful.
215
241
  def range_class
@@ -218,8 +244,13 @@ module JustEnumerableStats #:nodoc:
218
244
 
219
245
  # Actually instantiates the range, instead of producing a min and max array.
220
246
  def range_as_range(&block)
221
- range_class.new(min(&block), max(&block))
247
+ if @range_class_args and not @range_class_args.empty?
248
+ self.range_class.new(*@range_class_args)
249
+ else
250
+ self.range_class.new(min(&block), max(&block))
251
+ end
222
252
  end
253
+ alias :range_instance :range_as_range
223
254
 
224
255
  # I don't pass the block to the sort, because a sort block needs to look
225
256
  # something like: {|x,y| x <=> y}. To get around this, set the default
@@ -207,10 +207,42 @@ describe JustEnumerableStats::Stats do
207
207
  end
208
208
 
209
209
  it "should have a getter and a setter on the range class" do
210
- @a.range_class = Array
210
+ @a.set_range_class Array
211
211
  @a.range_class.should eql(Array)
212
212
  end
213
213
 
214
+ it "should be able to send extra arguments for the range class" do
215
+ @a.set_range_class FixedRange, 1, 3, 0.25
216
+ @a.range_as_range.should be_is_a(FixedRange)
217
+ @a.range_instance.should be_is_a(FixedRange)
218
+ end
219
+
220
+ it "should use the range arguments when instantiating a range" do
221
+ @a.set_range_class FixedRange, 1, 3, 0.5
222
+ @a.range_instance.should be_is_a(FixedRange)
223
+ @a.range_instance.min.should eql(1)
224
+ @a.range_instance.max.should eql(3)
225
+ @a.range_instance.step_size.should eql(0.5)
226
+ end
227
+
228
+ it "should be able to instantiate a range" do
229
+ @a.range_as_range.should eql(Range.new(1, 3))
230
+ end
231
+
232
+ it "should know its categories, based on its range" do
233
+ @a.categories.should eql([1,2,3])
234
+ end
235
+
236
+ it "should know its categories with a different range class" do
237
+ @a.set_range_class FixedRange, 1, 3, 0.5
238
+ @a.categories.should eql([1.0, 1.5, 2.0, 2.5, 3.0])
239
+ end
240
+
241
+ it "should know its categories from non-numeric values" do
242
+ a = [:this, :and, :that]
243
+ a.categories.should eql(a)
244
+ end
245
+
214
246
  it "should be able to instantiate a range" do
215
247
  @a.range_as_range.should eql(Range.new(1, 3))
216
248
  end
@@ -197,14 +197,42 @@ describe "JustEnumerableStats" do
197
197
  end
198
198
 
199
199
  it "should have a getter and a setter on the range class" do
200
- @a.range_class = Array
200
+ @a.set_range_class Array
201
201
  @a.range_class.should eql(Array)
202
202
  end
203
203
 
204
+ it "should be able to send extra arguments for the range class" do
205
+ @a.set_range_class FixedRange, 1, 3, 0.25
206
+ @a.range_as_range.should be_is_a(FixedRange)
207
+ @a.range_instance.should be_is_a(FixedRange)
208
+ end
209
+
210
+ it "should use the range arguments when instantiating a range" do
211
+ @a.set_range_class FixedRange, 1, 3, 0.5
212
+ @a.range_instance.should be_is_a(FixedRange)
213
+ @a.range_instance.min.should eql(1)
214
+ @a.range_instance.max.should eql(3)
215
+ @a.range_instance.step_size.should eql(0.5)
216
+ end
217
+
204
218
  it "should be able to instantiate a range" do
205
219
  @a.range_as_range.should eql(Range.new(1, 3))
206
220
  end
207
221
 
222
+ it "should know its categories, based on its range" do
223
+ @a.categories.should eql([1,2,3])
224
+ end
225
+
226
+ it "should know its categories with a different range class" do
227
+ @a.set_range_class FixedRange, 1, 3, 0.5
228
+ @a.categories.should eql([1.0, 1.5, 2.0, 2.5, 3.0])
229
+ end
230
+
231
+ it "should know its categories from non-numeric values" do
232
+ a = [:this, :and, :that]
233
+ a.categories.should eql(a)
234
+ end
235
+
208
236
  it "should be able to instantiate a range with a block" do
209
237
  @a.range_as_range(&@inverse_matcher).should eql(Range.new(3, 1))
210
238
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: davidrichards-just_enumerable_stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Richards
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-21 00:00:00 -07:00
12
+ date: 2009-08-03 00:00:00 -07:00
13
13
  default_executable: jes
14
14
  dependencies: []
15
15
 
@@ -36,6 +36,7 @@ files:
36
36
  - spec/spec_helper.rb
37
37
  has_rdoc: true
38
38
  homepage: http://github.com/davidrichards/just_enumerable_stats
39
+ licenses:
39
40
  post_install_message:
40
41
  rdoc_options:
41
42
  - --inline-source
@@ -57,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
58
  requirements: []
58
59
 
59
60
  rubyforge_project:
60
- rubygems_version: 1.2.0
61
+ rubygems_version: 1.3.5
61
62
  signing_key:
62
63
  specification_version: 2
63
64
  summary: Basic statistics on Ruby enumerables