expires 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1446302323c38bdefd615bf3233188cf80e00160
4
+ data.tar.gz: 4f8c9717d3e77a6197b7b5166cfbaec6a90343bd
5
+ SHA512:
6
+ metadata.gz: f7997dfca5ccdca7e0a94245c97710ef7ca33ed080c93a32eda844bb8371ab354bf670809eb5662df2ac2b09325cd19b4e296b246b0cad78b712d65da72e847a
7
+ data.tar.gz: 9762d5d75054c2d275b9e8f9c82d4ca1d15e4ba7a116126497d0160c2f9d513841d49831055453c5d7f140acafae91439dc5618a87208edfeb9134489e04b323
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in expires.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tatumaki
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Expires
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'expires'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install expires
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/expires/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/expires.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'expires/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "expires"
8
+ spec.version = Expires::VERSION
9
+ spec.authors = ["Tatumaki"]
10
+ spec.email = ["Tatumaki.x.euphoric@gmail.com"]
11
+ spec.summary = %q{Expires gives a valiable with limited time.}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "color_print"
25
+ spec.add_development_dependency "sqlite3"
26
+
27
+ end
@@ -0,0 +1,3 @@
1
+ class Expires
2
+ VERSION = "0.0.1"
3
+ end
data/lib/expires.rb ADDED
@@ -0,0 +1,114 @@
1
+ require "expires/version"
2
+ require 'sqlite3'
3
+
4
+ class Expires
5
+ class DummyIO;def puts str;end;end
6
+
7
+ @@dbs = Hash.new #namespace: instance
8
+ @@database = 'expires.sqlite3'
9
+ @@db = nil # Main instance
10
+ @@io = DummyIO.new
11
+
12
+ def self.database=(path)
13
+ @@database = path
14
+ end
15
+
16
+ def self.database
17
+ @@database
18
+ end
19
+
20
+ def self.db(namespace)
21
+ @@dbs[namespace]
22
+ end
23
+
24
+ def initialize namespace: namespace, expire: 5, hotload: true
25
+ @@db = SQLite3::Database.new(@@database) unless @@db
26
+ @namespace = namespace
27
+ @connected = false
28
+ @expire = expire
29
+ @hotload = hotload
30
+ @body = Hash.new
31
+
32
+ connect(namespace)
33
+ sync if @hotload
34
+ end
35
+
36
+ def sync(async: false)
37
+ raise "Database not connected" unless @connected
38
+ kill_expired
39
+ @@db.execute("select key,value from #{@namespace};").each do |array|
40
+ @body[array[0]] = array[1]
41
+ end
42
+ @synced = true
43
+ end
44
+
45
+ def create(namespace)
46
+ begin
47
+ @@db.execute(sql = "create table #{namespace} (key TEXT PRIMARY KEY, value TEXT, created_at INTEGER, updated_at INTEGER, expire INTEGER);")
48
+ @@io.puts sql
49
+ rescue SQLite3::SQLException => e
50
+ raise e unless e.message =~ /table .* already exists/
51
+ end
52
+ return namespace
53
+ end
54
+
55
+ def kill_expired
56
+ now = Time.new.to_i
57
+ @@db.execute(sql = "delete from #{@namespace} where (updated_at+expire) < #{now};")
58
+ @@io.puts sql
59
+ end
60
+
61
+ def connect(namespace)
62
+ unless @@dbs[namespace]
63
+ create(namespace)
64
+ @body = Hash.new
65
+ @@dbs.store(namespace, self)
66
+ @connected = true
67
+ end
68
+ end
69
+
70
+ def disconnect
71
+ if @connected
72
+ @@dbs.delete @namespace
73
+ @body = nil
74
+ @connected = false
75
+ end
76
+ end
77
+
78
+ def set key, value
79
+ @body[key] = value
80
+ kill_expired
81
+ record = @@db.execute("select created_at from #{@namespace} where key = '#{key}' limit 1;").first
82
+
83
+ if record
84
+ created_at = record[0]
85
+ updated_at = Time.new.to_i
86
+ else
87
+ updated_at = created_at = Time.new.to_i
88
+ end
89
+
90
+ @@db.execute(sql="insert or replace into #{@namespace} (key,value,created_at,updated_at,expire) values ('#{key}','#{value}','#{created_at}','#{updated_at}','#{@expire}');")
91
+ @@io.puts sql
92
+ #TODO: syncedで状態管理
93
+ end
94
+
95
+ def [](key)
96
+ if @hotload
97
+ self.kill_expired
98
+ return @body[key] = @@db.execute("select value from #{@namespace} where key = '#{key}' limit 1;").first.first
99
+ else
100
+ return @body[key]
101
+ end
102
+ rescue => e
103
+ return nil
104
+ end
105
+
106
+ def []=(key,value)
107
+ set(key,value)
108
+ end
109
+ end
110
+
111
+ def Expires.new namespace: namespace, expire: 5, hotload: true
112
+ raise "namespace is nil." if namespace.nil? or namespace == ""
113
+ return Expires.db(namespace) || super
114
+ end
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ describe Expires do
4
+ it 'has a version number' do
5
+ expect(Expires::VERSION).not_to be nil
6
+ end
7
+
8
+ context "on each connection state" do
9
+ describe "#database" do
10
+ it 'returns scpecific string' do
11
+ Expires.database = 'spec_test'
12
+ expect( Expires.database ).to eq "spec_test"
13
+ end
14
+ end
15
+ end
16
+
17
+ context "on initialize" do
18
+ describe "when hotload is true" do
19
+ expires = Expires.new(namespace: "on_initialize_when_hotload_is_true")
20
+ expires["a"] = :a
21
+ expires["b"] = :b
22
+ expires.disconnect
23
+
24
+ expires = Expires.new(namespace: "on_initialize_when_hotload_is_true", hotload: true)
25
+ it "have to loaded 'a' as 'a'" do
26
+ expect(expires["a"]).to eq 'a'
27
+ end
28
+
29
+ it "have to loaded 'b' as 'a'" do
30
+ expect(expires["b"]).to eq 'b'
31
+ end
32
+ end
33
+
34
+ describe "when hotload is false" do
35
+ expires = Expires.new(namespace: "on_initialize_when_hotload_is_false")
36
+ expires["a"] = :a
37
+ expires["b"] = :b
38
+ expires.disconnect
39
+
40
+ expires = Expires.new(namespace: "on_initialize_when_hotload_is_false", hotload: false)
41
+ it "have to loaded 'a' as nil" do
42
+ expect(expires["a"]).to eq nil
43
+ end
44
+
45
+ it "have to loaded 'b' as nil" do
46
+ expect(expires["b"]).to eq nil
47
+ end
48
+ end
49
+ end
50
+
51
+ context "when not connected" do
52
+ end
53
+
54
+ context "when connected" do
55
+ end
56
+
57
+
58
+
59
+ =begin
60
+ def self.db(namespace)
61
+ @@dbs[namespace]
62
+ end
63
+
64
+ def initialize namespace: namespace, expire: 5, hotload: true
65
+ @@db = SQLite3::Database.new(@@database) unless @@db
66
+ @namespace = namespace
67
+ @connected = false
68
+ @expire = expire
69
+
70
+ connect(namespace)
71
+ sync
72
+ end
73
+
74
+ def sync(async: false)
75
+ raise "Database not connected" unless @connected
76
+ kill_expired
77
+ @@db.execute("select key,value from #{@namespace};").each do |key,value|
78
+ @body[key] = value
79
+ end
80
+ end
81
+
82
+ def create(namespace)
83
+ begin
84
+ @@db.execute(sql = "create table #{namespace} (key TEXT PRIMARY KEY, value TEXT, created_at INTEGER, updated_at INTEGER, expire INTEGER);")
85
+ @@io.puts sql
86
+ rescue SQLite3::SQLException => e
87
+ raise e unless e.message == 'table test already exists'
88
+ end
89
+ return namespace
90
+ end
91
+
92
+ def kill_expired
93
+ now = Time.new.to_i
94
+ @@db.execute(sql = "delete from #{@namespace} where (updated_at+expire) < #{now};")
95
+ @@io.puts sql
96
+ end
97
+
98
+ def connect(namespace)
99
+ unless @@dbs[namespace]
100
+ create(namespace)
101
+ @body = Hash.new
102
+ @@db.execute("select key,value from #{namespace};").each do |row|
103
+ @body[row[0]] = row[1]
104
+ end
105
+ @@dbs.store(namespace, self)
106
+ @synced = true
107
+ @connected = true
108
+ end
109
+ end
110
+
111
+ def disconnect
112
+ @@db.close if @@db
113
+ @connected = false
114
+ end
115
+
116
+ def set key, value
117
+ @body[key] = value
118
+ kill_expired
119
+ record = @@db.execute("select created_at from #{@namespace} where key = '#{key}' limit 1;").first
120
+
121
+ if record
122
+ created_at = record[0]
123
+ updated_at = Time.new.to_i
124
+ else
125
+ updated_at = created_at = Time.new.to_i
126
+ end
127
+
128
+ @@db.execute(sql="insert or replace into #{@namespace} (key,value,created_at,updated_at,expire) values ('#{key}','#{value}','#{created_at}','#{updated_at}','#{@expire}');")
129
+ @@io.puts sql
130
+ #TODO: syncedで状態管理
131
+ end
132
+
133
+ def [](key)
134
+ self.kill_expired
135
+ return @body[key] = @@db.execute("select value from #{@namespace} where key = '#{key}' limit 1;").first
136
+ rescue => e
137
+ puts e
138
+ return nil
139
+ end
140
+
141
+ def []=(key,value)
142
+ set(key,value)
143
+ end
144
+ end
145
+
146
+ def Expires.new namespace: namespace, expire: 5, hotload: true
147
+ raise "namespace is nil." if namespace.nil? or namespace == ""
148
+ return Expires.db(namespace) || super
149
+ end
150
+
151
+ =end
152
+
153
+
154
+
155
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'expires'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expires
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tatumaki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: color_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - Tatumaki.x.euphoric@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - expires.gemspec
98
+ - lib/expires.rb
99
+ - lib/expires/version.rb
100
+ - spec/expires_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.3.0
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Expires gives a valiable with limited time.
126
+ test_files:
127
+ - spec/expires_spec.rb
128
+ - spec/spec_helper.rb