separa 0.0.2 → 0.0.3
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/.gitignore +1 -1
- data/CHANGELOG.md +9 -0
- data/README.md +62 -1
- data/lib/Separa/obj.rb +26 -11
- data/lib/separa.rb +2 -0
- data/makefile +1 -1
- data/separa.gemspec +2 -2
- data/tests/separa_test.rb +8 -8
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f1473a4f606c3fe324c23ef40d6415774476fef
|
4
|
+
data.tar.gz: 59f26263e9ab27476361ebe6723ed9bbdb1f1990
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79710d6e1abe160fd512d4c8a2ce6176ddcdee2e401c1d2836b50a5d5df36a97d3f8599dabea7f2a4edc8e9ad336e8eff901e0d908b5bbcb77ac95242436cdb6
|
7
|
+
data.tar.gz: d5b64d73087a76e06f00159cb0a16dd90098fdfbb1121c816ce30c8bd31e52aa71912093486e10954e11c6ace7711aca782981bbef25da2f2a13bbc68461f642
|
data/.gitignore
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
*.gem
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Separa splits chunks of text into tokens to be indexed
|
|
6
6
|
Description
|
7
7
|
-----------
|
8
8
|
|
9
|
-
Separa splits chunks of text or ruby objects into tokens to be indexed by [Busca](https://github.com/Porta/busca), the simple redis search
|
9
|
+
Separa splits chunks of text or ruby objects into tokens to be indexed by [Busca](https://github.com/Porta/busca), the simple redis search.
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -18,3 +18,64 @@ $ gem install separa
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
The simplest possible usage is with default options:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
separa = Separa.new
|
25
|
+
words = "This is a bunch of words. Separated"
|
26
|
+
result = separa.call(words)
|
27
|
+
puts result.inspect
|
28
|
+
# ["This", "is", "a", "bunch", "of", "words", "", "Separated"]
|
29
|
+
```
|
30
|
+
|
31
|
+
You'll notice a few things here:
|
32
|
+
* There's an empty element between `words` and `Separated`
|
33
|
+
* Words kept their capitalization
|
34
|
+
|
35
|
+
That's all intended. Separa only takes care of spliting the string into an array. It is up to you to filter later that array.
|
36
|
+
|
37
|
+
Separa comes bundled with two 'Separators', but you can roll your own (more on that later).
|
38
|
+
The separator usage is fairly simple, just pass the separator to the `Separa.new` constructor.
|
39
|
+
|
40
|
+
[Separa::Text](./lib/Separa/text.rb) Splits a string of text into an array. You can pass a regexp to be used on the split.
|
41
|
+
|
42
|
+
[Separa::Obj](./lib/Separa/obj.rb) Splits a ruby hash into an array. This is where things get interesting.
|
43
|
+
Let's see a example:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
separa = Separa.new(Separa::Obj)
|
47
|
+
h = { uno: 1, dos: 2, tres: {uno: 'one', dos: 'two'} }
|
48
|
+
result = separa.call(h)
|
49
|
+
puts result.inspect
|
50
|
+
# ["uno:1", "dos:2", "tres.uno:one", "tres.dos:two"]
|
51
|
+
```
|
52
|
+
|
53
|
+
By default, Separa::Obj will use a semicollon divide the object key and it's value. You can change that passing a different divider.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
separa = Separa.new(Separa::Obj, divider: '-')
|
57
|
+
h = { uno: 1, dos: 2, tres: {uno: 'one', dos: 'two'} }
|
58
|
+
result = separa.call(h)
|
59
|
+
puts result.inspect
|
60
|
+
# ["uno-1", "dos-2", "tres.uno-one", "tres.dos-two"]
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
## Roll your own separator
|
65
|
+
|
66
|
+
Writting your own separator is fairly simple. You only need to take care of 3 things.
|
67
|
+
|
68
|
+
* It should respond to a `call` method.
|
69
|
+
* The `call` method should receive 2 parameters. The string to split and a hash with options.
|
70
|
+
* It should return an array. (Actually, returning an array isn't required, but recommended. I mean, that's *half* of the objective of this library, right?)
|
71
|
+
|
72
|
+
Take a look at the bundled separators if you need inspiration:
|
73
|
+
|
74
|
+
[Separa::Text](./lib/Separa/text.rb)
|
75
|
+
|
76
|
+
[Separa::Obj](./lib/Separa/obj.rb)
|
77
|
+
|
78
|
+
The code is pretty straightforward.
|
79
|
+
|
80
|
+
Have fun splitting your strings, and drop a line to julian@porta.sh if you have something to say.
|
81
|
+
|
data/lib/Separa/obj.rb
CHANGED
@@ -1,15 +1,30 @@
|
|
1
1
|
class Separa
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
module Obj
|
3
|
+
|
4
|
+
def self.call(obj, opts)
|
5
|
+
divider = opts[:divider] || ':'
|
6
|
+
res = wonderflat(obj)
|
7
|
+
res.map{|pair,_| "#{pair.keys[0].to_s}#{divider}#{pair.values[0].to_s}"}
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.wonderflat(obj, memo = [])
|
11
|
+
return {
|
12
|
+
memo.join('.') => obj
|
13
|
+
} unless obj.respond_to?(:inject)
|
9
14
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
15
|
+
obj.inject([]) do |out_array, _val|
|
16
|
+
(head, *_tail) = _val
|
17
|
+
tail = _tail.pop
|
18
|
+
case tail
|
19
|
+
when Array
|
20
|
+
tail.map do |tail_elem|
|
21
|
+
out_array.push self.wonderflat(tail_elem, memo + [ head ])
|
22
|
+
end
|
23
|
+
else
|
24
|
+
out_array.push self.wonderflat( tail, memo + [ head ])
|
25
|
+
end
|
26
|
+
out_array.flatten
|
27
|
+
end
|
14
28
|
end
|
29
|
+
end
|
15
30
|
end
|
data/lib/separa.rb
CHANGED
data/makefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
test:
|
2
|
-
|
2
|
+
cutest ./tests/*_test.rb
|
data/separa.gemspec
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "separa"
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.3"
|
6
6
|
s.summary = "Separa splits chunks of text into tokens to be indexed"
|
7
7
|
s.description = "Separa splits chunks of text into tokens to be indexed by Busca, the simple redis search"
|
8
8
|
s.authors = ["Julián Porta"]
|
9
9
|
s.email = ["julian@porta.sh"]
|
10
|
-
s.homepage = "
|
10
|
+
s.homepage = "https://github.com/Porta/separa"
|
11
11
|
s.files = `git ls-files`.split("\n")
|
12
12
|
s.license = "MIT"
|
13
13
|
s.add_development_dependency "cutest", '~>1.2'
|
data/tests/separa_test.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
require File.expand_path("../lib/separa", File.dirname(__FILE__))
|
2
2
|
|
3
|
-
|
4
3
|
setup do
|
5
4
|
|
6
5
|
end
|
7
6
|
|
8
7
|
test "default separator should be text" do
|
9
|
-
|
8
|
+
sep = Separa.new()
|
10
9
|
assert_equal sep.separador, Separa::Text
|
11
10
|
end
|
12
11
|
|
@@ -51,9 +50,10 @@ test "Separa::Obj should separate Obj into key:value pairs" do
|
|
51
50
|
assert_equal result, ['uno:1', 'dos:2', 'tres.uno:one', 'tres.dos:two']
|
52
51
|
end
|
53
52
|
|
54
|
-
test "Separa::Obj should separate Obj into key
|
55
|
-
sep = Separa.new(Separa::Obj
|
56
|
-
h = { uno: 1, dos: 2, tres:
|
57
|
-
result = sep.call(h)
|
58
|
-
assert_equal result, ['uno
|
59
|
-
end
|
53
|
+
test "Separa::Obj should separate Obj into key:value pairs, even with arrays" do
|
54
|
+
sep = Separa.new(Separa::Obj)
|
55
|
+
h = { uno: 1, dos: 2, tres: [:uno, :dos, :tres] }
|
56
|
+
result = sep.call(h)
|
57
|
+
assert_equal result, ['uno:1', 'dos:2', 'tres:uno', 'tres:dos', 'tres:tres']
|
58
|
+
end
|
59
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: separa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julián Porta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cutest
|
@@ -33,6 +33,7 @@ extensions: []
|
|
33
33
|
extra_rdoc_files: []
|
34
34
|
files:
|
35
35
|
- ".gitignore"
|
36
|
+
- CHANGELOG.md
|
36
37
|
- LICENSE
|
37
38
|
- README.md
|
38
39
|
- lib/Separa/obj.rb
|
@@ -41,7 +42,7 @@ files:
|
|
41
42
|
- makefile
|
42
43
|
- separa.gemspec
|
43
44
|
- tests/separa_test.rb
|
44
|
-
homepage:
|
45
|
+
homepage: https://github.com/Porta/separa
|
45
46
|
licenses:
|
46
47
|
- MIT
|
47
48
|
metadata: {}
|