invert 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.html +36 -0
- data/README.md +36 -0
- data/VERSION.yml +4 -0
- data/init.rb +3 -0
- data/invert.gemspec +46 -0
- data/lib/invert.rb +32 -0
- data/spec/invert_spec.rb +27 -0
- data/spec/spec_helper.rb +1 -0
- metadata +63 -0
data/README.html
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
<head>
|
2
|
+
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
|
3
|
+
<link href="dev/github.css" rel="stylesheet" type="text/css" />
|
4
|
+
</head>
|
5
|
+
|
6
|
+
<h1 id="inverse-comparison-wrapper">Inverse Comparison Wrapper</h1>
|
7
|
+
|
8
|
+
<h2 id="introduction">Introduction</h2>
|
9
|
+
|
10
|
+
<p>To <tt>sort_by</tt> in reverse order, use the <tt>Invert</tt> wrapper supplied in this gem.</p>
|
11
|
+
|
12
|
+
<p>This gem is based on post by <strong>glenn mcdonald</strong>:</p>
|
13
|
+
|
14
|
+
<ul>
|
15
|
+
<li><a href="http://stackoverflow.com/questions/73032/how-can-i-sort-by-multiple-conditions-with-different-orders">http://stackoverflow.com/questions/73032/how-can-i-sort-by-multiple-conditions-with-different-orders</a></li>
|
16
|
+
<li><a href="http://stackoverflow.com/users/7919/glenn-mcdonald">http://stackoverflow.com/users/7919/glenn-mcdonald</a></li>
|
17
|
+
</ul>
|
18
|
+
|
19
|
+
<h2 id="setup">Setup</h2>
|
20
|
+
|
21
|
+
<pre><code>gem install invert
|
22
|
+
</code></pre>
|
23
|
+
|
24
|
+
<h2 id="examples">Examples</h2>
|
25
|
+
|
26
|
+
<pre><code>[1, 2, 3].sort_by {|i| Invert(i)} # => [3, 2, 1]
|
27
|
+
["alfa", "bravo", "charlie"].sort_by {|s| Invert(s)} # => ["charlie", "bravo", "alfa"]
|
28
|
+
</code></pre>
|
29
|
+
|
30
|
+
<p>Multi-sort:</p>
|
31
|
+
<pre><code>users.sort_by {|r| [Invert(r.age), r.name]}
|
32
|
+
</code></pre>
|
33
|
+
|
34
|
+
<h2 id="feedback">Feedback</h2>
|
35
|
+
|
36
|
+
<p>Send bug reports, suggestions and criticisms through <a href="http://github.com/dadooda/invert">project's page on GitHub</a>.</p>
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
Inverse Comparison Wrapper
|
3
|
+
==========================
|
4
|
+
|
5
|
+
Introduction
|
6
|
+
------------
|
7
|
+
|
8
|
+
To <tt>sort_by</tt> in reverse order, use the <tt>Invert</tt> wrapper supplied in this gem.
|
9
|
+
|
10
|
+
This gem is based on post by **glenn mcdonald**:
|
11
|
+
|
12
|
+
* [http://stackoverflow.com/questions/73032/how-can-i-sort-by-multiple-conditions-with-different-orders](http://stackoverflow.com/questions/73032/how-can-i-sort-by-multiple-conditions-with-different-orders)
|
13
|
+
* [http://stackoverflow.com/users/7919/glenn-mcdonald](http://stackoverflow.com/users/7919/glenn-mcdonald)
|
14
|
+
|
15
|
+
|
16
|
+
Setup
|
17
|
+
-----
|
18
|
+
|
19
|
+
$ gem sources --add http://rubygems.org
|
20
|
+
$ gem install invert
|
21
|
+
|
22
|
+
|
23
|
+
Examples
|
24
|
+
--------
|
25
|
+
|
26
|
+
[1, 2, 3].sort_by {|i| Invert(i)} # => [3, 2, 1]
|
27
|
+
["alfa", "bravo", "charlie"].sort_by {|s| Invert(s)} # => ["charlie", "bravo", "alfa"]
|
28
|
+
|
29
|
+
Multi-sort:
|
30
|
+
users.sort_by {|r| [Invert(r.age), r.name]}
|
31
|
+
|
32
|
+
|
33
|
+
Feedback
|
34
|
+
--------
|
35
|
+
|
36
|
+
Send bug reports, suggestions and criticisms through [project's page on GitHub](http://github.com/dadooda/invert).
|
data/VERSION.yml
ADDED
data/init.rb
ADDED
data/invert.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{invert}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Glenn McDonald", "Alex Fortuna"]
|
12
|
+
s.date = %q{2010-07-18}
|
13
|
+
s.description = %q{Inverse Comparison Wrapper}
|
14
|
+
s.email = %q{alex.r@askit.org}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.html",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"README.html",
|
21
|
+
"README.md",
|
22
|
+
"VERSION.yml",
|
23
|
+
"init.rb",
|
24
|
+
"invert.gemspec",
|
25
|
+
"lib/invert.rb"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/dadooda/invert}
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubygems_version = %q{1.3.5}
|
31
|
+
s.summary = %q{Inverse Comparison Wrapper}
|
32
|
+
s.test_files = [
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/invert_spec.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
else
|
43
|
+
end
|
44
|
+
else
|
45
|
+
end
|
46
|
+
end
|
data/lib/invert.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Inverse comparison wrapper. Terribly useful to use with <tt>sort_by</tt> to do asc/desc multisorting per each key.
|
2
|
+
#
|
3
|
+
# Based on the post by glenn mcdonald:
|
4
|
+
# http://stackoverflow.com/questions/73032/how-can-i-sort-by-multiple-conditions-with-different-orders
|
5
|
+
# http://stackoverflow.com/users/7919/glenn-mcdonald
|
6
|
+
#
|
7
|
+
# Examples:
|
8
|
+
# [1, 2, 3].sort_by {|i| Invert(i)} # => [3, 2, 1]
|
9
|
+
# ["alfa", "bravo", "charlie"].sort_by {|s| Invert(s)} # => ["charlie", "bravo", "alfa"]
|
10
|
+
#
|
11
|
+
# Multi-sort:
|
12
|
+
# users.sort_by {|r| [Invert(r.age), r.name]}
|
13
|
+
|
14
|
+
# Wrap an object to invert <tt><=></tt> method.
|
15
|
+
# items.sort_by {|r| [r.prop1, Invert.new(r.prop2)]}
|
16
|
+
class Invert
|
17
|
+
attr_reader :r
|
18
|
+
|
19
|
+
def initialize(r)
|
20
|
+
@r = r
|
21
|
+
end
|
22
|
+
|
23
|
+
def <=>(other)
|
24
|
+
-(@r <=> other.r)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# A shortened form of <tt>Invert.new</tt>.
|
29
|
+
# items.sort_by {|r| [r.prop1, Invert(r.prop2)]}
|
30
|
+
def Invert(*args)
|
31
|
+
Invert.new(*args)
|
32
|
+
end
|
data/spec/invert_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
describe "#{klass = Invert}" do
|
4
|
+
describe "#new" do
|
5
|
+
it "requires an argument" do
|
6
|
+
Proc.new do
|
7
|
+
klass.new
|
8
|
+
end.should raise_error ArgumentError
|
9
|
+
|
10
|
+
Proc.new do
|
11
|
+
klass.new(10)
|
12
|
+
end.should_not raise_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "generally works" do
|
17
|
+
[1, 2, 3].sort_by {|i| Invert.new(i)}.should == [3, 2, 1]
|
18
|
+
["alfa", "bravo", "charlie"].sort_by {|s| Invert.new(s)}.should == ["charlie", "bravo", "alfa"]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "Invert()" do
|
23
|
+
it "generally works" do
|
24
|
+
[1, 2, 3].sort_by {|i| Invert.new(i)}.should == [3, 2, 1]
|
25
|
+
["alfa", "bravo", "charlie"].sort_by {|s| Invert.new(s)}.should == ["charlie", "bravo", "alfa"]
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "../init.rb")
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: invert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Glenn McDonald
|
8
|
+
- Alex Fortuna
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-07-18 00:00:00 +04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Inverse Comparison Wrapper
|
18
|
+
email: alex.r@askit.org
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.html
|
25
|
+
- README.md
|
26
|
+
files:
|
27
|
+
- README.html
|
28
|
+
- README.md
|
29
|
+
- VERSION.yml
|
30
|
+
- init.rb
|
31
|
+
- invert.gemspec
|
32
|
+
- lib/invert.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/dadooda/invert
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Inverse Comparison Wrapper
|
61
|
+
test_files:
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
- spec/invert_spec.rb
|