ohm-expire 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 José P. Airosa
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ ohm-expire
2
+ ==========
3
+
4
+ Ohm plugin that exposes ttl control over modules
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require "rake/testtask"
2
+
3
+ REDIS_DIR = File.expand_path(File.join("..", "test"), __FILE__)
4
+ REDIS_CNF = File.join(REDIS_DIR, "test.conf")
5
+ REDIS_PID = File.join(REDIS_DIR, "db", "redis.pid")
6
+
7
+ task :default => :run
8
+
9
+ desc "Run tests and manage server start/stop"
10
+ task :run => [:start, :test, :stop]
11
+
12
+ desc "Start the Redis server"
13
+ task :start do
14
+ unless File.exists?(REDIS_PID)
15
+ system "redis-server #{REDIS_CNF}"
16
+ end
17
+ end
18
+
19
+ desc "Stop the Redis server"
20
+ task :stop do
21
+ if File.exists?(REDIS_PID)
22
+ system "kill #{File.read(REDIS_PID)}"
23
+ File.delete(REDIS_PID)
24
+ end
25
+ end
26
+
27
+ task :test do
28
+ require File.expand_path("./test/helper", File.dirname(__FILE__))
29
+
30
+ Cutest.run(Dir["test/*.rb"])
31
+ end
data/lib/ohm/expire.rb ADDED
@@ -0,0 +1,145 @@
1
+ module Ohm
2
+ # Main expire module
3
+ # ==== Examples
4
+ #
5
+ # Model < Ohm::Model
6
+ # include Ohm::Expire
7
+ # end
8
+ module Expire
9
+
10
+ # Extend this module with Ohm
11
+ def self.included(base)
12
+ base.send(:extend, ClassMethods)
13
+ end
14
+
15
+ # Extends Ohm::Model with new class methods
16
+ module ClassMethods
17
+
18
+ # Set expire for this model
19
+ #
20
+ # ==== Attributes
21
+ #
22
+ # * +ttl <i>Fixnum</i>+ - The expire amount in seconds
23
+ #
24
+ # ==== Returns
25
+ #
26
+ # * nil
27
+ #
28
+ # ==== Examples
29
+ #
30
+ # Model < Ohm::Model
31
+ # include Ohm::Expire
32
+ #
33
+ # attribute :hash
34
+ # index :hash
35
+ #
36
+ # expire 30
37
+ # end
38
+ def expire(ttl)
39
+ @expire = ttl.to_i
40
+ end
41
+
42
+ # Callback that is called everytime a new Model is created. This will initialize the export parameters.
43
+ #
44
+ # ==== Attributes
45
+ #
46
+ # * +args <i>Array</i>+ - The arguments supplied on the create method
47
+ #
48
+ # ==== Returns
49
+ #
50
+ # * self
51
+ def create(*args)
52
+ object = super(*args)
53
+ if !object.new? && @expire
54
+ Ohm.redis.expire(object.key, @expire)
55
+ Ohm.redis.expire("#{object.key}:_indices", @expire)
56
+ end
57
+ object
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ module Ohm
64
+ # Extends Ohm::Model with new methods
65
+ class Model
66
+
67
+ # Update the ttl of a given Model
68
+ #
69
+ # ==== Attributes
70
+ #
71
+ # * +model <i>Ohm::Model</i>+ - A Ohm model that will be used to update its ttl
72
+ # * +new_ttl <i>Fixnum</i>+ - The new expire amount
73
+ #
74
+ # ==== Returns
75
+ #
76
+ # * nil
77
+ #
78
+ # ==== Examples
79
+ #
80
+ # d = Model.create(:hash => "123")
81
+ # Model.update_ttl(d, 30)
82
+ def self.update_ttl(model, new_ttl)
83
+ unless model.respond_to? :update_ttl
84
+ model.update_ttl new_ttl
85
+ end
86
+ end
87
+
88
+ # Update the ttl
89
+ #
90
+ # ==== Attributes
91
+ #
92
+ # * +new_ttl <i>Fixnum</i>+ - The new expire amount
93
+ #
94
+ # ==== Returns
95
+ #
96
+ # * nil
97
+ #
98
+ # ==== Examples
99
+ #
100
+ # d = Model.create(:hash => "123")
101
+ # d.update_ttl(30)
102
+ def update_ttl new_ttl
103
+ # Make sure we have a valid value
104
+ new_ttl = -1 if !new_ttl.to_i.is_a?(Fixnum) || new_ttl.to_i < 0
105
+ # Update indices
106
+ Ohm.redis.expire(self.key, new_ttl)
107
+ Ohm.redis.expire("#{self.key}:_indices", new_ttl)
108
+ end
109
+
110
+ # Get ttl for a given Model
111
+ #
112
+ # ==== Attributes
113
+ #
114
+ # * +model <i>Ohm::Model</i>+ - A Ohm model that will be used to retrieve its ttl
115
+ #
116
+ # ==== Returns
117
+ #
118
+ # * nil
119
+ #
120
+ # ==== Examples
121
+ #
122
+ # d = Model.create(:hash => "123")
123
+ # Model.get_ttl(d)
124
+ def self.get_ttl model
125
+ unless model.respond_to? :get_ttl
126
+ model.get_ttl
127
+ end
128
+ end
129
+
130
+ # Get ttl for a given Model
131
+ #
132
+ # ==== Returns
133
+ #
134
+ # * nil
135
+ #
136
+ # ==== Examples
137
+ #
138
+ # d = Model.create(:hash => "123")
139
+ # d.get_ttl
140
+ def get_ttl
141
+ Ohm.redis.ttl(self.key)
142
+ end
143
+
144
+ end
145
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "helper"
2
+ require "ohm/expire"
3
+
4
+
5
+ class Model < Ohm::Model
6
+
7
+ include Ohm::Expire
8
+
9
+ attribute :hash
10
+ index :hash
11
+
12
+ expire 5
13
+
14
+ attribute :data
15
+ end
16
+
17
+ test do
18
+ Ohm.flush
19
+
20
+ m = Model.create(:hash => "123")
21
+ assert_equal 5, m.get_ttl
22
+
23
+ m.update_ttl 2
24
+ assert_equal 2, m.get_ttl
25
+
26
+ sleep 1
27
+
28
+ m = Model.find(:hash => "123").first
29
+ assert !m.hash.nil?
30
+
31
+ sleep 2
32
+
33
+ m = Model.find(:hash => "123").first
34
+ assert m.hash.nil?
35
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
4
+
5
+ begin
6
+ require "ruby-debug"
7
+ rescue LoadError
8
+ end
9
+
10
+ require "rubygems"
11
+ require "cutest"
12
+
13
+ def silence_warnings
14
+ original_verbose, $VERBOSE = $VERBOSE, nil
15
+ yield
16
+ ensure
17
+ $VERBOSE = original_verbose
18
+ end
19
+
20
+ $VERBOSE = true
21
+
22
+ require "ohm"
23
+
24
+ prepare do
25
+ Ohm.redis.flushall
26
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ohm-expire
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - José P. Airosa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ohm
16
+ version_requirements: &2056 !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 1.0.0
21
+ none: false
22
+ requirement: *2056
23
+ prerelease: false
24
+ type: :runtime
25
+ - !ruby/object:Gem::Dependency
26
+ name: cutest
27
+ version_requirements: &2074 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: '0.1'
32
+ none: false
33
+ requirement: *2074
34
+ prerelease: false
35
+ type: :development
36
+ description: A simple but useful plugin for Ohm that enables control over Redis TTL through Ohm
37
+ email: me@joseairosa.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - lib/ohm/expire.rb
43
+ - README.md
44
+ - LICENSE
45
+ - Rakefile
46
+ - test/expire-test.rb
47
+ - test/helper.rb
48
+ homepage: http://i.am.joseairosa.com/gems/ohm-expire
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ none: false
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ none: false
66
+ requirements: []
67
+ rubyforge_project: ohm-expire
68
+ rubygems_version: 1.8.15
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Ohm Expire Plugin
72
+ test_files: []
73
+ has_rdoc:
74
+ ...