wallet 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/tv/config/environment.rb +2 -31
- data/examples/tv/lib/wallet.rb +30 -19
- data/lib/wallet/action_controller.rb +19 -19
- data/lib/wallet/wallet.rb +18 -1
- data/lib/wallet.rb +1 -0
- data/spec/lib_specs/wallet_spec.rb +12 -12
- metadata +1 -2
- data/examples/tv/config/initializers/wallet.rb +0 -1
@@ -7,35 +7,6 @@ RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION
|
|
7
7
|
require File.join(File.dirname(__FILE__), 'boot')
|
8
8
|
|
9
9
|
Rails::Initializer.run do |config|
|
10
|
-
|
11
|
-
# Application configuration should go into files in config/initializers
|
12
|
-
# -- all .rb files in that directory are automatically loaded.
|
13
|
-
|
14
|
-
# Add additional load paths for your own custom dirs
|
15
|
-
# config.load_paths += %W( #{RAILS_ROOT}/extras )
|
16
|
-
|
17
|
-
# Specify gems that this application depends on and have them installed with rake gems:install
|
18
|
-
# config.gem "bj"
|
19
|
-
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
|
20
|
-
# config.gem "sqlite3-ruby", :lib => "sqlite3"
|
21
|
-
# config.gem "aws-s3", :lib => "aws/s3"
|
22
|
-
|
23
|
-
# Only load the plugins named here, in the order given (default is alphabetical).
|
24
|
-
# :all can be used as a placeholder for all plugins not explicitly named
|
25
|
-
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
26
|
-
|
27
|
-
# Skip frameworks you're not going to use. To use Rails without a database,
|
28
|
-
# you must remove the Active Record framework.
|
29
|
-
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
|
30
|
-
|
31
|
-
# Activate observers that should always be running
|
32
|
-
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
33
|
-
|
34
|
-
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
35
|
-
# Run "rake -D time" for a list of tasks for finding time zone names.
|
10
|
+
config.gem "wallet", :lib => 'wallet', :version => '>=0.0.1', :source => "http://gemcutter.org"
|
36
11
|
config.time_zone = 'UTC'
|
37
|
-
|
38
|
-
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
39
|
-
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
|
40
|
-
# config.i18n.default_locale = :de
|
41
|
-
end
|
12
|
+
end
|
data/examples/tv/lib/wallet.rb
CHANGED
@@ -44,6 +44,7 @@ class Wallet
|
|
44
44
|
yml = YAML.load config_yml rescue nil
|
45
45
|
@config = yml || {}
|
46
46
|
@default_ttl = (eval(@config["default_ttl"]).to_i || 60) rescue 60
|
47
|
+
setup_action_caching
|
47
48
|
end
|
48
49
|
|
49
50
|
# Returns true or false based on whether or not a controller action is configured for caching in the wallet
|
@@ -87,26 +88,36 @@ class Wallet
|
|
87
88
|
return *args
|
88
89
|
end
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
def wallet
|
97
|
-
@wallet ||= Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
|
98
|
-
controller_class_name = self.class.name
|
99
|
-
controller = controller_class_name.underscore.gsub(/_controller$/, '')
|
100
|
-
|
101
|
-
# if we haven't already setup action caching on this controller
|
102
|
-
if @@action_cached_controllers[controller] == nil
|
103
|
-
|
104
|
-
@@action_cached_controllers[controller] = controller
|
105
|
-
@wallet.cached_actions(controller).each do |action|
|
106
|
-
puts "I'm setting up action caching for #{controller_class_name}::#{action}"
|
107
|
-
controller_class_name.constantize.send :caches_action, action.to_sym, :expires_in => @wallet.ttl(controller, action)
|
91
|
+
def setup_action_caching
|
92
|
+
@config.each do |controller, actions|
|
93
|
+
controller_class = (controller + "_controller").camelize.constantize
|
94
|
+
cached_actions(controller).each do |action|
|
95
|
+
controller_class.send :caches_action, action.to_sym, :expires_in => ttl(controller, action)
|
108
96
|
end
|
109
97
|
end
|
110
98
|
end
|
111
|
-
|
112
99
|
end
|
100
|
+
|
101
|
+
Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
|
102
|
+
|
103
|
+
#ActionController::Base.class_eval do
|
104
|
+
# before_filter :wallet
|
105
|
+
# @@action_cached_controllers = {}
|
106
|
+
#
|
107
|
+
# def wallet
|
108
|
+
# @wallet ||= Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
|
109
|
+
# controller_class_name = self.class.name
|
110
|
+
# controller = controller_class_name.underscore.gsub(/_controller$/, '')
|
111
|
+
#
|
112
|
+
# # if we haven't already setup action caching on this controller
|
113
|
+
# if @@action_cached_controllers[controller] == nil
|
114
|
+
#
|
115
|
+
# @@action_cached_controllers[controller] = controller
|
116
|
+
# @wallet.cached_actions(controller).each do |action|
|
117
|
+
# puts "I'm setting up action caching for #{controller_class_name}::#{action}"
|
118
|
+
# controller_class_name.constantize.send :caches_action, action.to_sym, :expires_in => @wallet.ttl(controller, action)
|
119
|
+
# end
|
120
|
+
# end
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
#end
|
@@ -1,19 +1,19 @@
|
|
1
|
-
ActionController::Base.class_eval do
|
2
|
-
before_filter :wallet
|
3
|
-
@@action_cached_controllers = {}
|
4
|
-
|
5
|
-
def wallet
|
6
|
-
@wallet ||= Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
|
7
|
-
controller_class_name = self.class.name
|
8
|
-
controller = controller_class_name.underscore.gsub(/_controller$/, '')
|
9
|
-
|
10
|
-
# if we haven't already setup action caching on this controller
|
11
|
-
if @@action_cached_controllers[controller] == nil
|
12
|
-
@@action_cached_controllers[controller] = controller
|
13
|
-
@wallet.cached_actions(controller).each do |action|
|
14
|
-
controller_class_name.constantize.send :caches_action, action.to_sym, :expires_in => @wallet.ttl(controller, action)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end if defined?(ActionController::Base)
|
1
|
+
#ActionController::Base.class_eval do
|
2
|
+
# before_filter :wallet
|
3
|
+
# @@action_cached_controllers = {}
|
4
|
+
#
|
5
|
+
# def wallet
|
6
|
+
# @wallet ||= Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
|
7
|
+
# controller_class_name = self.class.name
|
8
|
+
# controller = controller_class_name.underscore.gsub(/_controller$/, '')
|
9
|
+
#
|
10
|
+
# # if we haven't already setup action caching on this controller
|
11
|
+
# if @@action_cached_controllers[controller] == nil
|
12
|
+
# @@action_cached_controllers[controller] = controller
|
13
|
+
# @wallet.cached_actions(controller).each do |action|
|
14
|
+
# controller_class_name.constantize.send :caches_action, action.to_sym, :expires_in => @wallet.ttl(controller, action)
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
#end if defined?(ActionController::Base)
|
data/lib/wallet/wallet.rb
CHANGED
@@ -5,7 +5,10 @@ class Wallet
|
|
5
5
|
|
6
6
|
# This error gets thrown whenever you attempt to retreive a TTL
|
7
7
|
# for an action that's not in the wallet.
|
8
|
-
class ActionNotCached <
|
8
|
+
class ActionNotCached < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
class UnknownController < StandardError
|
9
12
|
end
|
10
13
|
|
11
14
|
attr_reader :config #:nodoc:
|
@@ -44,6 +47,8 @@ class Wallet
|
|
44
47
|
yml = YAML.load config_yml rescue nil
|
45
48
|
@config = yml || {}
|
46
49
|
@default_ttl = (eval(@config["default_ttl"]).to_i || 60) rescue 60
|
50
|
+
@config.delete "default_ttl"
|
51
|
+
setup_action_caching
|
47
52
|
end
|
48
53
|
|
49
54
|
# Returns true or false based on whether or not a controller action is configured for caching in the wallet
|
@@ -87,4 +92,16 @@ class Wallet
|
|
87
92
|
return *args
|
88
93
|
end
|
89
94
|
|
95
|
+
def setup_action_caching
|
96
|
+
@config.each do |controller, actions|
|
97
|
+
begin
|
98
|
+
controller_class = (controller + "_controller").camelize.constantize
|
99
|
+
cached_actions(controller).each do |action|
|
100
|
+
controller_class.send :caches_action, action.to_sym, :expires_in => ttl(controller, action)
|
101
|
+
end
|
102
|
+
rescue Exception => e
|
103
|
+
#raise UnknownController.new("We're sorry, but the controller '#{(controller + "_controller").camelize}' does not exist in app/controllers/")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
90
107
|
end
|
data/lib/wallet.rb
CHANGED
@@ -4,7 +4,7 @@ describe Wallet do
|
|
4
4
|
describe "Wallet::new" do
|
5
5
|
it "should load the config on initialization if provided with a path to a yaml file" do
|
6
6
|
YAML.should_receive(:load).once
|
7
|
-
wallet = Wallet.new(
|
7
|
+
wallet = Wallet.new("channels:\n adult_swim:")
|
8
8
|
wallet.config.should_not == false
|
9
9
|
end
|
10
10
|
|
@@ -32,41 +32,41 @@ describe Wallet do
|
|
32
32
|
|
33
33
|
describe "Wallet::cached?" do
|
34
34
|
before do
|
35
|
-
config_yml = "
|
35
|
+
config_yml = "channels:\n cartoon_network:\n"
|
36
36
|
@wallet = Wallet.new config_yml
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should return true for a controller/action that is cached" do
|
40
|
-
@wallet.cached?("
|
40
|
+
@wallet.cached?("channels", "cartoon_network").should == true
|
41
41
|
end
|
42
42
|
|
43
43
|
it "should return false for a controller/action that is not cached" do
|
44
44
|
@wallet.cached?("blah", "blah").should == false
|
45
|
-
@wallet.cached?("
|
45
|
+
@wallet.cached?("channels", "blah").should == false
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should work with both strings and symbols" do
|
49
|
-
@wallet.cached?("
|
50
|
-
@wallet.cached?(:
|
49
|
+
@wallet.cached?("channels", "cartoon_network").should == true
|
50
|
+
@wallet.cached?(:channels, :cartoon_network).should == true
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe "Wallet::ttl" do
|
55
55
|
before do
|
56
|
-
config_yml = "
|
56
|
+
config_yml = "channels:\n cartoon_network: 5.minute\n current_tv: "
|
57
57
|
@wallet = Wallet.new config_yml
|
58
58
|
end
|
59
59
|
|
60
|
-
it "should return 5 minutes for the
|
61
|
-
@wallet.ttl(:
|
60
|
+
it "should return 5 minutes for the channels:cartoon_network ttl" do
|
61
|
+
@wallet.ttl(:channels, :cartoon_network).should == 5.minutes.to_i
|
62
62
|
end
|
63
63
|
|
64
|
-
it "should return the default ttl for the
|
65
|
-
@wallet.ttl(:
|
64
|
+
it "should return the default ttl for the current_tv action" do
|
65
|
+
@wallet.ttl(:channels, :current_tv).should == @wallet.default_ttl
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should raise a ActionNotCached error for the dne action" do
|
69
|
-
proc {@wallet.ttl(:
|
69
|
+
proc {@wallet.ttl(:channels, :dne)}.should raise_error(Wallet::ActionNotCached)
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wallet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Parker
|
@@ -79,7 +79,6 @@ test_files:
|
|
79
79
|
- examples/tv/config/initializers/mime_types.rb
|
80
80
|
- examples/tv/config/initializers/new_rails_defaults.rb
|
81
81
|
- examples/tv/config/initializers/session_store.rb
|
82
|
-
- examples/tv/config/initializers/wallet.rb
|
83
82
|
- examples/tv/config/routes.rb
|
84
83
|
- examples/tv/lib/wallet.rb
|
85
84
|
- examples/tv/test/performance/browsing_test.rb
|
@@ -1 +0,0 @@
|
|
1
|
-
require 'wallet'
|