array-sorted 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,3 @@
1
1
 
2
2
  class ::Array::Sorted < ::Array::Hooked
3
3
  end
4
-
5
- class ::SortedArray < ::Array::Sorted
6
- end
@@ -11,5 +11,3 @@ files.each do |this_file|
11
11
  require_relative( File.join( basepath, this_file ) + '.rb' )
12
12
  end
13
13
 
14
- require_relative( basepath + '.rb' )
15
-
@@ -1,5 +1,6 @@
1
1
 
2
2
  require 'array/hooked'
3
+ #require_relative '../../../array-hooked/lib/array-hooked.rb'
3
4
 
4
5
  # namespaces that have to be declared ahead of time for proper load order
5
6
  require_relative './namespaces'
@@ -9,8 +9,13 @@ module ::Array::Sorted::ArrayInterface
9
9
  # initialize #
10
10
  ################
11
11
 
12
- # Adds optional block for retaining sort order.
13
- # @yield Block to use to determine sort order.
12
+ ###
13
+ # Adds optional block for object by which sort order is determined.
14
+ #
15
+ # @yield [object] Block to use to determine sort order object.
16
+ # @yieldparam object Array member or insert item.
17
+ # @yieldreturn [Object] Object to be used for sort order
18
+ #
14
19
  def initialize( configuration_instance = nil, *args, & sort_object_block )
15
20
 
16
21
  super( configuration_instance, *args )
@@ -20,6 +25,18 @@ module ::Array::Sorted::ArrayInterface
20
25
  end
21
26
 
22
27
  end
28
+
29
+ #####################
30
+ # unsorted_insert #
31
+ #####################
32
+
33
+ alias_method :unsorted_insert, :hooked_insert
34
+
35
+ ##################
36
+ # unsorted_set #
37
+ ##################
38
+
39
+ alias_method :unsorted_set, :hooked_set
23
40
 
24
41
  ##############
25
42
  # reverse! #
@@ -93,6 +110,10 @@ module ::Array::Sorted::ArrayInterface
93
110
 
94
111
  end
95
112
 
113
+ ######################################################################################################################
114
+ private ##########################################################################################################
115
+ ######################################################################################################################
116
+
96
117
  ###############################
97
118
  # perform_set_between_hooks #
98
119
  ###############################
@@ -103,7 +124,9 @@ module ::Array::Sorted::ArrayInterface
103
124
  delete_at( index )
104
125
  end
105
126
 
106
- return perform_insert_between_hooks( index, object )
127
+ index = perform_single_object_insert_between_hooks( index, object )
128
+
129
+ return false
107
130
 
108
131
  end
109
132
 
@@ -117,57 +140,91 @@ module ::Array::Sorted::ArrayInterface
117
140
 
118
141
  # we have to have at least one member for comparison-based insert (to retain sorted order)
119
142
 
120
- objects.each do |this_object|
143
+ objects.each_with_index do |this_object, this_index|
144
+ perform_single_object_insert_between_hooks( index + this_index, this_object )
145
+ end
146
+
147
+ # return index our insert began with
148
+ return index
149
+
150
+ end
151
+
152
+ #############################
153
+ # perform_unsorted_insert #
154
+ #############################
155
+
156
+ def perform_unsorted_insert( index, object )
157
+
158
+ return undecorated_insert( index, object )
159
+
160
+ end
161
+
162
+ #################################
163
+ # get_index_for_sorted_insert #
164
+ #################################
165
+
166
+ def get_index_for_sorted_insert( object )
167
+
168
+ index = nil
169
+
170
+ if object.nil?
121
171
 
122
- insert_occurred = false
172
+ index = @sort_order_reversed ? count : 0
173
+
174
+ else
123
175
 
124
- if this_object.nil?
125
- if @sort_order_reversed
126
- super( count, this_object )
127
- else
128
- super( 0, this_object )
129
- end
130
- next
131
- end
132
-
133
176
  self.each_with_index do |this_member, this_index|
134
-
135
- insert_sort_object = this_object
177
+
178
+ insert_sort_object = object
136
179
  existing_sort_object = this_member
137
-
180
+
138
181
  if @sort_object_block
139
- insert_sort_object = @sort_object_block.call( this_object )
140
- existing_sort_object = @sort_object_block.call( this_member )
182
+ insert_sort_object = @sort_object_block.call( object )
183
+ existing_sort_object = @sort_object_block.call( object )
141
184
  end
142
-
185
+
143
186
  if @sort_order_reversed
144
-
187
+
145
188
  case insert_sort_object <=> existing_sort_object
146
189
  when 0, 1
147
- super( this_index, this_object )
148
- insert_occurred = true
190
+ index = this_index
149
191
  break
150
192
  end
151
-
193
+
152
194
  else
153
-
195
+
154
196
  case insert_sort_object <=> existing_sort_object
155
197
  when 0, -1
156
- super( this_index, this_object )
157
- insert_occurred = true
198
+ index = this_index
158
199
  break
159
200
  end
160
-
201
+
161
202
  end
162
-
203
+
163
204
  end
164
-
165
- unless insert_occurred
166
- super( count, this_object )
205
+
206
+ unless index
207
+ index = count
167
208
  end
168
-
209
+
169
210
  end
211
+
212
+ return index
213
+
214
+ end
170
215
 
216
+ ################################################
217
+ # perform_single_object_insert_between_hooks #
218
+ ################################################
219
+
220
+ def perform_single_object_insert_between_hooks( index, object )
221
+
222
+ insert_index = get_index_for_sorted_insert( object )
223
+
224
+ perform_unsorted_insert( insert_index, object )
225
+
226
+ return insert_index
227
+
171
228
  end
172
229
 
173
230
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array-sorted
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
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-06-30 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: array-hooked
@@ -40,8 +40,7 @@ files:
40
40
  - lib/array/sorted.rb
41
41
  - lib/array-hooked-sorted.rb
42
42
  - lib/array-sorted.rb
43
- - lib/hooked/array/sorted.rb
44
- - spec/Array/Sorted_spec.rb
43
+ - spec/array/sorted_spec.rb
45
44
  - README.md
46
45
  - CHANGELOG.rdoc
47
46
  homepage: http://rubygems.org/gems/array-sorted
@@ -1,2 +0,0 @@
1
-
2
- require_relative '../../array-sorted.rb'