sid_list 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcb011cca900e1071244d7ad7cc3b0190f4fadff
4
- data.tar.gz: 364210567619c5d65854c032d93b49890430f61a
3
+ metadata.gz: a74f514b61e1880376746c2b727ed403b6cc67f2
4
+ data.tar.gz: 640a8460a5ef9a01115a32e5ef7e01252bde20c5
5
5
  SHA512:
6
- metadata.gz: cdede409505f1ddba9077db2db90b1c4306d979ca1a966505ba5778505528246e855134cb237db800a9b7262a1476bea17fdc97d170962be5702b5b1887e5b42
7
- data.tar.gz: 5ddc2432944f46dbd8b7b4cff6e60f32b5924d7352540b026b835985e344c5756b98ce1549dfd2b199482b41738537a9f7081be7646fa2bfdd3c78a1c74980f1
6
+ metadata.gz: 974a122ebb91220ef9b697a77c3a2fa8ce549d75e7118bf95604bb5e861f5df3e0c983f2aea19b734973d0f83418cae30735b76888904dec9389109a1d1c5f64
7
+ data.tar.gz: ae0ab79b7afb76706cecaafa35b5a7393eb199b8e403eda613669ed0815f9c2398fb4b4e8dca98cee953f6db4154dd29e71b20040892aec6319eb18311020e11
data/README.rdoc CHANGED
@@ -8,25 +8,25 @@ Sid List is a list I created for one of the servers I developed. The idea behind
8
8
 
9
9
  As I had previously developed MdlSql (a modular sql gem), configuring the gem as I wanted proved itself clear and easy. For example, in case I wanted to make a list of instances which would be loaded from Mysql, it would go the following way:
10
10
 
11
- require 'mdlsql'
12
- class InstanceList < SidList
13
- def load_hash
14
- results = MdlSql::select.from(:instances).where(:status, 1, '>').execute
15
- end
16
- def update_hash time
17
- results = MdlSql::select.from(:instances).where("status > 1 AND updated_at > #{time}").execute
18
- end
19
- def new_obj obj_data
20
- obj = Instance.new obj_data
21
- end
22
- end
11
+ require 'mdlsql'
12
+ class InstanceList < SidList
13
+ def load_hash
14
+ results = MdlSql::select.from(:instances).where(:status, 1, '>').execute
15
+ end
16
+ def update_hash time
17
+ results = MdlSql::select.from(:instances).where("status > 1 AND updated_at > #{time}").execute
18
+ end
19
+ def new_obj obj_data
20
+ obj = Instance.new obj_data
21
+ end
22
+ end
23
23
 
24
24
  And with this I could just go straight to work with the list:
25
25
 
26
- il = InstanceList.new
27
- il.load
28
- il.ready.first # Get the first of the 'ready' instance
29
- il.update
26
+ il = InstanceList.new
27
+ il.load
28
+ il.ready.first # Get the first of the 'ready' instance
29
+ il.update
30
30
 
31
31
  == License
32
32
 
@@ -45,4 +45,4 @@ GNU General Public License for more details.
45
45
  You should have received a copy of the GNU General Public License
46
46
  along with this program. If not, see <http://www.gnu.org/licenses/>.
47
47
 
48
- In order to contact the author of this gem, please write to sikian@gmail.com.
48
+ In order to contact the author of this gem, please write to sikian@gmail.com.
@@ -1,3 +1,3 @@
1
1
  class SidList
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/sid_list.rb CHANGED
@@ -16,23 +16,23 @@ class SidList
16
16
  #
17
17
  # @params values [Hash] Params. for search.
18
18
  # @params values [Fixnum] Id.
19
- def find values={}
19
+ def find values
20
20
  found = Array.new
21
21
 
22
22
  if values.is_a? Fixnum
23
23
  return @list_by_id[values]
24
24
  # values = {:id => values}
25
25
  elsif !values.is_a? Hash
26
- raise ArgumentError, 'argument must be a Hash or a Fixnum.'
26
+ raise ArgumentError, 'Argument must be a Hash or a Fixnum.'
27
27
  end
28
28
 
29
29
  @list_by_status.each do |status, status_list|
30
30
 
31
31
  status_list.each do |obj|
32
32
 
