vidibus-core_extensions 0.3.8 → 0.3.9
Sign up to get free protection for your applications and to get access to all the features.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.9
|
@@ -32,19 +32,75 @@ class Array
|
|
32
32
|
# [1, 'a'].merge([1, 2]) # => [1, 2, 'a']
|
33
33
|
# [1, 'a'].merge([3, 1, 2]) # => [3, 1, 2, 'a']
|
34
34
|
#
|
35
|
-
def merge(array)
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
def merge(array, options = {})
|
36
|
+
strict = options[:strict]
|
37
|
+
exclude = options[:exclude] || []
|
38
|
+
list = array.dup
|
39
|
+
for value in list
|
40
|
+
array.delete(value) and next if include?(value) or exclude.include?(value)
|
41
|
+
position = nil
|
42
|
+
if found = merging_predecessor(value, list)
|
39
43
|
position = index(found) + 1
|
40
|
-
elsif found = merging_follower(value,
|
44
|
+
elsif found = merging_follower(value, list)
|
41
45
|
position = index(found)
|
42
46
|
end
|
43
|
-
|
47
|
+
if position or !strict
|
48
|
+
insert(position || length, value)
|
49
|
+
array.delete(value)
|
50
|
+
end
|
44
51
|
end
|
45
52
|
self
|
46
53
|
end
|
47
54
|
|
55
|
+
def merge_nested(array)
|
56
|
+
combined = self.dup
|
57
|
+
append = []
|
58
|
+
array.each_with_index do |a,i|
|
59
|
+
source = a.dup
|
60
|
+
combined.each_with_index do |c,j|
|
61
|
+
break if source.empty?
|
62
|
+
existing = combined.flatten_once - c
|
63
|
+
combined[j] = c.merge(source, :strict => true, :exclude => existing)
|
64
|
+
end
|
65
|
+
if source.any?
|
66
|
+
combined[i] ? combined[i].concat(source) : combined.last.concat(source)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
combined
|
70
|
+
end
|
71
|
+
|
72
|
+
def flatten_with_boundaries(options = {})
|
73
|
+
prefix = options[:prefix] || "__a"
|
74
|
+
i = -1
|
75
|
+
inject([]) do |v,e|
|
76
|
+
if e.is_a?(Array)
|
77
|
+
i += 1
|
78
|
+
k = "#{prefix}#{i}"
|
79
|
+
v << k
|
80
|
+
v.concat(e)
|
81
|
+
v << k
|
82
|
+
else
|
83
|
+
v << e
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def convert_boundaries(options = {})
|
89
|
+
prefix = options[:prefix] || "__a"
|
90
|
+
i = 0
|
91
|
+
boundaries = select {|a| a.match(/#{prefix}\d+/) if a.is_a?(String)}
|
92
|
+
return self unless boundaries.any?
|
93
|
+
array = self.dup
|
94
|
+
converted = []
|
95
|
+
for boundary in boundaries.uniq
|
96
|
+
converted.concat(array.slice!(0, array.index(boundary)))
|
97
|
+
array.shift
|
98
|
+
converted << array.slice!(0, array.index(boundary))
|
99
|
+
array.shift
|
100
|
+
end
|
101
|
+
converted.concat(array)
|
102
|
+
end
|
103
|
+
|
48
104
|
private
|
49
105
|
|
50
106
|
# Returns predecessor of given needle from haystack.
|
@@ -90,13 +90,13 @@ class String
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
# Truncates string to given length while preserves whole words.
|
95
95
|
# If string exceeds given length, an ellipsis will be appended.
|
96
96
|
#
|
97
97
|
# Example:
|
98
|
-
#
|
99
|
-
# "O Brother, Where Art Thou?".snip(13) # => "O Brother, Where…"
|
98
|
+
#
|
99
|
+
# "O Brother, Where Art Thou?".snip(13) # => "O Brother, Where…"
|
100
100
|
#
|
101
101
|
def snip(length, ellipsis = "…")
|
102
102
|
return self if self.empty?
|
@@ -110,4 +110,14 @@ class String
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
113
|
+
|
114
|
+
# Removes all html tags from given string.
|
115
|
+
def strip_tags
|
116
|
+
self.gsub(/<+\/?[^>]*>+/, '')
|
117
|
+
end
|
118
|
+
|
119
|
+
# Removes all html tags on self.
|
120
|
+
def strip_tags!
|
121
|
+
self.replace strip_tags
|
122
|
+
end
|
113
123
|
end
|
@@ -55,5 +55,111 @@ describe "Vidibus::CoreExtensions::Array" do
|
|
55
55
|
it "should merge [2,'b',1,'a'] with [5,3,6,7,1,2,4]" do
|
56
56
|
[2,'b',1,'a'].merge([5,3,6,7,1,2,4]).should eql([2,4,'b',5,3,6,7,1,'a'])
|
57
57
|
end
|
58
|
+
|
59
|
+
context "with :strict option" do
|
60
|
+
it "should merge [] with [1,2]" do
|
61
|
+
array = [1,2]
|
62
|
+
[].merge(array, :strict => true).should eql([])
|
63
|
+
array.should eql([1,2])
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should merge [1,'a'] with [3,1,2]" do
|
67
|
+
array = [3,1,2]
|
68
|
+
[1,'a'].merge(array, :strict => true).should eql([3,1,2,'a'])
|
69
|
+
array.should be_empty
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#merge_nested" do
|
75
|
+
it "should merge [[]] with [[1,2]]" do
|
76
|
+
[[]].merge_nested([[1,2]]).should eql([[1,2]])
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should merge [[]] with [[1],[2]]" do
|
80
|
+
[[]].merge_nested([[1],[2]]).should eql([[1,2]])
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should merge [[],[]] with [[1],[2]]" do
|
84
|
+
[[],[]].merge_nested([[1],[2]]).should eql([[1],[2]])
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should merge [[1],[]] with [[1],[2]]" do
|
88
|
+
[[1],[]].merge_nested([[1],[2]]).should eql([[1],[2]])
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should merge [[1],[2]] with [[1],[2]]" do
|
92
|
+
[[1],[2]].merge_nested([[1],[2]]).should eql([[1],[2]])
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should merge [[2],[1]] with [[1],[2]]" do
|
96
|
+
[[2],[1]].merge_nested([[1],[2]]).should eql([[2],[1]])
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should merge [[2]] with [[1],[2]]" do
|
100
|
+
[[2]].merge_nested([[1],[2]]).should eql([[2,1]])
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should merge [[2],[]] with [[1],[2]]" do
|
104
|
+
[[2],[]].merge_nested([[1],[2]]).should eql([[2,1],[]])
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should merge [[1,2,3]] with [[1,2],[3]]" do
|
108
|
+
[[1,2,3]].merge_nested([[1,2],[3]]).should eql([[1,2,3]])
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should merge [[1,2],[3]] with [[1],[2,3]]" do
|
112
|
+
[[1,2],[3]].merge_nested([[1],[2,3]]).should eql([[1,2],[3]])
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should keep source intact" do
|
116
|
+
source = [[1,2]]
|
117
|
+
[[1,2]].merge_nested(source)
|
118
|
+
source.should eql([[1,2]])
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "#flatten_with_boundaries" do
|
123
|
+
it "should flatten [[1]]" do
|
124
|
+
[[1]].flatten_with_boundaries.should eql(["__a0",1,"__a0"])
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should flatten [[1],2,[3]]" do
|
128
|
+
[[1],2,[3]].flatten_with_boundaries.should eql(["__a0",1,"__a0",2,"__a1",3,"__a1"])
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should flatten [1,[2],3]" do
|
132
|
+
[1,[2],3].flatten_with_boundaries.should eql([1,"__a0",2,"__a0",3])
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should flatten [1,[2],3,4,[["x"]]]' do
|
136
|
+
[1,[2],3,4,[["x"]]].flatten_with_boundaries.should eql([1,"__a0",2,"__a0",3,4,"__a1",["x"],"__a1"])
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should handle [1,2]" do
|
140
|
+
[1,2].flatten_with_boundaries.should eql([1,2])
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "#convert_boundaries" do
|
145
|
+
it 'should convert ["__a0",1,"__a0"]' do
|
146
|
+
["__a0",1,"__a0"].convert_boundaries.should eql([[1]])
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should convert ["__a0",1,"__a0",2,"__a1",3,"__a1"]' do
|
150
|
+
["__a0",1,"__a0",2,"__a1",3,"__a1"].convert_boundaries.should eql([[1],2,[3]])
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should convert [1,"__a0",2,"__a0",3]' do
|
154
|
+
[1,"__a0",2,"__a0",3].convert_boundaries.should eql([1,[2],3])
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should convert [1,"__a0",2,"__a0",3,4,"__a1",["x"],"__a1"]' do
|
158
|
+
[1,"__a0",2,"__a0",3,4,"__a1",["x"],"__a1"].convert_boundaries.should eql([1,[2],3,4,[["x"]]])
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should convert [1,2]" do
|
162
|
+
[1,2].convert_boundaries.should eql([1,2])
|
163
|
+
end
|
58
164
|
end
|
59
165
|
end
|
@@ -75,26 +75,44 @@ describe "Vidibus::CoreExtensions::String" do
|
|
75
75
|
string.should eql("Masao, Mutoh")
|
76
76
|
end
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
describe "snip" do
|
80
80
|
it "should truncate string to given length while preserving words" do
|
81
81
|
"O Brother, Where Art Thou?".snip(13).should eql("O Brother, Where…")
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
it "should return whole string if it fits in given length" do
|
85
85
|
"O Brother, Where Art Thou?".snip(100).should eql("O Brother, Where Art Thou?")
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
it "should return whole string if it equals length" do
|
89
89
|
"O Brother, Where Art Thou?".snip(26).should eql("O Brother, Where Art Thou?")
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
it "should return whole string if it equals length" do
|
93
93
|
"O Brother, Where Art Thou?".snip(11).should eql("O Brother,…")
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
it "should trim white space" do
|
97
97
|
"O Brother, Where Art Thou?".snip(11).should eql("O Brother,…")
|
98
98
|
end
|
99
99
|
end
|
100
|
+
|
101
|
+
describe "strip_tags" do
|
102
|
+
it "should remove all tags from string" do
|
103
|
+
"<p>Think<br />different</p>".strip_tags.should eql("Thinkdifferent")
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should even remove chars that aren't tags but look like ones" do
|
107
|
+
"small < large > small".strip_tags.should eql("small small")
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "strip_tags!" do
|
112
|
+
it "should strip tags on self" do
|
113
|
+
string = "<p>Think<br />different</p>"
|
114
|
+
string.strip_tags!
|
115
|
+
string.should eql("Thinkdifferent")
|
116
|
+
end
|
117
|
+
end
|
100
118
|
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.9"
|
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{2010-10-
|
12
|
+
s.date = %q{2010-10-22}
|
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: 1
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 9
|
10
|
+
version: 0.3.9
|
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: 2010-10-
|
18
|
+
date: 2010-10-22 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|