Skip to content

Commit

Permalink
Added timestampable and update_in_list
Browse files Browse the repository at this point in the history
  • Loading branch information
treeder committed Sep 17, 2012
1 parent 348ebe3 commit a945e45
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ocm (0.0.2)
ocm (0.0.3)
iron_cache
jsonable

Expand Down
1 change: 1 addition & 0 deletions lib/ocm.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative 'ocm/version'
require_relative 'ocm/timestampable'
require_relative 'ocm/ormable'
require_relative 'ocm/iron_cache_orm'
39 changes: 37 additions & 2 deletions lib/ocm/iron_cache_orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def save(idable)
unless idable.id
idable.id = Idable.generate_id
end
if defined? idable.set_timestamps
idable.set_timestamps
end
put(key_for(idable), idable)

end

def save_list(key, array)
Expand All @@ -38,12 +40,41 @@ def get_list(key)
messages
end

# first item that matches comps is replaced with item.
def update_in_list(key, item, comps)
messages = get_list(key)
messages.each_with_index do |m,i|
match = true
comps.each_pair do |k,v|
if m.is_a?(Hash)
if m[k] != v
match = false
break
end
else
if m.__send__(k.to_sym) != v
match = false
break
end
end
end
if match
messages[i] = item
put(key, messages)
return
end
end
raise "No matching item found in list"
end

# Warning, this is not a safe operation, be sure it is only being called once at a time
def prepend_to_list(key, item)
messages = get_list(key)
messages.unshift(item)
put(key, messages)
end

# Warning, this is not a safe operation, be sure it is only being called once at a time
def append_to_list(key, item)
messages = get_list(key)
messages.push(item)
Expand Down Expand Up @@ -85,14 +116,18 @@ def put(key, value, options={})
@cache.put(key, value, options)
end

def remove(idable, id=nil)
delete(key_for(idable, id))
end

def delete(key)
@cache.delete(key)
end

def increment(key, value=1)
puts 'INC'
begin
ret = @cache.increment(key)
ret = @cache.increment(key, value)
puts 'INC RET ' + ret.inspect
return ret.value
rescue Rest::HttpError, IronCore::ResponseError => ex
Expand Down
30 changes: 30 additions & 0 deletions lib/ocm/timestampable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Ocm
module Timestampable

def created_at
@created_at
end

def created_at=(t)
@created_at = t
end

def updated_at
@updated_at
end

def updated_at=(t)
@updated_at = t
end

def set_timestamps
time = Time.now.utc
if !created_at
self.created_at = time
end
self.updated_at = time
end

end
end

0 comments on commit a945e45

Please sign in to comment.