33
- # Catch is now acting as an OR condition.
33
+ # Catch is now acting as an AND condition.
34
34
  # Maybe it would be a good idea to do it an AND
35
- # @todo change this to AND
35
+ # @todo make OR condition available
36
36
  catch :not_equal do
37
37
  values.each do |term, value|
38
38
  if obj.send(term)!= value
@@ -45,10 +45,23 @@ class SidList
45
45
  end
46
46
  end
47
47
 
48
+ # if found.length == 1
49
+ # found = found.first
50
+ # end
51
+
52
+ # Return just an array, even if it's empty!
48
53
  return found
49
54
  end
50
55
 
56
+ def [] id
57
+ @list_by_id[id]
58
+ end
59
+
51
60
  def add obj
61
+ if obj.is_a? Hash
62
+ obj = new_obj obj
63
+ end
64
+
52
65
  @list_by_id[obj.id] = obj # Add obj by id
53
66
 
54
67
  # Add obj by status. ensure_status_in_list is called to make sure that the status
@@ -94,7 +107,7 @@ class SidList
94
107
  # @todo better time options
95
108
  # @todo find a way to move adding here without cycling again
96
109
  def update opts={}
97
- time = opts[:time]? opts[:time] : Time.now.utc
110
+ time = opts[:time]? opts[:time] : Time.now#.utc
98
111
  update_objs time
99
112
  end
100
113
 
@@ -119,16 +132,29 @@ class SidList
119
132
 
120
133
  # Using opts[:old_status]
121
134
  if opts[:old_status]
122
- if @list_by_status[:old_status].delete(obj)
135
+ if @list_by_status[opts[:old_status]].delete(obj)
123
136
  deleted = true
124
137
  end
138
+
139
+ # Delete status in @list_by_status if it's empty
140
+ # Useful if status hasn't been already changed.
141
+ if @list_by_status[opts[:old_status]].length == 0
142
+ @list_by_status.delete opts[:old_status]
143
+ end
125
144
  end
126
145
 
127
146
  # Using obj.status
128
- unless deleted && obj.status == new_status
129
- if @list_by_status[obj.status].delete(obj)
147
+ unless deleted || obj.status == new_status
148
+ status = obj.status
149
+ # puts obj.status
150
+ if @list_by_status[status].delete(obj)
130
151
  deleted = true
131
152
  end
153
+
154
+ # Delete status in @list_by_status if it's empty
155
+ if @list_by_status[status].length == 0
156
+ @list_by_status.delete status
157
+ end
132
158
  end
133
159
 
134
160
  # Search through @list_by_status
@@ -138,6 +164,11 @@ class SidList
138
164
  if status_list.include? obj
139
165
  @list_by_status[status].delete obj
140
166
  deleted = true
167
+
168
+ # Delete status in @list_by_status if it's empty
169
+ if @list_by_status[status].length == 0
170
+ @list_by_status.delete status
171
+ end
141
172
  throw :deleted
142
173
  end
143
174
  end
@@ -155,6 +186,35 @@ class SidList
155
186
  return deleted
156
187
  end
157
188
 
189
+ def count
190
+ @list_by_id.compact.count
191
+ end
192
+
193
+ def first
194
+ i = @list_by_id.find_index { |x| x }
195
+ @list_by_id[i]
196
+ end
197
+
198
+ def compact
199
+ @list_by_id.compact
200
+ end
201
+
202
+ def list_status *values
203
+ @list_by_status.each do |stat, arr|
204
+ puts "#{stat}:"
205
+ arr.each do |obj|
206
+ print "\t [#{obj.id}]"
207
+ values.each do |v|
208
+ print "\t #{obj.send v}" if obj.respond_to? v
209
+ end
210
+ print "\n"
211
+ end
212
+ end
213
+ return nil
214
+ end
215
+
216
+ alias_method :to_s, :list_status
217
+
158
218
  private
159
219
 
160
220
  ###
@@ -166,21 +226,23 @@ class SidList
166
226
 
167
227
  # @return [Array] data to be loaded into list
168
228
  # @note This method must be overwritten to configure the list.
169
- def load_hash
229
+ def load_data
170
230
  raise 'StatusList#load_hash() has not been overwritten to allow data to be loaded into the list.'
