Skip to content

Commit

Permalink
[NPE-5] Address Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
ojameso committed Apr 27, 2023
1 parent 2530a3f commit 04250df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fakememcached (0.1.1)
fakememcached (0.1.3)

GEM
remote: http://rubygems.org/
Expand All @@ -17,3 +17,6 @@ DEPENDENCIES
fakememcached!
rake
rspec (~> 1.3.0)

BUNDLED WITH
2.1.4
24 changes: 19 additions & 5 deletions lib/fakememcached/cache_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class CacheEntry

def initialize(value, ttl, marshal)
if marshal
@value = Marshal.dump(value)
@value = singleton_safe_marshal(value)
else
@value = value.to_s
end

@expires_at = Time.now + ttl
end

def expired?
Time.now > @expires_at
end
Expand All @@ -23,13 +23,27 @@ def increment(amount = 1)
def decrement(amount = 1)
@value = (@value.to_i - amount).to_s
end

def unmarshal
Marshal.load(@value)
singleton_safe_unmarshal
end

def to_i
@value.to_i
end

def singleton_safe_marshal(value)
# Rspec mocks create singletons out of objects in order to track calls
# However, singletons can't be dumped, so skip marshaling
return value if value.singleton_methods.any?
Marshal.dump(value)
end

def singleton_safe_unmarshal
# Rspec mocks create singletons out of objects in order to track calls
# However, singletons can't be dumped, so skip marshaling
return value if value.singleton_methods.any?
Marshal.load(value)
end
end
end
2 changes: 1 addition & 1 deletion lib/fakememcached/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class FakeMemcached
module Version
MAJOR = 0
MINOR = 1
PATCH = 2
PATCH = 3
STRING = [MAJOR, MINOR, PATCH].join('.')
end
end

0 comments on commit 04250df

Please sign in to comment.