Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewrite to accommodate application’s config.

  • Loading branch information...
commit cdb1bae81f0cde2585b53343b7b092c4bad9be37 1 parent d68fa7a
@chauhonglinh2 chauhonglinh2 authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+Gemfile.lock
+.ruby-version
View
22 README.md
@@ -1,5 +1,27 @@
+[![Gem Version](https://badge.fury.io/rb/object_cacheable.svg)](http://badge.fury.io/rb/object_cacheable)
+
This is a gem that provides Ruby objects with the capability of object caching. This gem is framework-agnostic, so it can be used in all the types of Ruby applications.
+### Installation
+
+Use **gem**:
+```
+gem install object_cacheable
+```
+
+Use **bundle**:<br/>
+In Gemfile, write:
+```
+gem 'object_cacheable'
+```
+
+In your application, write:
+```
+require 'object_cacheable'
+```
+
+In your application, there must be a directory called **conf** or **config** (either will work) that contains a file called [**cache_configs.yml**](https://github.com/linhchauatl/object_cacheable/blob/master/conf/cache_configs.yml).<br/>
+
Object that wants to be cached must include Cacheable,
class Person
View
0  lib/cache_configs.yml → conf/cache_configs.yml 100644 → 100755
File renamed without changes
View
60 lib/cache_configuration.rb 100644 → 100755
@@ -14,28 +14,42 @@ class CacheConfiguration
@@cache_instance = nil
@@logger_instance = nil
- def self.load_config
- configs = YAML.load_file("#{File.dirname(__FILE__)}/cache_configs.yml")
- env = environment
+ class << self
+ def load_config
+ base_path = File.expand_path('.')
+ config_file = nil
+ ['conf', 'config'].each do |sub_path|
+ if File.exist?("#{base_path}/#{sub_path}/cache_configs.yml")
+ config_file = "#{base_path}/#{sub_path}/cache_configs.yml"
+ end
+ end
+
+ raise("You must have the file cache_configs.yml in either 'conf' or 'config' directory of your application") unless config_file
+
+ configs = YAML.load_file(config_file)
+ env = environment
+
+ @@cache_instance = eval(configs[env]['cache'])
+ @@logger_instance = eval(configs[env]['logger'])
+ end
- @@cache_instance = eval(configs[env]['cache'])
- @@logger_instance = eval(configs[env]['logger'])
- end
-
- def self.environment
- Rails.env
- rescue => error
- 'test'
- end
-
- def self.cache
- load_config if @@cache_instance.nil?
- @@cache_instance
- end
-
- def self.logger
- load_config if @@logger_instance.nil?
- @@logger_instance
- end
- end
+ def environment
+ return Rails.env if defined? Rails
+ return ENV['RACK_ENV'] if ENV['RACK_ENV']
+ 'test'
+ rescue => error
+ 'test'
+ end
+
+ def cache
+ load_config if @@cache_instance.nil?
+ @@cache_instance
+ end
+
+ def logger
+ load_config if @@logger_instance.nil?
+ @@logger_instance
+ end
+ end # class methods
+ end # class CacheConfiguration
end
View
1  lib/object_cacheable.rb
@@ -0,0 +1 @@
+require "#{File.dirname(__FILE__)}/cacheable"
View
7 object_cacheable.gemspec 100644 → 100755
@@ -1,14 +1,15 @@
Gem::Specification.new do |s|
s.name = 'object_cacheable'
- s.version = '1.0.0'
+ s.version = '1.1.2'
s.summary = "Object-level caching"
s.description = "A gem for object-level caching."
s.authors = ['Linh Chau']
s.email = 'chauhonglinh@gmail.com'
s.files = [
- './Gemfile', './object_cacheable.gemspec',
- 'lib/cache_configs.yml', 'lib/cache_configuration.rb', 'lib/cache_utils.rb', 'lib/cacheable.rb',
+ './Gemfile', './object_cacheable.gemspec', 'lib/object_cacheable.rb',
+ 'conf/cache_configs.yml', 'lib/cache_configuration.rb', 'lib/cache_utils.rb', 'lib/cacheable.rb',
'test/models/cacheable_test.rb'
]
s.homepage = 'https://github.com/linhchauatl/object_cacheable.git'
+ s.license = 'MIT'
end
View
7 test/models/cacheable_test.rb
@@ -66,9 +66,10 @@ def ==(obj)
cached_obj.must_equal obj
# Make sure that it doesn't make a real call to fetch the second time
- TestCacheable.stubs(:fetch).returns(nil) # If this gets called, the result_obj will be nil
- result_obj = TestCacheable.fetch_by_id(1)
- result_obj.must_equal obj
+ TestCacheable.stub(:fetch, nil) do
+ result_obj = TestCacheable.fetch_by_id(1)
+ result_obj.must_equal obj
+ end
# clean the cache after finish
obj.delete_from_cache
Please sign in to comment.
Something went wrong with that request. Please try again.