171
231
 
172
232
  return Array.new
173
233
  end
234
+ alias_method :load_hash, :load_data
174
235
 
175
- # @return [Hash] values to be updated/added. Please see example to see how this hash should be.
236
+ # @return [Array] values to be updated/added. Please see example to see how this hash should be.
176
237
  # @example update_hash's return
177
- # update_hash(now) # => {:created => [{:id => 1, ...}, ...], :updated => [{:id => 10, ...}, ...]}
238
+ # update_hash(now) # => [{:id => 1, ...}, ...]
178
239
  # @note This method must be overwritten to configure the list.
179
- def update_hash time
240
+ def update_data time
180
241
  raise 'StatusList#update_hash() has not been overwritten to allow data in the list to be updated.'
181
242
 
182
- return Hash.new
243
+ return Array.new
183
244
  end
245
+ alias_method :update_hash, :update_data
184
246
 
185
247
  # @param obj_data [Hash] data to be loaded to the Object.
186
248
  # @return [Object] new Object the list is composed of.
@@ -190,7 +252,7 @@ class SidList
190
252
  end
191
253
 
192
254
  def load_objs
193
- list_data = load_hash()
255
+ list_data = load_data()
194
256
  obj_array = Array.new()
195
257
 
196
258
  list_data.each do |obj_data|
@@ -201,24 +263,37 @@ class SidList
201
263
  end
202
264
 
203
265
  # Uses #update_hash to get all objects to be added or edited.
266
+ # @since 0.0.4 no longer need :updated and :created
204
267
  # @param time [Time] time to pass to update_hash in order to get new changes.
205
- # @note update_hash must return a Hash with keys :created and :updated (see #update_hash).
206
268
 
207
269
  def update_objs time
208
- list_data = update_hash(time)
209
- created_obj_array = Array.new()
210
- updated_obj_array = Array.new()
270
+ list_data = update_data(time)
211
271
 
212
- if list_data[:created]
213
- list_data[:created].each do |obj_data|
214
- add new_obj obj_data
215
- end
216
- end
217
- if list_data[:updated]
218
- list_data[:updated].each do |obj_data|
219
- edit_obj obj_data
272
+ # @note the update can be taken care of in update_data if it returns nil.
273
+
274
+ if list_data
275
+ created_obj_array = Array.new()
276
+ updated_obj_array = Array.new()
277
+
278
+ list_data.each do |obj_data|
279
+ if find obj_data[:id]
280
+ edit_obj obj_data
281
+ else
282
+ add new_obj obj_data
283
+ end
220
284
  end
221
285
  end
286
+
287
+ # if list_data[:created]
288
+ # list_data[:created].each do |obj_data|
289
+ # add new_obj obj_data
290
+ # end
291
+ # end
292
+ # if list_data[:updated]
293
+ # list_data[:updated].each do |obj_data|
294
+ # edit_obj obj_data
295
+ # end
296
+ # end
222
297
  end
223
298
 
224
299
  # Updates/Edits an object that already exists.
@@ -230,8 +305,12 @@ class SidList
230
305
  obj = @list_by_id[obj_data[:id]]
231
306
 
232
307
  if obj
233
- obj_data.each do |key,value|
234
- obj.send("#{key}=", value)
308
+ if obj.respond_to? 'update_with_hash'
309
+ obj.update_with_hash obj_data
310
+ else
311
+ obj_data.each do |key,value|
312
+ obj.send("#{key}=", value) if obj.respond_to? key
313
+ end
235
314
  end
236
315
  end
237
316
 
@@ -247,13 +326,14 @@ class SidList
247
326
  unless @list_by_status[status].is_a? Array
248
327
  @list_by_status[status] = Array.new
249
328
  end
329
+ if !(status.is_a? Symbol || status.is_a?(String))
330
+ raise "Object's status has no name (status #{status} was passed)."
331
+ end
250
332
  unless self.respond_to?(status)
251
- puts 'i dont respond'
252
333
  self.class.send(:define_method, status) do
253
334
  @list_by_status[status]
254
335
  end
255
336
  end
256
-
257
337
  return @list_by_status[status]
258
338
  end
259
339
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sid_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sikian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-02 00:00:00.000000000 Z
11
+ date: 2013-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler