webmat-mini_facet 0.0.1
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/MIT-LICENSE +20 -0
- data/README.markdown +26 -0
- data/Rakefile +14 -0
- data/lib/mini_facet.rb +7 -0
- data/lib/mini_facet/array.rb +3 -0
- data/lib/mini_facet/array/count_distinct.rb +14 -0
- data/lib/mini_facet/array/includes_all.rb +17 -0
- data/lib/mini_facet/hash.rb +3 -0
- data/lib/mini_facet/hash/extract.rb +51 -0
- data/lib/mini_facet/hash/split.rb +19 -0
- data/lib/mini_facet/version.rb +12 -0
- data/tasks/gem.rake +67 -0
- data/tasks/rdoc.rake +10 -0
- data/tasks/test.rake +21 -0
- data/test/test_helper.rb +7 -0
- data/test/unit/array/count_distinct_test.rb +21 -0
- data/test/unit/array/includes_all_test.rb +31 -0
- data/test/unit/hash/extract_test.rb +77 -0
- data/test/unit/hash/split_test.rb +31 -0
- data/test/unit/test_helper.rb +3 -0
- metadata +122 -0
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2008 Mathieu Martin
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
MiniFacet
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
This is a small library to add a few basic niceties to Ruby. It's not meant to
|
|
5
|
+
be all-encompassing and include tons of things like Ruby Facets, however.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Example
|
|
9
|
+
=======
|
|
10
|
+
|
|
11
|
+
require 'mini_facet'
|
|
12
|
+
# Will include everything
|
|
13
|
+
# Including piece by piece is coming, but is not yet done
|
|
14
|
+
|
|
15
|
+
Right now, mini_facet only adds the following:
|
|
16
|
+
Hash#extract([]) #=> Hash
|
|
17
|
+
Hash#extract(&block) #=> Hash
|
|
18
|
+
Hash#extract(proc) #=> Hash
|
|
19
|
+
Hash#split(&block) #=> Hash(block true), Hash(block false)
|
|
20
|
+
Hash#split(proc) #=> Hash(proc true), Hash(proc false)
|
|
21
|
+
Array#count_distinct #=> Hash with the count of each element, e.g. [:a, :a] => {:a => 2}
|
|
22
|
+
Array#includes_all?([]) #=> true/false
|
|
23
|
+
Array#include_all? #Just replicating Ruby's typos ;-)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Copyright (c) 2008 Mathieu Martin, released under the MIT license
|
data/Rakefile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
|
|
3
|
+
require 'rake'
|
|
4
|
+
|
|
5
|
+
HERE = File.dirname(__FILE__)
|
|
6
|
+
windows = (RUBY_PLATFORM =~ /win32|cygwin/) rescue nil
|
|
7
|
+
SUDO = windows ? "" : "sudo"
|
|
8
|
+
|
|
9
|
+
require "#{HERE}/lib/mini_facet"
|
|
10
|
+
Dir['tasks/**/*.rake'].each { |rake| load rake }
|
|
11
|
+
|
|
12
|
+
desc 'Default: run all tests.'
|
|
13
|
+
task :default => :test
|
|
14
|
+
|
data/lib/mini_facet.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Can be included in any class that responds to #each.
|
|
2
|
+
# Such as Array.
|
|
3
|
+
module MiniFacet::CountDistinct
|
|
4
|
+
def count_distinct(purge_smaller_than=0)
|
|
5
|
+
h={}
|
|
6
|
+
self.each {|e|
|
|
7
|
+
h[e] ? h[e] += 1 : h[e] = 1
|
|
8
|
+
}
|
|
9
|
+
h.extract{|k,v| v >= purge_smaller_than}
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Array.send :include, MiniFacet::CountDistinct
|
|
14
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module MiniFacet::IncludesAll
|
|
2
|
+
def includes_all?(ary)
|
|
3
|
+
our_count = self.count_distinct
|
|
4
|
+
their_count = ary.count_distinct
|
|
5
|
+
|
|
6
|
+
their_count.each_pair do |k,v|
|
|
7
|
+
our_v = our_count[k]
|
|
8
|
+
return false if our_v.nil? || our_v < v
|
|
9
|
+
end
|
|
10
|
+
return true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
alias :include_all? :includes_all? #Replicating Ruby's typos
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
Array.send :include, MiniFacet::IncludesAll
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module MiniFacet::Extract
|
|
2
|
+
# Hash#extract([keys]) => Hash
|
|
3
|
+
# Hash#extract{|k,v| predicate } => Hash
|
|
4
|
+
# Hash#extract(proc) => Hash
|
|
5
|
+
#
|
|
6
|
+
# Returns a new Hash that contains only the k,v pairs where the k was
|
|
7
|
+
# specified in the keys array.
|
|
8
|
+
# If any k in keys is not present in the original Hash, it's simply
|
|
9
|
+
# not the resulting Hash.
|
|
10
|
+
#
|
|
11
|
+
# This is very useful to check that a Hash contains at least some desired keys
|
|
12
|
+
# or to get a sanitized Hash out of the one we currently have.
|
|
13
|
+
#
|
|
14
|
+
# Examples:
|
|
15
|
+
# h = {:bob=>'Marley',:mom=>'Barley'}
|
|
16
|
+
# h.extract([:bob]) #=> {:bob=>'Marley'}
|
|
17
|
+
# h.extract([:bob, :mom]) #=> {:bob=>'Marley',:mom=>'Barley'}
|
|
18
|
+
# h.extract([:sos]) #=> {}
|
|
19
|
+
#
|
|
20
|
+
# h.extract{|k,v| v =~ /Mar/} #=>{:bob=>'Marley}
|
|
21
|
+
|
|
22
|
+
def extract(*args, &block)
|
|
23
|
+
# Despide the *args, extract doesn't accept a splatted array.
|
|
24
|
+
# It's really either an array or some form of block/proc.
|
|
25
|
+
if block_given?
|
|
26
|
+
extract_block(&block)
|
|
27
|
+
elsif args[0].is_a? Proc
|
|
28
|
+
extract_block(&args[0])
|
|
29
|
+
elsif args[0].is_a? Array
|
|
30
|
+
extract_keys(args[0])
|
|
31
|
+
else
|
|
32
|
+
raise ArgumentError, "extract requires either an array of keys, a block or a proc"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
def extract_keys(keys)
|
|
38
|
+
extracted = self.class.new #Can therefore be included in any hash-like container
|
|
39
|
+
keys.each { |k| extracted[k] = self[k] if self.include?(k) }
|
|
40
|
+
extracted
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def extract_block(&block)
|
|
44
|
+
extracted = self.class.new
|
|
45
|
+
each_pair{ |k,v| extracted[k] = v if yield(k,v) }
|
|
46
|
+
extracted
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Hash.send :include, MiniFacet::Extract
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module MiniFacet::Split
|
|
2
|
+
# Returns two hashes. The first contains all pairs for which the block evaluated to true,
|
|
3
|
+
# the second contains all the others.
|
|
4
|
+
def split(&block)
|
|
5
|
+
trues, falses = self.class.new, self.class.new
|
|
6
|
+
each_pair do |k,v|
|
|
7
|
+
if yield(k,v)
|
|
8
|
+
trues[k] = v
|
|
9
|
+
else
|
|
10
|
+
falses[k] = v
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
#each_pair{ |k,v| (yield(k,v) ? trues : falses)[k] = v }
|
|
14
|
+
return trues, falses
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Hash.send :include, MiniFacet::Split
|
data/tasks/gem.rake
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
3
|
+
require 'rake/gempackagetask'
|
|
4
|
+
|
|
5
|
+
task :clean => :clobber_package
|
|
6
|
+
|
|
7
|
+
spec = Gem::Specification.new do |s|
|
|
8
|
+
s.name = MiniFacet::NAME
|
|
9
|
+
s.version = MiniFacet::VERSION::STRING
|
|
10
|
+
s.summary = "Adding a few basic, useful things to Ruby."
|
|
11
|
+
s.description = "Adding a few basic, useful things to Ruby."
|
|
12
|
+
|
|
13
|
+
s.authors = ['Mathieu Martin']
|
|
14
|
+
s.email = "webmat@gmail.com"
|
|
15
|
+
s.homepage = "http://github.com/webmat/mini_facet"
|
|
16
|
+
s.rubyforge_project = 'mini_facet'
|
|
17
|
+
|
|
18
|
+
s.has_rdoc = true
|
|
19
|
+
|
|
20
|
+
s.test_files = Dir['test/**/*'].reject{|f| f =~ /test\/my_test_helper.rb/}
|
|
21
|
+
s.files = Dir['**/*'].reject{|f| f =~ /\Apkg|\Acoverage|\.gemspec\Z|test\/my_test_helper.rb/}
|
|
22
|
+
|
|
23
|
+
s.require_path = "lib"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
#Creates clobber_package, gem, package and repackage tasks
|
|
27
|
+
Rake::GemPackageTask.new(spec) do |p|
|
|
28
|
+
p.gem_spec = spec
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
TAG_COMMAND = "git tag -m 'Tagging version #{MiniFacet::VERSION::STRING}' -a v#{MiniFacet::VERSION::STRING}"
|
|
32
|
+
task :tag_warn do
|
|
33
|
+
puts "*" * 40,
|
|
34
|
+
"Don't forget to tag the release:",
|
|
35
|
+
'',
|
|
36
|
+
" " + TAG_COMMAND,
|
|
37
|
+
'',
|
|
38
|
+
"or run rake tag",
|
|
39
|
+
"*" * 40
|
|
40
|
+
end
|
|
41
|
+
task :tag do
|
|
42
|
+
sh TAG_COMMAND
|
|
43
|
+
end
|
|
44
|
+
task :gem => :tag_warn
|
|
45
|
+
|
|
46
|
+
namespace :gem do
|
|
47
|
+
desc "Update the gemspec for GitHub's gem server"
|
|
48
|
+
task :github do
|
|
49
|
+
File.open("#{MiniFacet::NAME}.gemspec", 'w'){|f| f.puts YAML::dump(spec) }
|
|
50
|
+
puts "gemspec generated here: #{MiniFacet::NAME}.gemspec"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
desc 'Upload gem to rubyforge.org'
|
|
54
|
+
task :rubyforge => :gem do
|
|
55
|
+
sh 'rubyforge login'
|
|
56
|
+
sh "rubyforge add_release mini_facet mini_facet '#{MiniFacet::VERSION::STRING}' pkg/#{spec.full_name}.gem"
|
|
57
|
+
sh "rubyforge add_file mini_facet mini_facet #{MiniFacet::VERSION::STRING} pkg/#{spec.full_name}.gem"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
task :install => [:clean, :gem] do
|
|
62
|
+
sh "#{SUDO} gem install pkg/#{spec.full_name}.gem"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
task :uninstall do
|
|
66
|
+
sh "#{SUDO} gem uninstall -v #{MiniFacet::VERSION::STRING} -x #{MiniFacet::NAME}"
|
|
67
|
+
end
|
data/tasks/rdoc.rake
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require 'rake/rdoctask'
|
|
2
|
+
|
|
3
|
+
desc 'Generate documentation for mini_facet.'
|
|
4
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
5
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
6
|
+
rdoc.title = 'MiniFacet'
|
|
7
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
|
8
|
+
rdoc.rdoc_files.include('README.markdown')
|
|
9
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
10
|
+
end
|
data/tasks/test.rake
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'rake/testtask'
|
|
2
|
+
|
|
3
|
+
desc "Run all tests"
|
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
|
5
|
+
t.pattern = 'test/**/*_test.rb'
|
|
6
|
+
t.verbose = true
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
namespace :test do
|
|
10
|
+
# desc "Run integration tests"
|
|
11
|
+
# Rake::TestTask.new(:integration) do |t|
|
|
12
|
+
# t.pattern = 'test/integration/**/*_test.rb'
|
|
13
|
+
# t.verbose = true
|
|
14
|
+
# end
|
|
15
|
+
|
|
16
|
+
desc "Run unit tests"
|
|
17
|
+
Rake::TestTask.new(:unit) do |t|
|
|
18
|
+
t.pattern = 'test/unit/**/*_test.rb'
|
|
19
|
+
t.verbose = true
|
|
20
|
+
end
|
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "#{ File.dirname(__FILE__) }/../test_helper"
|
|
2
|
+
|
|
3
|
+
class ArrayCountDistinctTest < Test::Unit::TestCase
|
|
4
|
+
context "Array#count_distinct" do
|
|
5
|
+
should "work with empty arrays" do
|
|
6
|
+
assert_equal({}, [].count_distinct)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
should "calculate the sums correctly" do
|
|
10
|
+
assert_equal({:key => 2, 'key2' => 1}, [:key, 'key2', :key].count_distinct)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
should "support and count nil elements" do
|
|
14
|
+
assert_equal({:key => 2, nil => 3}, [nil, :key, nil, nil, :key].count_distinct)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
should "filter out elements with a low count based on purge_smaller_than" do
|
|
18
|
+
assert_equal({:key => 2}, [:key, 'key', nil, :key].count_distinct(2))
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require "#{ File.dirname(__FILE__) }/../test_helper"
|
|
2
|
+
|
|
3
|
+
class ArrayIncludesAllTest < Test::Unit::TestCase
|
|
4
|
+
def self.test_includes_all(options)
|
|
5
|
+
context "#{ options[:array].inspect }.includes_all?(#{ options[:includes_all?].inspect })" do
|
|
6
|
+
should "return #{options[:returns]}" do
|
|
7
|
+
assert_equal options[:returns], options[:array].includes_all?(options[:includes_all?])
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
test_includes_all :array => [], :includes_all? => [], :returns => true # Same as Ruby's Set
|
|
13
|
+
test_includes_all :array => [], :includes_all? => [:a], :returns => false
|
|
14
|
+
|
|
15
|
+
test_includes_all :array => [:a], :includes_all? => [], :returns => true # Same as Ruby's Set
|
|
16
|
+
test_includes_all :array => [:a], :includes_all? => [:a], :returns => true
|
|
17
|
+
test_includes_all :array => [:a], :includes_all? => [:a, :a], :returns => false
|
|
18
|
+
test_includes_all :array => [:a, :a], :includes_all? => [:a], :returns => true
|
|
19
|
+
test_includes_all :array => [:a, :a], :includes_all? => [:a, :a], :returns => true
|
|
20
|
+
|
|
21
|
+
test_includes_all :array => [:a, :b], :includes_all? => [:a], :returns => true
|
|
22
|
+
test_includes_all :array => [:a, :b], :includes_all? => [:a, :b], :returns => true
|
|
23
|
+
test_includes_all :array => [:a, :b], :includes_all? => [:b, :a], :returns => true
|
|
24
|
+
|
|
25
|
+
test_includes_all :array => [[:a], [:b]], :includes_all? => [[:a]], :returns => true
|
|
26
|
+
test_includes_all :array => [[:a], [:b]], :includes_all? => [[:a], [:b]], :returns => true
|
|
27
|
+
|
|
28
|
+
test_includes_all :array => [[:a], [:b]], :includes_all? => [:a, :b], :returns => false
|
|
29
|
+
test_includes_all :array => [[:a, :b]], :includes_all? => [:a, :b], :returns => false
|
|
30
|
+
test_includes_all :array => [:a, :b], :includes_all? => [[:a, :b]], :returns => false
|
|
31
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require "#{ File.dirname(__FILE__) }/../test_helper"
|
|
2
|
+
|
|
3
|
+
class HashExtractTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
super
|
|
6
|
+
@h = {
|
|
7
|
+
:bob => 'Marley',
|
|
8
|
+
:mom => 'Barley',
|
|
9
|
+
:gods => 'Harley',
|
|
10
|
+
:chris => 'Farley'
|
|
11
|
+
}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context "extract" do
|
|
15
|
+
context "with a block" do
|
|
16
|
+
|
|
17
|
+
should "return all elements for which the block evaluates to a true value" do
|
|
18
|
+
assert_equal( {:bob => 'Marley', :mom => 'Barley'}, @h.extract{ |k, v| v =~ /\A[M|B]/ })
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
should "ignore the args and go with the block" do
|
|
22
|
+
assert_equal( {:bob => 'Marley', :mom => 'Barley'}, @h.extract([:gods]){ |k, v| v =~ /\A[M|B]/ })
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
should "accept a proc as a normal parameter" do
|
|
27
|
+
proc = Proc.new { |k, v| v =~ /\A[M|B]/ }
|
|
28
|
+
assert_equal( {:bob => 'Marley', :mom => 'Barley'}, @h.extract(proc))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
should "accept a lambda as a normal parameter" do
|
|
32
|
+
l = lambda { |k, v| v =~ /\A[M|B]/ }
|
|
33
|
+
assert_equal( {:bob => 'Marley', :mom => 'Barley'}, @h.extract(l))
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_normal
|
|
38
|
+
assert_equal( {:bob => 'Marley'}, @h.extract([:bob]) )
|
|
39
|
+
assert_equal( {:mom => 'Barley', :bob => 'Marley'}, @h.extract([:bob, :mom]) )
|
|
40
|
+
|
|
41
|
+
all_keys = @h.extract [:bob, :mom, :gods, :chris]
|
|
42
|
+
assert_not_equal( all_keys.object_id, @h.object_id )
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_normal_alternative
|
|
46
|
+
assert_equal( {:bob => 'Marley'}, @h.extract([:bob]) )
|
|
47
|
+
assert_equal( {:mom => 'Barley', :bob => 'Marley'}, @h.extract([:bob, :mom]) )
|
|
48
|
+
|
|
49
|
+
all_extracted = @h.extract([:bob, :mom, :gods, :chris])
|
|
50
|
+
assert_not_equal( all_extracted.object_id, @h.object_id )
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_edge_case
|
|
54
|
+
assert_equal( {}, @h.extract([]) )
|
|
55
|
+
assert_equal( @h, @h.extract([:bob, :mom, :gods, :chris]) )
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_unknown_key
|
|
59
|
+
assert_equal( {}, @h.extract([:ahem]) )
|
|
60
|
+
assert_equal( {:bob => 'Marley'}, @h.extract([:bob, :ahem]) )
|
|
61
|
+
assert_equal( {}, @h.extract(['some other type of key']) )
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_keep_nil_value_in_hash
|
|
65
|
+
#Yes, h2 as in Hummer :-)
|
|
66
|
+
h2 = {:price => 'expensive', :sex_appeal => nil}
|
|
67
|
+
assert_equal( h2, h2.extract([:sex_appeal, :price]) )
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class CustomTestHash < Hash; end
|
|
71
|
+
def test_something_else_than_hash_is_extractable
|
|
72
|
+
m=CustomTestHash.new.merge({:a=>:b,:c=>:d})
|
|
73
|
+
n=m.extract([:a])
|
|
74
|
+
assert_kind_of CustomTestHash, n
|
|
75
|
+
assert_equal( CustomTestHash.new.merge({:a=>:b}), n )
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require "#{ File.dirname(__FILE__) }/../test_helper"
|
|
2
|
+
|
|
3
|
+
class HashSplitTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
super
|
|
6
|
+
@h = {
|
|
7
|
+
:bob => 'Marley',
|
|
8
|
+
:mom => 'Barley',
|
|
9
|
+
:gods => 'Harley',
|
|
10
|
+
:chris => 'Farley'
|
|
11
|
+
}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context "split" do
|
|
15
|
+
should "return two hashes" do
|
|
16
|
+
assert_kind_of Hash, (@h.split{|k,v| false})[0]
|
|
17
|
+
assert_kind_of Hash, (@h.split{|k,v| false})[1]
|
|
18
|
+
end
|
|
19
|
+
context "splits the values" do
|
|
20
|
+
setup do
|
|
21
|
+
@first, @second = @h.split{|k,v| k.to_s.length == 3}
|
|
22
|
+
end
|
|
23
|
+
should "putting all pairs evaluating to true in the first one" do
|
|
24
|
+
assert_equal( {:bob => 'Marley', :mom => 'Barley'}, @first)
|
|
25
|
+
end
|
|
26
|
+
should "putting all pairs evaluating to false in the second one" do
|
|
27
|
+
assert_equal( {:gods => 'Harley', :chris => 'Farley'}, @second)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: webmat-mini_facet
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mathieu Martin
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-10-07 21:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: Adding a few basic, useful things to Ruby.
|
|
17
|
+
email: webmat@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
|
|
24
|
+
files:
|
|
25
|
+
- lib
|
|
26
|
+
- lib/mini_facet
|
|
27
|
+
- lib/mini_facet/array
|
|
28
|
+
- lib/mini_facet/array/count_distinct.rb
|
|
29
|
+
- lib/mini_facet/array/includes_all.rb
|
|
30
|
+
- lib/mini_facet/array.rb
|
|
31
|
+
- lib/mini_facet/enumerable
|
|
32
|
+
- lib/mini_facet/hash
|
|
33
|
+
- lib/mini_facet/hash/extract.rb
|
|
34
|
+
- lib/mini_facet/hash/split.rb
|
|
35
|
+
- lib/mini_facet/hash.rb
|
|
36
|
+
- lib/mini_facet/object
|
|
37
|
+
- lib/mini_facet/version.rb
|
|
38
|
+
- lib/mini_facet.rb
|
|
39
|
+
- MIT-LICENSE
|
|
40
|
+
- Rakefile
|
|
41
|
+
- rdoc
|
|
42
|
+
- rdoc/classes
|
|
43
|
+
- rdoc/classes/MiniFacet
|
|
44
|
+
- rdoc/classes/MiniFacet/CountDistinct.html
|
|
45
|
+
- rdoc/classes/MiniFacet/Extract.html
|
|
46
|
+
- rdoc/classes/MiniFacet/IncludesAll.html
|
|
47
|
+
- rdoc/classes/MiniFacet/Split.html
|
|
48
|
+
- rdoc/classes/MiniFacet/VERSION.html
|
|
49
|
+
- rdoc/classes/MiniFacet.html
|
|
50
|
+
- rdoc/created.rid
|
|
51
|
+
- rdoc/files
|
|
52
|
+
- rdoc/files/lib
|
|
53
|
+
- rdoc/files/lib/mini_facet
|
|
54
|
+
- rdoc/files/lib/mini_facet/array
|
|
55
|
+
- rdoc/files/lib/mini_facet/array/count_distinct_rb.html
|
|
56
|
+
- rdoc/files/lib/mini_facet/array/includes_all_rb.html
|
|
57
|
+
- rdoc/files/lib/mini_facet/array_rb.html
|
|
58
|
+
- rdoc/files/lib/mini_facet/hash
|
|
59
|
+
- rdoc/files/lib/mini_facet/hash/extract_rb.html
|
|
60
|
+
- rdoc/files/lib/mini_facet/hash/split_rb.html
|
|
61
|
+
- rdoc/files/lib/mini_facet/hash_rb.html
|
|
62
|
+
- rdoc/files/lib/mini_facet/version_rb.html
|
|
63
|
+
- rdoc/files/lib/mini_facet_rb.html
|
|
64
|
+
- rdoc/files/README_markdown.html
|
|
65
|
+
- rdoc/fr_class_index.html
|
|
66
|
+
- rdoc/fr_file_index.html
|
|
67
|
+
- rdoc/fr_method_index.html
|
|
68
|
+
- rdoc/index.html
|
|
69
|
+
- rdoc/rdoc-style.css
|
|
70
|
+
- README.markdown
|
|
71
|
+
- tasks
|
|
72
|
+
- tasks/gem.rake
|
|
73
|
+
- tasks/rdoc.rake
|
|
74
|
+
- tasks/test.rake
|
|
75
|
+
- test
|
|
76
|
+
- test/integration
|
|
77
|
+
- test/test_helper.rb
|
|
78
|
+
- test/unit
|
|
79
|
+
- test/unit/array
|
|
80
|
+
- test/unit/array/count_distinct_test.rb
|
|
81
|
+
- test/unit/array/includes_all_test.rb
|
|
82
|
+
- test/unit/hash
|
|
83
|
+
- test/unit/hash/extract_test.rb
|
|
84
|
+
- test/unit/hash/split_test.rb
|
|
85
|
+
- test/unit/test_helper.rb
|
|
86
|
+
has_rdoc: true
|
|
87
|
+
homepage: http://github.com/webmat/mini_facet
|
|
88
|
+
post_install_message:
|
|
89
|
+
rdoc_options: []
|
|
90
|
+
|
|
91
|
+
require_paths:
|
|
92
|
+
- lib
|
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: "0"
|
|
98
|
+
version:
|
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: "0"
|
|
104
|
+
version:
|
|
105
|
+
requirements: []
|
|
106
|
+
|
|
107
|
+
rubyforge_project: mini_facet
|
|
108
|
+
rubygems_version: 1.2.0
|
|
109
|
+
signing_key:
|
|
110
|
+
specification_version: 2
|
|
111
|
+
summary: Adding a few basic, useful things to Ruby.
|
|
112
|
+
test_files:
|
|
113
|
+
- test/integration
|
|
114
|
+
- test/test_helper.rb
|
|
115
|
+
- test/unit
|
|
116
|
+
- test/unit/array
|
|
117
|
+
- test/unit/array/count_distinct_test.rb
|
|
118
|
+
- test/unit/array/includes_all_test.rb
|
|
119
|
+
- test/unit/hash
|
|
120
|
+
- test/unit/hash/extract_test.rb
|
|
121
|
+
- test/unit/hash/split_test.rb
|
|
122
|
+
- test/unit/test_helper.rb
|