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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6a8d611823d678984ca8c293a069fbf6a202156
4
- data.tar.gz: dfdbe90a9abeaf81a989c9653a82c0cf295cba71
3
+ metadata.gz: 3f1473a4f606c3fe324c23ef40d6415774476fef
4
+ data.tar.gz: 59f26263e9ab27476361ebe6723ed9bbdb1f1990
5
5
  SHA512:
6
- metadata.gz: 9905516337c8682f76a845c093633686158a55056f7fc3a8742512b0a7a2443c1d335e2b03ca9fcf7210cb272408aab766cb68b16b11a578e7f5d87f1f34b47a
7
- data.tar.gz: b5ac4c97b2fc0644548dc8ca0551007fdbbc9655507073ff0de6223c9655a155ead53cfd6f11a72a13c465b99815134d838ce6d0e56abf74e7f2a377fe99d69f
6
+ metadata.gz: 79710d6e1abe160fd512d4c8a2ce6176ddcdee2e401c1d2836b50a5d5df36a97d3f8599dabea7f2a4edc8e9ad336e8eff901e0d908b5bbcb77ac95242436cdb6
7
+ data.tar.gz: d5b64d73087a76e06f00159cb0a16dd90098fdfbb1121c816ce30c8bd31e52aa71912093486e10954e11c6ace7711aca782981bbef25da2f2a13bbc68461f642
data/.gitignore CHANGED
@@ -1 +1 @@
1
- .idea/
1
+ *.gem
@@ -0,0 +1,9 @@
1
+ # Separa Changelog
2
+
3
+ ## [0.0.3] - 2015-07-26
4
+
5
+ ### Changed
6
+ - Fixed arrays in [Separa::Obj](./lib/Separa/obj.rb)
7
+
8
+ ### Added
9
+ - Changelog
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
+
@@ -1,15 +1,30 @@
1
1
  class Separa
2
- module Obj
3
-
4
- def self.call(text, opts)
5
- divider = opts[:divider] || ':'
6
- res = wonderflat(text)
7
- res.map{|k, v| "#{k.to_s}#{divider}#{v.to_s}"}
8
- end
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
- def self.wonderflat(hash, k = [])
11
- return {k.join('.') => hash} unless hash.is_a?(Hash)
12
- hash.inject({}){ |h, v| h.merge! self.wonderflat(v[-1], k + [v[0]]) }
13
- end
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
@@ -5,6 +5,8 @@ class Separa
5
5
  attr_reader :separador
6
6
 
7
7
  def initialize(separador = Separa::Text, opts = {})
8
+
9
+
8
10
  if separador.respond_to?(:call)
9
11
  @separador = separador
10
12
  @opts = opts
data/makefile CHANGED
@@ -1,2 +1,2 @@
1
1
  test:
2
- cutest ./tests/*_test.rb
2
+ cutest ./tests/*_test.rb
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "separa"
5
- s.version = "0.0.2"
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 = "http://github.com/Porta/separa"
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'
@@ -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
- sep = Separa.new()
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.value pairs" do
55
- sep = Separa.new(Separa::Obj, divider: '.')
56
- h = { uno: 1, dos: 2, tres: {uno: 'one', dos: 'two'} }
57
- result = sep.call(h)
58
- assert_equal result, ['uno.1', 'dos.2', 'tres.uno.one', 'tres.dos.two']
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.2
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-05-03 00:00:00.000000000 Z
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: http://github.com/Porta/separa
45
+ homepage: https://github.com/Porta/separa
45
46
  licenses:
46
47
  - MIT
47
48
  metadata: {}