autocompletion 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ Usage
28
28
  Person.new("Luke", "Skywalker"),
29
29
  Person.new("Anakin", "Skywalker"),
30
30
  ]
31
- auto = AutoCompletion.map(people) { |person|
31
+ auto = AutoCompletion.map_keys(people) { |person|
32
32
  [person.first_name, person.last_name]
33
33
  }
34
34
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "autocompletion"
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
  s.authors = "Stefan Rusterholz"
7
7
  s.email = "stefan.rusterholz@gmail.com"
8
8
  s.homepage = "http://github.com/apeiros/autocompletion"
@@ -24,7 +24,7 @@ require 'autocompletion/version'
24
24
  # Person.new("Luke", "Skywalker"),
25
25
  # Person.new("Anakin", "Skywalker"),
26
26
  # ]
27
- # auto = AutoCompletion.map(people) { |person|
27
+ # auto = AutoCompletion.map_keys(people) { |person|
28
28
  # [person.first_name, person.last_name]
29
29
  # }
30
30
  #
@@ -53,9 +53,9 @@ class AutoCompletion
53
53
  end
54
54
 
55
55
  # @return [AutoCompletion]
56
- # Map a list of entities to their keys. The block should return an array of valid
57
- # prefixes for the yielded entity.
58
- def self.map(entities)
56
+ # Map a list of entities to many of their attributes.
57
+ # The block should return an array of strings which can be prefix-searched.
58
+ def self.map_keys(entities)
59
59
  mapped = entities.flat_map { |entity|
60
60
  keys = yield(entity)
61
61
  keys.flat_map { |key| [key, entity] }
@@ -64,6 +64,17 @@ class AutoCompletion
64
64
  unordered_tuples(mapped.each_slice(2))
65
65
  end
66
66
 
67
+ # @return [AutoCompletion]
68
+ # Map a list of entities to one its attributes.
69
+ # The block should return string which can be prefix-searched.
70
+ def self.map_key(entities)
71
+ mapped = entities.flat_map { |entity|
72
+ [key, yield(entity)]
73
+ }
74
+
75
+ unordered_tuples(mapped)
76
+ end
77
+
67
78
  # @return [AutoCompletion]
68
79
  # Creates an AutoCompletion for an unordered array of the form [["prefix", value], …].
69
80
  def self.unordered_tuples(entities)
@@ -83,16 +94,15 @@ class AutoCompletion
83
94
  # @see AutoCompletion::words, AutoCompletion::map
84
95
  def initialize(entities, force=false)
85
96
  @entities = entities
86
- raise InvalidOrder.new unless force || valid?
97
+ raise InvalidOrder unless force || valid?
87
98
  end
88
99
 
89
100
  # @return [Boolean]
90
101
  # Returns true if the prefixes are in a valid order.
91
102
  def valid?
92
- @entities.each_slice(2).each_cons(2) do |(a,_),(b,_)|
93
- return false unless a <= b
94
- end
95
- true
103
+ @entities.each_slice(2).each_cons(2).all? { |(a,_),(b,_)|
104
+ a <= b
105
+ }
96
106
  end
97
107
 
98
108
  # @return [Boolean]
@@ -138,8 +148,12 @@ class AutoCompletion
138
148
  word < @entities.first[0,word.size] || word > @entities[-2][0,word.size]
139
149
  }
140
150
 
141
- slices = prefixes.map { |word| range_search(word) }
142
- return [] if slices.include?(nil) # short-cut
151
+ slices = prefixes.map { |word|
152
+ slice = range_search(word)
153
+ return [] unless slice # short-cut
154
+
155
+ slice
156
+ }
143
157
 
144
158
  result = @entities[slices.pop].each_slice(2).map(&:last).uniq
145
159
  slices.each do |slice|
@@ -168,7 +182,7 @@ class AutoCompletion
168
182
  return length..length if @entities[-2][0,prefix_size] < prefix # prefix is bigger than biggest value
169
183
 
170
184
  # binary search for smallest index
171
- # mark biggest right that include prefix, and biggest mark that doesn't include prefix
185
+ # mark biggest right which includes prefix, and biggest mark that doesn't include prefix
172
186
  while(left<right)
173
187
  index = (left+right)>>1
174
188
  cmp_value = @entities.at(index<<1)[0,prefix_size]
@@ -9,5 +9,5 @@ end
9
9
  class AutoCompletion
10
10
 
11
11
  # The version of the gem
12
- Version = Gem::Version.new("0.0.1")
12
+ Version = Gem::Version.new("0.0.2")
13
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autocompletion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-19 00:00:00.000000000Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: This gem provides fast prefix-autocompletion in pure ruby.
15
15
  email: stefan.rusterholz@gmail.com
@@ -42,8 +42,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  version: 1.3.1
43
43
  requirements: []
44
44
  rubyforge_project:
45
- rubygems_version: 1.8.15
45
+ rubygems_version: 1.8.24
46
46
  signing_key:
47
47
  specification_version: 3
48
48
  summary: Fast prefix-autocompletion in pure ruby.
49
49
  test_files: []
50
+ has_rdoc: