From 44702b5edd656ea93adfd5a2e9d7b842f10afef5 Mon Sep 17 00:00:00 2001 From: grosser Date: Sun, 1 Jul 2012 20:05:02 -0700 Subject: [PATCH] cleanup --- lib/memflash.rb | 37 +++++++++++++++++++------------------ test/memflash_test.rb | 32 +++++++++++--------------------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/lib/memflash.rb b/lib/memflash.rb index a14d78e..5d5c183 100644 --- a/lib/memflash.rb +++ b/lib/memflash.rb @@ -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 @@ -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 diff --git a/test/memflash_test.rb b/test/memflash_test.rb index 0a55fac..198420d 100644 --- a/test/memflash_test.rb +++ b/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 @@ -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" @@ -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