Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jul 2, 2012
1 parent 3f7d1f0 commit 44702b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
37 changes: 19 additions & 18 deletions lib/memflash.rb
Expand Up @@ -2,15 +2,14 @@ module Memflash
mattr_accessor :threshold
self.threshold = 300 # Messages longer than this will be stored in Rails.cache

def self.included(base)
base.class_eval do
include InstanceMethods
alias_method_chain :[]=, :caching
alias_method_chain :[], :caching
module CachingLayer
def self.included(base)
base.class_eval do
alias_method_chain :[]=, :caching
alias_method_chain :[], :caching
end
end
end

module InstanceMethods
define_method "[]_with_caching=" do |key, value|
value_for_hash = value

Expand All @@ -28,21 +27,23 @@ module InstanceMethods
end

private
def memflash_key(hash_key)
"Memflash-#{hash_key}-#{Time.now.to_f}-#{Kernel.rand}"
end

def memflashed?(key, value)
!!(value =~ /^Memflash-#{key}/)
end
end # InstanceMethods
end # Memflash
def memflash_key(hash_key)
"Memflash-#{hash_key}-#{Time.now.to_f}-#{Kernel.rand}"
end

def memflashed?(key, value)
!!(value =~ /^Memflash-#{key}/)
end
end
end

require 'action_pack/version'
if ActionPack::VERSION::MAJOR >= 3
base = if ActionPack::VERSION::MAJOR >= 3
require 'action_dispatch'
ActionDispatch::Flash::FlashHash.class_eval { include Memflash }
ActionDispatch::Flash::FlashHash
else
require 'action_controller'
ActionController::Flash::FlashHash.class_eval { include Memflash }
ActionController::Flash::FlashHash
end
base.send :include, Memflash::CachingLayer
32 changes: 11 additions & 21 deletions test/memflash_test.rb
@@ -1,25 +1,26 @@
require "test_helper"

class MemflashTest < Test::Unit::TestCase
class EnhancedHash < Hash
include Memflash
end

def setup
@hash = EnhancedHash.new
base = if ActionPack::VERSION::MAJOR >= 3
ActionDispatch::Flash::FlashHash
else
ActionController::Flash::FlashHash
end
@hash = base.new
end

context "A memflash-enhanced Hash" do
context "Flash::FlashHash" do
should "have a caching-enabled []" do
assert @hash.respond_to?("[]_with_caching")
end

should "have a caching-enabled []=" do
assert @hash.respond_to?("[]_with_caching=")
end
end
context "In a memflash-enhanced Hash, storing a value" do

context "storing a value" do
context "that is a String" do
context "shorter than Memflash.threshold" do
should "not affect the cache" do
Expand Down Expand Up @@ -73,7 +74,7 @@ def setup
end
end

context "From a memflash-enhanced Hash, reading by a key" do
context "reading a key" do
should "check whether the value in the hash was memflashed" do
@hash[:hello] = "world"

Expand Down Expand Up @@ -107,15 +108,4 @@ def setup
end
end
end

context "Flash::FlashHash" do
should "be automatically cache-enhanced" do
base = if ActionPack::VERSION::MAJOR >= 3
ActionDispatch::Flash::FlashHash
else
ActionController::Flash::FlashHash
end
assert base.ancestors.include?(Memflash)
end
end
end

0 comments on commit 44702b5

Please sign in to comment.