vidibus-core_extensions 0.3.14 → 0.3.15
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/VERSION +1 -1
- data/lib/vidibus/core_extensions/array.rb +30 -0
- data/spec/vidibus/core_extensions/array_spec.rb +29 -3
- data/vidibus-core_extensions.gemspec +2 -2
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.15
|
@@ -101,6 +101,36 @@ class Array
|
|
101
101
|
converted.concat(array)
|
102
102
|
end
|
103
103
|
|
104
|
+
# Sorts array of values, hashes or objects by given order.
|
105
|
+
# In order to sort hashes or objects, an attribute is required.
|
106
|
+
#
|
107
|
+
# Examples:
|
108
|
+
#
|
109
|
+
# list = ["two", "one", "three"]
|
110
|
+
# map = ["one", "two", "three"]
|
111
|
+
# list.sort_by_map(map)
|
112
|
+
# # => ["one", "two", "three"]
|
113
|
+
#
|
114
|
+
# list = [{:n => "two"}, {:n => "one"}, {:n => "three"}]
|
115
|
+
# map = ["one", "two", "three"]
|
116
|
+
# list.sort_by_map(map, :n)
|
117
|
+
# # => [{:n => "one"}, {:n => "two"}, {:n => "three"}]
|
118
|
+
#
|
119
|
+
def sort_by_map(map, attribute = nil)
|
120
|
+
ordered = []
|
121
|
+
for a in map
|
122
|
+
ordered << self.detect do |m|
|
123
|
+
if attribute
|
124
|
+
n = m.is_a?(Hash) ? m[attribute] : m.send(attribute)
|
125
|
+
a == n
|
126
|
+
else
|
127
|
+
a == m
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
ordered.compact
|
132
|
+
end
|
133
|
+
|
104
134
|
private
|
105
135
|
|
106
136
|
# Returns predecessor of given needle from haystack.
|
@@ -103,15 +103,15 @@ describe "Array" do
|
|
103
103
|
it "should merge [[2],[]] with [[1],[2]]" do
|
104
104
|
[[2],[]].merge_nested([[1],[2]]).should eql([[2,1],[]])
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
it "should merge [[1,2,3]] with [[1,2],[3]]" do
|
108
108
|
[[1,2,3]].merge_nested([[1,2],[3]]).should eql([[1,2,3]])
|
109
109
|
end
|
110
|
-
|
110
|
+
|
111
111
|
it "should merge [[1,2],[3]] with [[1],[2,3]]" do
|
112
112
|
[[1,2],[3]].merge_nested([[1],[2,3]]).should eql([[1,2],[3]])
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
it "should keep source intact" do
|
116
116
|
source = [[1,2]]
|
117
117
|
[[1,2]].merge_nested(source)
|
@@ -162,4 +162,30 @@ describe "Array" do
|
|
162
162
|
[1,2].convert_boundaries.should eql([1,2])
|
163
163
|
end
|
164
164
|
end
|
165
|
+
|
166
|
+
describe "#sort_by_map" do
|
167
|
+
it "should sort the list of hashes by given order of attribute values" do
|
168
|
+
list = [{:n => "two"}, {:n => "one"}, {:n => "three"}]
|
169
|
+
map = ["one", "two", "three"]
|
170
|
+
list.sort_by_map(map, :n).should eql([{:n => "one"}, {:n => "two"}, {:n => "three"}])
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should sort the list of objects by given order of attribute values" do
|
174
|
+
list = []
|
175
|
+
list << OpenStruct.new(:n => "two")
|
176
|
+
list << OpenStruct.new(:n => "one")
|
177
|
+
list << OpenStruct.new(:n => "three")
|
178
|
+
map = ["one", "two", "three"]
|
179
|
+
ordered = list.sort_by_map(map, :n)
|
180
|
+
ordered[0].n.should eql("one")
|
181
|
+
ordered[1].n.should eql("two")
|
182
|
+
ordered[2].n.should eql("three")
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should sort the list of values by given order" do
|
186
|
+
list = ["two", "one", "three"]
|
187
|
+
map = ["one", "two", "three"]
|
188
|
+
list.sort_by_map(map).should eql(["one", "two", "three"])
|
189
|
+
end
|
190
|
+
end
|
165
191
|
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{vidibus-core_extensions}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.15"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andre Pankratz"]
|
12
|
-
s.date = %q{2011-01-
|
12
|
+
s.date = %q{2011-01-15}
|
13
13
|
s.description = %q{Provides some extensions to the ruby core.}
|
14
14
|
s.email = %q{andre@vidibus.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vidibus-core_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 15
|
10
|
+
version: 0.3.15
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andre Pankratz
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-15 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|