fluorescent 0.0.5 → 0.0.6
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.
- checksums.yaml +4 -4
- data/lib/fluorescent.rb +25 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f6908fe9d74e9c6ff92248c064aaa777e2c6e11
|
4
|
+
data.tar.gz: 97960713c3cde86ae2cf5ca31b76834147b94b73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eef1ea24dfbed1ad5ff5c62e5ee1c240d9cf6adf6e01107a25d393c8275abd54e8124b299355da0512114ff594319685b25c8062e12983990ad0326388b49506
|
7
|
+
data.tar.gz: a9f0a1e5a7f8f53ea87de472e016200726d74de404dc1223cf4d8c94632d20c67b8c6e647ae338300322d0a82024a2451bbc9d36dae8f048f8d37cfdf66cb1f2
|
data/lib/fluorescent.rb
CHANGED
@@ -17,15 +17,21 @@ class Fluorescent
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
def column_names_to_h r
|
21
|
+
row = {}
|
22
|
+
# make sure we get the rest of the column names in the hash
|
23
|
+
r.class.column_names.each do |c|
|
24
|
+
row[c] = r.send(c)
|
25
|
+
end
|
26
|
+
row
|
27
|
+
end
|
28
|
+
|
20
29
|
# distill search result down to n characters before and after
|
21
30
|
# search terms
|
22
31
|
def distill
|
23
32
|
@results.each do |r|
|
24
|
-
row =
|
25
|
-
|
26
|
-
r.class.column_names.each do |c|
|
27
|
-
row[c] = r.send(c)
|
28
|
-
end
|
33
|
+
row = column_names_to_h(r)
|
34
|
+
|
29
35
|
@columns.each do |c|
|
30
36
|
# 1. highlight the search terms
|
31
37
|
# 2. replace the search terms in the results with bold text
|
@@ -34,16 +40,25 @@ class Fluorescent
|
|
34
40
|
string = r.send(c).to_s
|
35
41
|
row[c] = highlight string # need to find a better way to do this
|
36
42
|
if @to_filter.include? c
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
43
|
+
# if nothing matches, we don't want to try to highlight
|
44
|
+
if string.index(@terms[0]) != nil
|
45
|
+
row[c] = highlight string[
|
46
|
+
string.index(@terms[0]),
|
47
|
+
string.index(@terms[0]) + string.length + @padding
|
48
|
+
] << "..."
|
49
|
+
end
|
41
50
|
end
|
42
51
|
end
|
43
|
-
|
52
|
+
|
53
|
+
# symbolize hash keys
|
54
|
+
@formatted_results.push(symbolize_keys(row))
|
44
55
|
end
|
45
56
|
end
|
46
57
|
|
58
|
+
def symbolize_keys row
|
59
|
+
row.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
60
|
+
end
|
61
|
+
|
47
62
|
# highlight the search terms in the result
|
48
63
|
def highlight(string)
|
49
64
|
string.gsub @terms, "<b>#{@terms}</b>"
|