enf 0.1.1 → 0.1.2
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/enf/elephant.rb +16 -14
- data/lib/enf/elephant/fetch_service.rb +21 -0
- data/lib/enf/suggest.rb +10 -8
- data/lib/enf/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ef34aabd94c40d9a77e37ccec899650c1d0aeb8
|
4
|
+
data.tar.gz: cfcaf0c7b4a5aab9f89feaf57ad51f3711f49e5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17431f413061396131d72af1b32c070865c9d8b9c296387a043c8a9dce0175aacfbe6be33c65ba561d8d401543b1ec5010c3b68272bd07c2c1c1ce53aa2d552a
|
7
|
+
data.tar.gz: 508b75410a629e1086b852b7dc097f005c501670839e5e49510137c546951aeeaf35231f70ab01a26d3055cdd071d5ecc3d483ad2ef4639a506cb1dc0217c7e0
|
data/lib/enf/elephant.rb
CHANGED
@@ -1,30 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'enf/input'
|
4
|
+
require 'enf/elephant/fetch_service'
|
4
5
|
|
5
6
|
module Enf
|
6
7
|
# Represent a node of the graph
|
7
8
|
class Elephant
|
9
|
+
attr_reader :default_leave_value, :children
|
10
|
+
attr_accessor :leave
|
8
11
|
class CannotRegister < RuntimeError; end
|
9
12
|
def initialize(default_leave_value = false)
|
10
13
|
@default_leave_value = default_leave_value
|
11
|
-
|
14
|
+
self.leave = default_leave_value
|
12
15
|
@children = Hash.new { |hash, key| hash[key] = Elephant.new }
|
13
16
|
end
|
14
17
|
|
15
18
|
def register!(element, payload = true)
|
16
19
|
fail CannotRegister if frozen? || Input.invalid?(element)
|
17
|
-
return (
|
18
|
-
|
20
|
+
return (self.leave = payload) if element.empty?
|
21
|
+
children[element[0]].register!(element[1..-1])
|
19
22
|
end
|
20
23
|
|
21
24
|
# Null node, used to answer to unknown values
|
22
|
-
class Nope
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
def include_impl(_)
|
27
|
-
false
|
25
|
+
class Nope < Elephant
|
26
|
+
def initialize(leave = false)
|
27
|
+
@leave = leave
|
28
|
+
super(leave)
|
28
29
|
end
|
29
30
|
|
30
31
|
def register!(_)
|
@@ -33,13 +34,14 @@ module Enf
|
|
33
34
|
end
|
34
35
|
|
35
36
|
def include?(element)
|
36
|
-
return
|
37
|
-
|
37
|
+
return default_leave_value if Input.invalid?(element)
|
38
|
+
FetchService.search_in(self, element) { neutral_node }.leave
|
38
39
|
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
protected
|
42
|
+
|
43
|
+
def neutral_node
|
44
|
+
Nope.new default_leave_value
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Enf
|
3
|
+
class Elephant
|
4
|
+
# Pattern dectection service class
|
5
|
+
class FetchService
|
6
|
+
SEARCH_KEY_ERROR = 'key not found: %s'
|
7
|
+
def self.search_in(head, searched)
|
8
|
+
elephant = head
|
9
|
+
index = 0
|
10
|
+
while index < searched.size
|
11
|
+
elephant = elephant.children.fetch(searched[index]) do
|
12
|
+
return yield if block_given?
|
13
|
+
fail KeyError, SEARCH_KEY_ERROR % searched[index].inspect
|
14
|
+
end
|
15
|
+
index += 1
|
16
|
+
end
|
17
|
+
elephant
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/enf/suggest.rb
CHANGED
@@ -18,23 +18,23 @@ module Enf
|
|
18
18
|
class Silent
|
19
19
|
require 'singleton'
|
20
20
|
include Singleton
|
21
|
-
def
|
21
|
+
def complete(_limit, _incompletes, _prefix, results)
|
22
22
|
results
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def suggest_impl(start, limit, incompletes, prefix = '', results = Set.new)
|
27
|
-
|
27
|
+
fetch_root(start).complete(limit, incompletes, prefix, results)
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
def fetch_root(start)
|
31
|
+
Elephant::FetchService.search_in(self, start) { Silent.instance }
|
31
32
|
end
|
32
33
|
|
33
34
|
def complete(limit, incompletes, prefix, results)
|
34
|
-
return results if limit != :none && limit < 0
|
35
35
|
add_current_prefix_if_needed(results, limit, incompletes, prefix)
|
36
36
|
limit = next_limit limit
|
37
|
-
return results
|
37
|
+
return results if limit != :none && limit < 0
|
38
38
|
@children.each do |key, child|
|
39
39
|
results = child.complete(limit, incompletes, prefix + key, results)
|
40
40
|
end
|
@@ -64,10 +64,12 @@ module Enf
|
|
64
64
|
raise SuggestParamError, error.message
|
65
65
|
end
|
66
66
|
|
67
|
-
INVALID_ERR = 'Invalid input
|
67
|
+
INVALID_ERR = 'Invalid input %s'
|
68
68
|
|
69
69
|
def once_validated!(start, char_limit, return_incompletes)
|
70
|
-
|
70
|
+
if Input.invalid? start
|
71
|
+
fail SuggestParamError, INVALID_ERR % start.inspect
|
72
|
+
end
|
71
73
|
char_limit = validate_char_limit!(char_limit)
|
72
74
|
yield start, char_limit, return_incompletes
|
73
75
|
end
|
data/lib/enf/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandre Ignjatovic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Graph based white/black list implementation. Your elephant won't forget.
|
14
14
|
email: alexandre.ignjatovic@gmail.com
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- enf.gemspec
|
22
22
|
- lib/enf.rb
|
23
23
|
- lib/enf/elephant.rb
|
24
|
+
- lib/enf/elephant/fetch_service.rb
|
24
25
|
- lib/enf/input.rb
|
25
26
|
- lib/enf/suggest.rb
|
26
27
|
- lib/enf/version.rb
|