From 30bb879c4cbf7f4c0f9423d00adc290717a77122 Mon Sep 17 00:00:00 2001 From: Derrick Parkhurst Date: Thu, 18 Dec 2014 17:58:47 -0600 Subject: [PATCH] add support for refinements --- .simplecov | 1 + README.md | 245 +++++++++--------- Rakefile | 25 +- descriptive_statistics.gemspec | 2 +- .../enumerable_extension.rb | 11 +- lib/descriptive_statistics/refinement.rb | 33 +++ pkg/descriptive_statistics-2.4.0.gem | Bin 6144 -> 0 bytes pkg/descriptive_statistics-2.5.0.gem | Bin 0 -> 6144 bytes spec/{ => monkeypatch}/array_spec.rb | 2 +- .../empty_collection_spec.rb | 6 +- spec/{ => monkeypatch}/enumerable_spec.rb | 2 +- spec/{ => monkeypatch}/hash_spec.rb | 2 +- spec/{ => monkeypatch}/object_spec.rb | 2 +- spec/{ => monkeypatch}/set_spec.rb | 2 +- spec/{ => monkeypatch}/single_value_spec.rb | 2 +- spec/monkeypatch/spec_helper.rb | 2 + spec/refinement/refinement_spec.rb | 27 ++ spec/refinement/spec_helper.rb | 2 + spec/{ => safe}/class_method_spec.rb | 4 +- spec/{ => safe}/extend_spec.rb | 2 +- spec/safe/spec_helper.rb | 2 + spec/{ => safe}/stats_spec.rb | 4 +- spec/spec_helper.rb | 2 - test/test.rb | 3 +- 24 files changed, 241 insertions(+), 142 deletions(-) create mode 100644 .simplecov create mode 100644 lib/descriptive_statistics/refinement.rb delete mode 100644 pkg/descriptive_statistics-2.4.0.gem create mode 100644 pkg/descriptive_statistics-2.5.0.gem rename spec/{ => monkeypatch}/array_spec.rb (97%) rename spec/{ => monkeypatch}/empty_collection_spec.rb (97%) rename spec/{ => monkeypatch}/enumerable_spec.rb (95%) rename spec/{ => monkeypatch}/hash_spec.rb (97%) rename spec/{ => monkeypatch}/object_spec.rb (98%) rename spec/{ => monkeypatch}/set_spec.rb (97%) rename spec/{ => monkeypatch}/single_value_spec.rb (97%) create mode 100644 spec/monkeypatch/spec_helper.rb create mode 100644 spec/refinement/refinement_spec.rb create mode 100644 spec/refinement/spec_helper.rb rename spec/{ => safe}/class_method_spec.rb (95%) rename spec/{ => safe}/extend_spec.rb (98%) create mode 100644 spec/safe/spec_helper.rb rename spec/{ => safe}/stats_spec.rb (95%) delete mode 100644 spec/spec_helper.rb diff --git a/.simplecov b/.simplecov new file mode 100644 index 0000000..4f3baca --- /dev/null +++ b/.simplecov @@ -0,0 +1 @@ +SimpleCov.start diff --git a/README.md b/README.md index 097d41d..642c53f 100644 --- a/README.md +++ b/README.md @@ -24,34 +24,33 @@ descriptive statistics of Numeric sample data in collections that have included When requiring DescriptiveStatistics, the Enumerable module is monkey patched so that the statistical methods are available on any instance of a class that has included Enumerable. For example with an Array: ``` -> require 'descriptive_statistics' - => true -> data = [2,6,9,3,5,1,8,3,6,9,2] - => [2, 6, 9, 3, 5, 1, 8, 3, 6, 9, 2] -> data.number - => 11.0 -> data.sum - => 54.0 -> data.mean - => 4.909090909090909 -> data.median - => 5.0 -> data.variance - => 7.7190082644628095 -> data.standard_deviation - => 2.778310325442932 -> data.percentile(30) - => 3.0 -> data.percentile(70) - => 6.0 -> data.percentile_rank(8) - => 81.81818181818183 -> data.mode - => 2 -> data.range - => 8 -> data.descriptive_statistics - => {:number=>11.0, +require 'descriptive_statistics' +data = [2,6,9,3,5,1,8,3,6,9,2] +# => [2, 6, 9, 3, 5, 1, 8, 3, 6, 9, 2] +data.number +# => 11.0 +data.sum +# => 54.0 +data.mean +# => 4.909090909090909 +data.median +# => 5.0 +data.variance +# => 7.7190082644628095 +data.standard_deviation +# => 2.778310325442932 +data.percentile(30) +# => 3.0 +data.percentile(70) +# => 6.0 +data.percentile_rank(8) +# => 81.81818181818183 +data.mode +# => 2 +data.range +# => 8 +data.descriptive_statistics +# => {:number=>11.0, :sum=>54, :variance=>7.7190082644628095, :standard_deviation=>2.778310325442932, @@ -68,16 +67,14 @@ that the statistical methods are available on any instance of a class that has i and with other types of objects: ``` -> require 'set' -=> true -> require 'descriptive_statistics' -=> true -> {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}.mean #Hash -=> 3.0 -> Set.new([1,2,3,4,5]).mean #Set -=> 3.0 -> (1..5).mean #Range -=> 3.0 +require 'set' +require 'descriptive_statistics' +{:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}.mean #Hash +# => 3.0 +Set.new([1,2,3,4,5]).mean #Set +# => 3.0 +(1..5).mean #Range +# => 3.0 ``` including instances of your own classes, when an `each` method is provided that @@ -97,7 +94,7 @@ foo.bar = 1 foo.baz = 2 foo.bat = 3 foo.mean -=> 2.0 +# => 2.0 ``` @@ -121,7 +118,7 @@ foo.bar = 1 foo.baz = 2 foo.bat = 3 foo.mean -=> 2.0 +# => 2.0 ``` and even Structs: @@ -132,72 +129,90 @@ bowling.sally = 203 bowling.john = 134 bowling.peter = 233 bowling.mean -=> 190.0 +# => 190.0 ``` All methods optionally take blocks that operate on object values. For example: ``` -> require 'descriptive_statistics' -> LineItem = Struct.new(:price, :quantity) -> cart = [ LineItem.new(2.50, 2), LineItem.new(5.10, 9), LineItem.new(4.00, 5) ] -> total_items = cart.sum(&:quantity) - => 16 -> total_price = cart.sum{ |i| i.price * i.quantity } - => 70.9 +require 'descriptive_statistics' +LineItem = Struct.new(:price, :quantity) +cart = [ LineItem.new(2.50, 2), LineItem.new(5.10, 9), LineItem.new(4.00, 5) ] +total_items = cart.sum(&:quantity) +# => 16 +total_price = cart.sum{ |i| i.price * i.quantity } +# => 70.9 ``` Note that you can extend DescriptiveStatistics on individual objects by requiring DescriptiveStatistics safely, thus avoiding the monkey patch. For example: ``` -> require 'descriptive_statistics/safe' - => true -> data = [2,6,9,3,5,1,8,3,6,9,2] - => [2, 6, 9, 3, 5, 1, 8, 3, 6, 9, 2] -> data.extend(DescriptiveStatistics) - => [2, 6, 9, 3, 5, 1, 8, 3, 6, 9, 2] -> data.number - => 11.0 -> data.sum - => 54 +require 'descriptive_statistics/safe' +data = [2,6,9,3,5,1,8,3,6,9,2] +# => [2, 6, 9, 3, 5, 1, 8, 3, 6, 9, 2] +data.extend(DescriptiveStatistics) +# => [2, 6, 9, 3, 5, 1, 8, 3, 6, 9, 2] +data.number +# => 11.0 +data.sum +# => 54 ``` Or, if you prefer leaving your collection pristine, you can create a Stats object that references your collection: ``` -> require 'descriptive_statistics/safe' - => true -> data = [1, 2, 3, 4, 5, 1] - => [1, 2, 3, 4, 5, 1] -> stats = DescriptiveStatistics::Stats.new(data) - => [1, 2, 3, 4, 5, 1] -> stats.class - => DescriptiveStatistics::Stats -> stats.mean - => 2.6666666666666665 -> stats.median - => 2.5 -> stats.mode - => 1 -> data << 2 - => [1, 2, 3, 4, 5, 1, 2] -> data << 2 - => [1, 2, 3, 4, 5, 1, 2, 2] -> stats.mode - => 2 +require 'descriptive_statistics/safe' +data = [1, 2, 3, 4, 5, 1] +# => [1, 2, 3, 4, 5, 1] +stats = DescriptiveStatistics::Stats.new(data) +# => [1, 2, 3, 4, 5, 1] +stats.class +# => DescriptiveStatistics::Stats +stats.mean +# => 2.6666666666666665 +stats.median +# => 2.5 +stats.mode +# => 1 +data << 2 +# => [1, 2, 3, 4, 5, 1, 2] +data << 2 +3 =[1, 2, 3, 4, 5, 1, 2, 2] +stats.mode +# => 2 ``` Or you call the statistical methods directly: ``` -> require 'descriptive_statistics/safe' - => true -> DescriptiveStatistics.mean([1,2,3,4,5]) - => 3.0 -> DescriptiveStatistics.mode([1,2,3,4,5]) - => 1 -> DescriptiveStatistics.variance([1,2,3,4,5]) - => 2.0 +require 'descriptive_statistics/safe' +# => true +DescriptiveStatistics.mean([1,2,3,4,5]) +# => 3.0 +DescriptiveStatistics.mode([1,2,3,4,5]) +# => 1 +DescriptiveStatistics.variance([1,2,3,4,5]) +# => 2.0 ``` +Or you can use [Refinements](http://www.ruby-doc.org/core/doc/syntax/refinements_rdoc.html) (available in Ruby >= 2.1) to augment any class that mixes in the Enumerable module. Refinements are lexically scoped and so the statistical methods will only be available in the file where they are used. Note that the lexical scope can be limited to a Class or Module, but only applies to code in that file. This approach provides a great deal of protection against introducing conflicting modifications to Enumerable while retaining the convenience of the monkey patch approach. +``` +require 'descriptive_statistics/refinement' + +class SomeServiceClass + using DescriptiveStatistics::Refinement.new(Array) + + def self.calculate_something(array) + array.standard_deviation + end +end + +[1,2,3].standard_deviation +# => NoMethodError: undefined method `standard_deviation' for [1, 2, 3]:Array + +SomeServiceClass.calculate_something([1,2,3]) +#=> 0.816496580927726 +``` + + Ruby on Rails ------------- @@ -220,39 +235,37 @@ Notes * All methods return a Float object except for `mode`, which will return a Numeric object from the collection. `mode` will always return nil for empty collections. * All methods return nil when the collection is empty, except for `number`, which returns 0.0. This is a different behavior than [ActiveSupport's Enumerable monkey patch of sum](http://apidock.com/rails/Enumerable/sum), which by deafult returns the Fixnum 0 for empty collections. You can change this behavior by specifying the default value returned for empty collections all at once: ``` -> require 'descriptive_statistics' - => true -> [].mean - => nil -> [].sum - => nil -> DescriptiveStatistics.empty_collection_default_value = 0.0 - => 0.0 -> [].mean - => 0.0 -> [].sum - => 0.0 +require 'descriptive_statistics' +[].mean +# => nil +[].sum +# => nil +DescriptiveStatistics.empty_collection_default_value = 0.0 +# => 0.0 +[].mean +# => 0.0 +[].sum +# => 0.0 ``` or one at a time: ``` -> require 'descriptive_statistics' - => true -> [].mean - => nil -> [].sum - => nil -> DescriptiveStatistics.sum_empty_collection_default_value = 0.0 - => 0.0 -> [].mean - => nil -> [].sum - => 0.0 -> DescriptiveStatistics.mean_empty_collection_default_value = 0.0 - => 0.0 -> [].mean - => 0.0 -> [].sum - => 0.0 +require 'descriptive_statistics' +[].mean +# => nil +[].sum +# => nil +DescriptiveStatistics.sum_empty_collection_default_value = 0.0 +# => 0.0 +[].mean +# => nil +[].sum +# => 0.0 +DescriptiveStatistics.mean_empty_collection_default_value = 0.0 +# => 0.0 +[].mean +# => 0.0 +[].sum +# => 0.0 ``` * The scope of this gem covers [Descriptive Statistics](http://en.wikipedia.org/wiki/Descriptive_statistics) and not Inferential Statistics. From wikipedia: diff --git a/Rakefile b/Rakefile index 38d739f..97aab99 100644 --- a/Rakefile +++ b/Rakefile @@ -1,13 +1,32 @@ require 'bundler/setup' require 'bundler/gem_tasks' require 'rake/testtask' -require 'rspec/core/rake_task' Rake::TestTask.new do |t| t.test_files = FileList['test/*.rb'] t.verbose = true end -RSpec::Core::RakeTask.new(:spec) +begin + require 'rspec/core/rake_task' -task :default => [ :test, :spec ] + RSpec::Core::RakeTask.new(:refinement) do |task| + task.rspec_opts = "--order rand" + task.pattern = "spec/refinement/*_spec.rb" + end + + RSpec::Core::RakeTask.new(:monkeypatch) do |task| + task.rspec_opts = "--order rand" + task.pattern = "spec/monkeypatch/*_spec.rb" + end + + RSpec::Core::RakeTask.new(:safe) do |task| + task.rspec_opts = "--order rand" + task.pattern = "spec/safe/*_spec.rb" + end + +rescue LoadError + warn "rspec unavailable" +end + +task :default => [ :test, :monkeypatch, :refinement, :safe ] diff --git a/descriptive_statistics.gemspec b/descriptive_statistics.gemspec index 2333398..3f70947 100644 --- a/descriptive_statistics.gemspec +++ b/descriptive_statistics.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'descriptive_statistics' - s.version = '2.4.0' + s.version = '2.5.0' s.homepage = 'https://github.com/thirtysixthspan/descriptive_statistics' s.summary = 'Descriptive Statistics' s.description = 'Adds descriptive statistics methods to Enumerable module for use on collections or Numeric data' diff --git a/lib/descriptive_statistics/enumerable_extension.rb b/lib/descriptive_statistics/enumerable_extension.rb index 814933a..477caca 100644 --- a/lib/descriptive_statistics/enumerable_extension.rb +++ b/lib/descriptive_statistics/enumerable_extension.rb @@ -1,11 +1,8 @@ module Enumerable - include DescriptiveStatistics - # This is necessary because otherwise objects which - # have already included Enumerable (such as Array) won't - # be able to access DescriptiveStatistics's methods. - # It is an evil hack though :-/ - DescriptiveStatistics.instance_methods.each do |m| - define_method(m, DescriptiveStatistics.instance_method(m)) + DescriptiveStatistics.instance_methods.each do |name| + method = DescriptiveStatistics.instance_method(name) + define_method(name, method) end + end diff --git a/lib/descriptive_statistics/refinement.rb b/lib/descriptive_statistics/refinement.rb new file mode 100644 index 0000000..455a300 --- /dev/null +++ b/lib/descriptive_statistics/refinement.rb @@ -0,0 +1,33 @@ +require "descriptive_statistics/safe" + +module DescriptiveStatistics + + module Refinement + + def self.new(*klasses) + refinement_module = Module.new + + klasses.each do |klass| + + refinement_module.instance_eval do + + refine klass do + + DescriptiveStatistics.instance_methods.each do |name| + method = DescriptiveStatistics.instance_method(name) + define_method(name, method) + end + + end + + end + + end + + return refinement_module + + end + + end + +end \ No newline at end of file diff --git a/pkg/descriptive_statistics-2.4.0.gem b/pkg/descriptive_statistics-2.4.0.gem deleted file mode 100644 index 0aa9c2a53ecd509ff4ed5560eacaa2ad4c9b471a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6144 zcmeHKc|6qZ79UIYQIuw- z#{`8!LErf?{m3P+T!DfluRx_FVbaHX(jW;*sqgB%5`UCIzrWWDc$N#Qdo)|ii(44&?B^5SB^Zu~Jsg8{!_WabWU8AlGtdG<+u`lWL zFcjgr(Fz%9{}70M@k(rXqfdsg4$(`GV4O%{?SLoPA1yrB5@OqbRPQ9kIAUV>rcTj> z-Q<#JC6pnF9_j05dNWS%iDR;v4JTVa#@3MiUS6lJdqPS=+tWl3+80_n1*k#uo85{f ziGFb*;#WiO@CkimijFn^=%ZMak4_^_pRne35U|uIu1itP@R5p#EE!22t<7kAodlxf$@5O#`;%)ZdHB&t#90ecsVh^B|9f9aA58jb z^gu@9ybs5A$5Q#D{N47kB?g!lw!yMO!C#d4R+d+-Z`~7L^dO7PLv)Y4DLkaA^g)F7 z@A^~zH|_ct_8%kQUx@$5U=RBd|D|A3Kk@(1$hhC||0@DO$Ru6t z2w^EbH$XAfmQ_)kE8!3wU;Sl1Nnbo>dsbcF$E?D(!>Br5#T+p_C!6KFeOgY$Lp)Kk<$U#n zjjuZ@H8ZdR2w;-`P~=5}jOqSpQI!(a(bXHx;`5ecMmGFgv#O{EVxAI#J;>0h9k7hT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hQS!*U(grOx#| zoMo|ir5gT@Q&0%>yhwik5_92eG+QL+C2x_E-0o!d)yZJ_X0NEDe!J_<#Q@B>wh!2x z(g*h10L`M}1}{ps@+iMBz4u=AKu5{rjEBl7JrQ$zmnW2Yug z*V?Hi@|xG@%VVqfAm9Bq%j-=WV{wxP54@7JLs5{ns#EKB?L)Ee*yi1Imgg*WT*1f| zl__1Rh|dZ=a{kyvN@&;_!_9k3p+KWloPelYXwq~G2T>{fMIWvuOGH9Huw6y5R2$jb z?Xb#UlPoX)34+(CPG^w=KMQBDg_$27w-`0T`Pn%tlaeE6LV+VHS%jd>L&wLF<~MG= zS#Fg?^-FaMgZz#4CL}d)+^x{BUXib0@xPOrX04x6=VW`M$JKa6^(=l#ImI=G>(Dpd eKz*@^1QLeUSLc5J@jvNj1b#-~X9WI_BJfX_w9|V4 diff --git a/pkg/descriptive_statistics-2.5.0.gem b/pkg/descriptive_statistics-2.5.0.gem new file mode 100644 index 0000000000000000000000000000000000000000..26bf2e08f7d3545e7b4fb634338c3c41ad9e7771 GIT binary patch literal 6144 zcmeHKc|6qJ8lMRfA~T}MPRKTzF@i+`J+}o4o8BNkMDXQkt#6dUAr4nU z7Z=ap8vfA!U-p0J_G@x~82(r7io~;l*gR6q_b@$8oAjZ;4m_LOvC3?xRi3wsjuH>w ztW&1Lq7HooM-|5t&;}Uw+`M{rI?3N~C@{8;*IrNVDeBCbNkVN<-fP!o@Y>+fjX{3% zx)u#vUMrzv4G8vPy5Y$O7xfY~lRp<*V-AZiMY)?Fz?PDYePhzNXVP!`u`1N-j@scA zy33?(pW}2iL&+1fDZ3`_ZX0dI7niuh7&-tmON8Q)qBQLEqqP)~W{nyhn z`p2?iV*EQg_9_+GvE7u6{l-}1{Nbs$UitsP`ou;;(tO8^#kLf9P30|^qJ?%=t=(e{ zV)#rkz(csjGU$_u!;k*$=SKur36qhfG6W&PSb;GTQ3CRvYT9P@B7_D6VxcwB6#_nWk=cS8Pz-w_^v*g$1>J zp|1iwwO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jIHitFzmLa1c@4WxviTLeE{_cY1Pc>P*s^$l3nt c&@Hg3eK)cg{5x2Fc77u86M>%y{Ou9=C+TI${Qv*} literal 0 HcmV?d00001 diff --git a/spec/array_spec.rb b/spec/monkeypatch/array_spec.rb similarity index 97% rename from spec/array_spec.rb rename to spec/monkeypatch/array_spec.rb index 21e9740..6f65437 100644 --- a/spec/array_spec.rb +++ b/spec/monkeypatch/array_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'monkeypatch/spec_helper' describe "DescriptiveStatistics" do require 'descriptive_statistics' diff --git a/spec/empty_collection_spec.rb b/spec/monkeypatch/empty_collection_spec.rb similarity index 97% rename from spec/empty_collection_spec.rb rename to spec/monkeypatch/empty_collection_spec.rb index 698b883..4a74b73 100644 --- a/spec/empty_collection_spec.rb +++ b/spec/monkeypatch/empty_collection_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'monkeypatch/spec_helper' describe "DescriptiveStatistics" do require 'descriptive_statistics' @@ -7,6 +7,10 @@ context "with a default of nil" do + before do + DescriptiveStatistics.empty_collection_default_value = nil + end + it "calculates the number" do expect(subject.number).to eql(0.0) end diff --git a/spec/enumerable_spec.rb b/spec/monkeypatch/enumerable_spec.rb similarity index 95% rename from spec/enumerable_spec.rb rename to spec/monkeypatch/enumerable_spec.rb index 658f49e..285a0e8 100644 --- a/spec/enumerable_spec.rb +++ b/spec/monkeypatch/enumerable_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'monkeypatch/spec_helper' describe "DescriptiveStatistics" do require 'descriptive_statistics' diff --git a/spec/hash_spec.rb b/spec/monkeypatch/hash_spec.rb similarity index 97% rename from spec/hash_spec.rb rename to spec/monkeypatch/hash_spec.rb index 02a0bb0..0422d5f 100644 --- a/spec/hash_spec.rb +++ b/spec/monkeypatch/hash_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'monkeypatch/spec_helper' describe "DescriptiveStatistics" do require 'descriptive_statistics' diff --git a/spec/object_spec.rb b/spec/monkeypatch/object_spec.rb similarity index 98% rename from spec/object_spec.rb rename to spec/monkeypatch/object_spec.rb index 14be6a8..574a1ef 100644 --- a/spec/object_spec.rb +++ b/spec/monkeypatch/object_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'monkeypatch/spec_helper' require 'ostruct' describe "DescriptiveStatistics" do diff --git a/spec/set_spec.rb b/spec/monkeypatch/set_spec.rb similarity index 97% rename from spec/set_spec.rb rename to spec/monkeypatch/set_spec.rb index d80fbfe..77e5951 100644 --- a/spec/set_spec.rb +++ b/spec/monkeypatch/set_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'monkeypatch/spec_helper' describe "DescriptiveStatistics" do require 'descriptive_statistics' diff --git a/spec/single_value_spec.rb b/spec/monkeypatch/single_value_spec.rb similarity index 97% rename from spec/single_value_spec.rb rename to spec/monkeypatch/single_value_spec.rb index 2001765..1c09711 100644 --- a/spec/single_value_spec.rb +++ b/spec/monkeypatch/single_value_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'monkeypatch/spec_helper' describe "DescriptiveStatistics" do require 'descriptive_statistics' diff --git a/spec/monkeypatch/spec_helper.rb b/spec/monkeypatch/spec_helper.rb new file mode 100644 index 0000000..e5aeb60 --- /dev/null +++ b/spec/monkeypatch/spec_helper.rb @@ -0,0 +1,2 @@ +require 'simplecov' +SimpleCov.command_name 'rspec:monkeypatch' \ No newline at end of file diff --git a/spec/refinement/refinement_spec.rb b/spec/refinement/refinement_spec.rb new file mode 100644 index 0000000..bbbd24d --- /dev/null +++ b/spec/refinement/refinement_spec.rb @@ -0,0 +1,27 @@ +require 'refinement/spec_helper' + +describe "DescriptiveStatistics" do + require 'descriptive_statistics/refinement' + + it "is refinable" do + + class Test + def self.unrefined + [1, 2, 3].mean + {a:1, b:2, c:3}.mean + end + + using DescriptiveStatistics::Refinement.new(Array, Hash) + + def self.refined + [1,2,3].mean + {a:1, b:2, c:3}.mean + end + end + + expect{Test.unrefined}.to raise_error + expect{Test.refined}.not_to raise_error + + end + +end \ No newline at end of file diff --git a/spec/refinement/spec_helper.rb b/spec/refinement/spec_helper.rb new file mode 100644 index 0000000..193aac8 --- /dev/null +++ b/spec/refinement/spec_helper.rb @@ -0,0 +1,2 @@ +require 'simplecov' +SimpleCov.command_name 'rspec:refinement' \ No newline at end of file diff --git a/spec/class_method_spec.rb b/spec/safe/class_method_spec.rb similarity index 95% rename from spec/class_method_spec.rb rename to spec/safe/class_method_spec.rb index d7ae5ca..53d93a3 100644 --- a/spec/class_method_spec.rb +++ b/spec/safe/class_method_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require 'safe/spec_helper' -describe "DescriptiveStatistics::Stats" do +describe "DescriptiveStatistics" do require 'descriptive_statistics/safe' context "calculated using class methods" do diff --git a/spec/extend_spec.rb b/spec/safe/extend_spec.rb similarity index 98% rename from spec/extend_spec.rb rename to spec/safe/extend_spec.rb index dc25240..a34356f 100644 --- a/spec/extend_spec.rb +++ b/spec/safe/extend_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'safe/spec_helper' describe "DescriptiveStatistics" do require 'descriptive_statistics/safe' diff --git a/spec/safe/spec_helper.rb b/spec/safe/spec_helper.rb new file mode 100644 index 0000000..f9d9326 --- /dev/null +++ b/spec/safe/spec_helper.rb @@ -0,0 +1,2 @@ +require 'simplecov' +SimpleCov.command_name 'rspec:safe' \ No newline at end of file diff --git a/spec/stats_spec.rb b/spec/safe/stats_spec.rb similarity index 95% rename from spec/stats_spec.rb rename to spec/safe/stats_spec.rb index 2c9bff7..1cd3d57 100644 --- a/spec/stats_spec.rb +++ b/spec/safe/stats_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require 'safe/spec_helper' -describe "DescriptiveStatistics::Stats" do +describe "DescriptiveStatistics" do require 'descriptive_statistics/safe' context "calculated from a Stats object" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb deleted file mode 100644 index 76e1b4e..0000000 --- a/spec/spec_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -require 'simplecov' -SimpleCov.start \ No newline at end of file diff --git a/test/test.rb b/test/test.rb index 8a395c3..01115b5 100644 --- a/test/test.rb +++ b/test/test.rb @@ -1,9 +1,10 @@ require 'simplecov' -SimpleCov.start require 'minitest/autorun' require 'csv' require './lib/descriptive_statistics' +SimpleCov.command_name 'minitest' + class TestData < MiniTest::Unit::TestCase def setup