davidrichards-just_enumerable_stats 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 0
4
- :patch: 14
4
+ :patch: 15
@@ -294,9 +294,49 @@ module Enumerable
294
294
  @_jes_categories = hash.keys
295
295
  @_jes_range_hash = hash
296
296
  end
297
+ @_jes_category_values = nil
297
298
  @_jes_categories
298
299
  end
299
300
  safe_alias :_jes_set_range
301
+ safe_alias :_jes_set_categories, :_jes_set_range
302
+ safe_alias :set_categories, :_jes_set_range
303
+
304
+ # Allows you to add one category at a time. You can actually add more
305
+ # than one category at a time, but it won't disrupt any previously-set
306
+ # categories.
307
+ def _jes_add_category(hash)
308
+ _jes_init_range_hash
309
+ hash.each do |k, v|
310
+ @_jes_range_hash[k] = v
311
+ end
312
+ @_jes_category_values = nil
313
+ hash
314
+ end
315
+ safe_alias :_jes_add_category
316
+
317
+ def method_missing(sym, *args, &block)
318
+ if self.categories.include?(sym)
319
+ self._jes_render_category(sym)
320
+ else
321
+ super
322
+ end
323
+ end
324
+
325
+ # Returns a specific category's values
326
+ def _jes_render_category(category)
327
+ self.category_values[category]
328
+ end
329
+
330
+ def _jes_init_range_hash
331
+ if defined?(Dictionary)
332
+ @_jes_range_hash ||= Dictionary.new
333
+ @_jes_categories ||= []
334
+ else
335
+ @_jes_range_hash ||= {}
336
+ @_jes_categories ||= []
337
+ end
338
+ end
339
+ protected :_jes_init_range_hash
300
340
 
301
341
  # The hash of lambdas that are used to categorize the enumerable.
302
342
  attr_reader :_jes_range_hash
@@ -240,6 +240,26 @@ describe "JustEnumerableStats" do
240
240
  })
241
241
  @a.categories.sort.should eql(["<= 2", "> 2"].sort)
242
242
  end
243
+
244
+ it "should be able to set a category as an alias to set_range" do
245
+ @a.set_categories({
246
+ "<= 2" => lambda{ |e| e <= 2 },
247
+ "> 2" => lambda{ |e| e > 2 }
248
+ })
249
+ @a.categories.sort.should eql(["<= 2", "> 2"].sort)
250
+ end
251
+
252
+ it "should be able to add a category" do
253
+ @a.set_categories({
254
+ "<= 2" => lambda{ |e| e <= 2 },
255
+ "== 2" => lambda{ |e| e == 2 }
256
+ })
257
+ @a.category_values["<= 2"].should eql([1,2])
258
+ @a.category_values["== 2"].should eql([2])
259
+ @a.add_category({">= 2" => lambda{ |e| e >= 2}})
260
+ @a.categories.sort.should eql([">= 2", "<= 2", "== 2"].sort)
261
+ @a.category_values[">= 2"].should eql([2,3])
262
+ end
243
263
 
244
264
  it "should be able to get a hash of category values" do
245
265
  @a.set_range({
@@ -250,6 +270,14 @@ describe "JustEnumerableStats" do
250
270
  @a.category_values["> 2"].should eql([3])
251
271
  end
252
272
 
273
+ it "should be able to get categories with dot notation" do
274
+ @a.set_range({
275
+ :small => lambda{ |e| e <= 2 },
276
+ :large => lambda{ |e| e > 2 }
277
+ })
278
+ @a.small.should eql([1,2])
279
+ end
280
+
253
281
  it "should be able to get category values with a regular range" do
254
282
  @a.category_values[1].should eql([1])
255
283
  @a.category_values[2].should eql([2])
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,13 @@ Spec::Runner.configure do |config|
7
7
 
8
8
  end
9
9
 
10
+
11
+ # This is one monster class that happens to have everything that we want
12
+ # to define with this gem. Since all the definitions raise an error,
13
+ # all we have to do is call the _jes_ version of each method and make
14
+ # sure that no error is raised. This helps us make sure there are no
15
+ # dependencies from within the method that use a non-jes version of a
16
+ # method.
10
17
  class BusyClass
11
18
  include Enumerable
12
19
  def initialize(*vals)
@@ -33,6 +40,8 @@ class BusyClass
33
40
  def range(&block); raise ArgumentError, "Should not be called"; end
34
41
  def set_range_class(klass, *args); raise ArgumentError, "Should not be called"; end
35
42
  def set_range(hash); raise ArgumentError, "Should not be called"; end
43
+ def set_categories(hash); raise ArgumentError, "Should not be called"; end
44
+ def add_category(hash); raise ArgumentError, "Should not be called"; end
36
45
  def dichotomize(split_value, first_label, second_label); raise ArgumentError, "Should not be called"; end
37
46
  def count_if(&block); raise ArgumentError, "Should not be called"; end
38
47
  def category_values(reset=false); raise ArgumentError, "Should not be called"; end
@@ -72,6 +72,14 @@ describe "JustEnumerableStats" do
72
72
  lambda{@a._jes_set_range({:a => 1})}.should_not raise_error
73
73
  end
74
74
 
75
+ it "should not use the native set_categories" do
76
+ lambda{@a._jes_set_categories({:a => 1})}.should_not raise_error
77
+ end
78
+
79
+ it "should not use the native add_category" do
80
+ lambda{@a._jes_add_category({:a => 1})}.should_not raise_error
81
+ end
82
+
75
83
  it "should not use the native dichotomize" do
76
84
  lambda{@a._jes_dichotomize(2, :small, :big)}.should_not raise_error
77
85
  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.14
4
+ version: 0.0.15
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-17 00:00:00 -07:00
12
+ date: 2009-08-19 00:00:00 -07:00
13
13
  default_executable: jes
14
14
  dependencies: []
15
15