davidrichards-just_enumerable_stats 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/just_enumerable_stats.rb +49 -7
- data/spec/just_enumerable_stats_spec.rb +29 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/unobtrusive_just_enumerable_stats_spec.rb +12 -0
- metadata +2 -2
data/VERSION.yml
CHANGED
@@ -289,9 +289,9 @@ module Enumerable
|
|
289
289
|
if defined?(Dictionary)
|
290
290
|
@_jes_range_hash = Dictionary.new
|
291
291
|
@_jes_range_hash.merge!(hash)
|
292
|
-
@_jes_categories = @_jes_range_hash.keys
|
292
|
+
@_jes_categories = @_jes_range_hash.keys.dup
|
293
293
|
else
|
294
|
-
@_jes_categories = hash.keys
|
294
|
+
@_jes_categories = hash.keys.dup
|
295
295
|
@_jes_range_hash = hash
|
296
296
|
end
|
297
297
|
@_jes_category_values = nil
|
@@ -304,13 +304,19 @@ module Enumerable
|
|
304
304
|
# Allows you to add one category at a time. You can actually add more
|
305
305
|
# than one category at a time, but it won't disrupt any previously-set
|
306
306
|
# categories.
|
307
|
-
def _jes_add_category(hash)
|
307
|
+
def _jes_add_category(hash, &block)
|
308
308
|
_jes_init_range_hash
|
309
|
-
hash.
|
310
|
-
|
309
|
+
if hash.is_a?(Hash)
|
310
|
+
hash.each do |k, v|
|
311
|
+
@_jes_range_hash[k] = v
|
312
|
+
@_jes_categories << k
|
313
|
+
end
|
314
|
+
@_jes_category_values = nil
|
315
|
+
hash
|
316
|
+
# Allows the syntax a.add_category(:two_or_three) {|e| e == 2 or e == 3}
|
317
|
+
elsif block
|
318
|
+
_jes_add_category(hash => block)
|
311
319
|
end
|
312
|
-
@_jes_category_values = nil
|
313
|
-
hash
|
314
320
|
end
|
315
321
|
safe_alias :_jes_add_category
|
316
322
|
|
@@ -391,6 +397,42 @@ module Enumerable
|
|
391
397
|
end
|
392
398
|
safe_alias :_jes_category_values
|
393
399
|
|
400
|
+
# Returns the first category of a value. Example:
|
401
|
+
# a = [1,2,3]
|
402
|
+
# a.first_category(2) # => 2
|
403
|
+
# a.add_category(:small) {|e| e <= 1}
|
404
|
+
# a.add_category(:large) {|e| e > 1}
|
405
|
+
# a.first_category(2) # => :large
|
406
|
+
def _jes_first_category(value)
|
407
|
+
return value unless self.range_hash
|
408
|
+
self._jes_range_hash.each do |cat, block|
|
409
|
+
return cat if block.call(value)
|
410
|
+
end
|
411
|
+
return nil
|
412
|
+
end
|
413
|
+
safe_alias :_jes_first_category
|
414
|
+
safe_alias :category, :_jes_first_category
|
415
|
+
|
416
|
+
def _jes_all_categories(value)
|
417
|
+
return [value] unless self.range_hash
|
418
|
+
self._jes_range_hash.inject([]) do |list, e|
|
419
|
+
cat = e.first
|
420
|
+
block = e.last
|
421
|
+
list << cat if block.call(value)
|
422
|
+
list
|
423
|
+
end
|
424
|
+
end
|
425
|
+
safe_alias :_jes_all_categories
|
426
|
+
|
427
|
+
def _jes_category_map(reset=false)
|
428
|
+
@_jes_category_map = nil if reset
|
429
|
+
return @_jes_category_map if @_jes_category_map
|
430
|
+
@_jes_category_map = self.inject([]) do |list, e|
|
431
|
+
list << self._jes_first_category(e)
|
432
|
+
end
|
433
|
+
end
|
434
|
+
safe_alias :_jes_category_map
|
435
|
+
|
394
436
|
# When creating a range, what class will it be? Defaults to Range, but
|
395
437
|
# other classes are sometimes useful.
|
396
438
|
def _jes_range_class
|
@@ -250,6 +250,12 @@ describe "JustEnumerableStats" do
|
|
250
250
|
end
|
251
251
|
|
252
252
|
it "should be able to add a category" do
|
253
|
+
@a.add_category({">= 2" => lambda{ |e| e >= 2}})
|
254
|
+
@a.categories.sort.should eql([">= 2"].sort)
|
255
|
+
@a.category_values[">= 2"].should eql([2,3])
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should be able to add a category with categories already set" do
|
253
259
|
@a.set_categories({
|
254
260
|
"<= 2" => lambda{ |e| e <= 2 },
|
255
261
|
"== 2" => lambda{ |e| e == 2 }
|
@@ -270,6 +276,29 @@ describe "JustEnumerableStats" do
|
|
270
276
|
@a.category_values["> 2"].should eql([3])
|
271
277
|
end
|
272
278
|
|
279
|
+
it "should be able to return the first category a value belongs to" do
|
280
|
+
@a.first_category(2).should eql(2)
|
281
|
+
@a.category(2).should eql(2)
|
282
|
+
@a.add_category(:small) {|e| e <= 2}
|
283
|
+
@a.add_category(:large) {|e| e >= 2}
|
284
|
+
@a.first_category(2).should eql(:small)
|
285
|
+
@a.category(2).should eql(:small)
|
286
|
+
end
|
287
|
+
|
288
|
+
it "should be able to return all categories a value belongs to" do
|
289
|
+
@a.all_categories(2).should eql([2])
|
290
|
+
@a.add_category(:small) {|e| e <= 2}
|
291
|
+
@a.add_category(:large) {|e| e >= 2}
|
292
|
+
@a.all_categories(2).should eql([:small, :large])
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should be able to return a map of each item's category" do
|
296
|
+
@a.category_map.should eql([1,2,3])
|
297
|
+
@a.add_category(:small) {|e| e <= 1}
|
298
|
+
@a.add_category(:large) {|e| e > 1}
|
299
|
+
@a.category_map(true).should eql([:small, :large, :large])
|
300
|
+
end
|
301
|
+
|
273
302
|
it "should be able to get categories with dot notation" do
|
274
303
|
@a.set_range({
|
275
304
|
:small => lambda{ |e| e <= 2 },
|
data/spec/spec_helper.rb
CHANGED
@@ -45,6 +45,9 @@ class BusyClass
|
|
45
45
|
def dichotomize(split_value, first_label, second_label); raise ArgumentError, "Should not be called"; end
|
46
46
|
def count_if(&block); raise ArgumentError, "Should not be called"; end
|
47
47
|
def category_values(reset=false); raise ArgumentError, "Should not be called"; end
|
48
|
+
def first_category(value); raise ArgumentError, "Should not be called"; end
|
49
|
+
def all_categories(value); raise ArgumentError, "Should not be called"; end
|
50
|
+
def category_map(reset=false); raise ArgumentError, "Should not be called"; end
|
48
51
|
def range_class; raise ArgumentError, "Should not be called"; end
|
49
52
|
def range_as_range(&block); raise ArgumentError, "Should not be called"; end
|
50
53
|
def new_sort(&block); raise ArgumentError, "Should not be called"; end
|
@@ -92,6 +92,18 @@ describe "JustEnumerableStats" do
|
|
92
92
|
lambda{@a._jes_category_values}.should_not raise_error
|
93
93
|
end
|
94
94
|
|
95
|
+
it "should not use the native first_category" do
|
96
|
+
lambda{@a._jes_first_category(1)}.should_not raise_error
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should not use the native all_categories" do
|
100
|
+
lambda{@a._jes_all_categories(1)}.should_not raise_error
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should not use the native category_map" do
|
104
|
+
lambda{@a._jes_category_map}.should_not raise_error
|
105
|
+
end
|
106
|
+
|
95
107
|
it "should not use the native range_class" do
|
96
108
|
lambda{@a._jes_range_class}.should_not raise_error
|
97
109
|
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.
|
4
|
+
version: 0.0.16
|
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-08-
|
12
|
+
date: 2009-08-24 00:00:00 -07:00
|
13
13
|
default_executable: jes
|
14
14
|
dependencies: []
|
15
15
|
|