tdiary-io-rdb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f1f3278cfb2194a6bacca2be1f34daff5544401
4
+ data.tar.gz: ae93406a10b408c0ea0c85d759b7ee21ce2cd77f
5
+ SHA512:
6
+ metadata.gz: e2dfb2b922649ac5b2e1649df445ee99184376b341e8df0db19af7a37e2c929845dcb1ac1c420fa2f2b82a37b35a33bf430a59834e9444a464f315667ba4a64a
7
+ data.tar.gz: 921b64e1a0caffa1cdc1ab907d07b3cee3d9eea0363d1df3c58036156bd1369fef84c8a85e56fef6b95ccb09875a626ea868ecd581e3f5fbb0f56461eb468b05
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tdiary-io-rdb.gemspec
4
+ gemspec
5
+
6
+ gem 'tdiary', github: 'tdiary/tdiary-core'
7
+ gem 'sqlite3'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 SHIBATA Hiroshi
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.
@@ -0,0 +1,29 @@
1
+ # Tdiary::Io::Rdb
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tdiary-io-rdb'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tdiary-io-rdb
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
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 new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,215 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # rdb.rb: RDB IO for tDiary 3.x
4
+ #
5
+ # NAME rdb
6
+ #
7
+ # DESCRIPTION RDB 向け tDiary IO クラス
8
+ # 日記データは RDB に保存、リンク元の記録には未対応
9
+ #
10
+ # Copyright (C) 2003 ma2tak <ma2tak@ma2tak.dyndns.org>
11
+ # (C) 2004 moonwolf <moonwolf@mooonwolf.com>
12
+ # (C) 2005 Kazuhiko <kazuhiko@fdiary.net>
13
+ # (C) 2012 hsbt <shibata.hiroshi@gmail.com>
14
+ #
15
+ # You can distribute this under GPL.
16
+ require 'tdiary/io/base'
17
+ require 'tempfile'
18
+ require 'sequel'
19
+
20
+ module TDiary
21
+ module IO
22
+ module Comment
23
+ def restore_comment(diaries)
24
+ diaries.each do |date, diary_object|
25
+ db[:comments].filter(:diary_id => date).order_by(:no).select(:name, :mail, :last_modified, :visible, :comment).each do |row|
26
+ comment = Comment.new(row[:name], row[:mail], row[:comment], Time.at(row[:last_modified].to_i))
27
+ comment.show = row[:visible]
28
+ diary_object.add_comment(comment)
29
+ end
30
+ end
31
+ end
32
+
33
+ def store_comment(diaries)
34
+ diaries.each do |diary_id, diary|
35
+ no = 0
36
+ diary.each_comment(diary.count_comments(true)) do |com|
37
+ no += 1
38
+ date = {
39
+ :diary_id => diary_id,
40
+ :no => no
41
+ }
42
+ body = {
43
+ :name => com.name,
44
+ :mail => com.mail,
45
+ :last_modified => com.date.to_i,
46
+ :visible => com.visible?,
47
+ :comment => com.body
48
+ }
49
+ comment = db[:comments].filter(date)
50
+ if comment.count > 0
51
+ comment.update(body)
52
+ else
53
+ db[:comments].insert(date.merge(body))
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ module Referer
61
+ def restore_referer(diaries)
62
+ # not implemented yet
63
+ return
64
+ end
65
+
66
+ def store_referer(diaries)
67
+ # not implemented yet
68
+ return
69
+ end
70
+ end
71
+
72
+ class Rdb < Base
73
+ include Comment
74
+ include Referer
75
+ include Cache
76
+
77
+ class << self
78
+ def load_cgi_conf(conf)
79
+ if cgi_conf = db(conf)[:conf].select(:body).first
80
+ cgi_conf[:body]
81
+ else
82
+ ""
83
+ end
84
+ end
85
+
86
+ def save_cgi_conf(conf, result)
87
+ if db(conf)[:conf].count > 0
88
+ db(conf)[:conf].update(:body => result)
89
+ else
90
+ db(conf)[:conf].insert(:body => result)
91
+ end
92
+ end
93
+
94
+ def db(conf)
95
+ @@_db ||= Sequel.connect(conf.database_url || ENV['DATABASE_URL'])
96
+
97
+ @@_db.create_table :conf do
98
+ String :body, :text => true
99
+ end unless @@_db.table_exists?(:conf)
100
+
101
+ @@_db.create_table :diaries do
102
+ String :diary_id, :size => 8
103
+ String :year, :size => 4
104
+ String :month, :size => 2
105
+ String :day, :size => 2
106
+ String :title, :text => true
107
+ String :body, :text => true
108
+ String :style, :text => true
109
+ Fixnum :last_modified
110
+ TrueClass :visible
111
+ primary_key :diary_id
112
+ end unless @@_db.table_exists?(:diaries)
113
+
114
+ @@_db.create_table :comments do
115
+ String :diary_id, :size => 8
116
+ Fixnum :no
117
+ String :name, :text => true
118
+ String :mail, :text => true
119
+ String :comment, :text => true
120
+ Fixnum :last_modified
121
+ TrueClass :visible
122
+ primary_key [:diary_id, :no]
123
+ end unless @@_db.table_exists?(:comments)
124
+
125
+ @@_db
126
+ end
127
+ end
128
+
129
+ #
130
+ # block must be return boolean which dirty diaries.
131
+ #
132
+ def transaction(date)
133
+ diaries = {}
134
+
135
+ if cache = restore_parser_cache(date)
136
+ diaries.update(cache)
137
+ else
138
+ restore(date.strftime("%Y%m%d"), diaries)
139
+ restore_comment(diaries)
140
+ end
141
+
142
+ dirty = yield(diaries) if iterator?
143
+
144
+ store(diaries) if (dirty & TDiary::TDiaryBase::DIRTY_DIARY) != 0
145
+ store_comment(diaries) if (dirty & TDiary::TDiaryBase::DIRTY_COMMENT) != 0
146
+
147
+ store_parser_cache(date, diaries) if dirty || !cache
148
+ end
149
+
150
+ def calendar
151
+ calendar = Hash.new{|hash, key| hash[key] = []}
152
+ db[:diaries].select(:year, :month).group_by(:year, :month).order_by(:year, :month).each do |row|
153
+ calendar[row[:year]] << row[:month]
154
+ end
155
+ calendar
156
+ end
157
+
158
+ def cache_dir
159
+ @tdiary.conf.cache_path || "#{Dir.tmpdir}/cache"
160
+ end
161
+
162
+ private
163
+
164
+ def restore(date, diaries, month = true)
165
+ query = db[:diaries].select(:diary_id, :title, :last_modified, :visible, :body, :style)
166
+ query = if month && /(\d\d\d\d)(\d\d)(\d\d)/ =~ date
167
+ query.filter(:year => $1, :month => $2)
168
+ else
169
+ query.filter(:diary_id => date)
170
+ end
171
+ query.each do |row|
172
+ style = if row[:style].nil? || row[:style].empty?
173
+ 'wiki'
174
+ else
175
+ row[:style].downcase
176
+ end
177
+ diary = eval("#{style(style)}::new(row[:diary_id], row[:title], row[:body], Time::at(row[:last_modified].to_i))")
178
+ diary.show(row[:visible])
179
+ diaries[row[:diary_id]] = diary
180
+ end
181
+ end
182
+
183
+ def store(diaries)
184
+ diaries.each do |diary_id, diary|
185
+ date = if /(\d\d\d\d)(\d\d)(\d\d)/ =~ diary_id
186
+ {
187
+ :year => $1,
188
+ :month => $2,
189
+ :day => $3,
190
+ :diary_id => diary_id
191
+ }
192
+ end
193
+ body = {
194
+ :title => diary.title,
195
+ :last_modified => diary.last_modified.to_i,
196
+ :style => diary.style,
197
+ :visible => diary.visible?,
198
+ :body => diary.to_src
199
+ }
200
+
201
+ entry = db[:diaries].filter(date)
202
+ if entry.count > 0
203
+ entry.update(body)
204
+ else
205
+ db[:diaries].insert(date.merge(body))
206
+ end
207
+ end
208
+ end
209
+
210
+ def db
211
+ self.class.db(@tdiary.conf)
212
+ end
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,73 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'tdiary/comment_manager'
3
+ require 'tdiary/referer_manager'
4
+ require 'tdiary/cache/file'
5
+ require 'tdiary/io/rdb'
6
+
7
+ module TDiary
8
+ class TDiaryBase
9
+ DIRTY_DIARY = 1
10
+ DIRTY_COMMENT = 2
11
+ end
12
+
13
+ module IO
14
+ class Rdb
15
+ def initialize(tdiary)
16
+ @tdiary = tdiary
17
+ end
18
+
19
+ def style(style)
20
+ DummyStyle
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ class DummyTDiary
27
+ def conf
28
+ DummyConf.new
29
+ end
30
+
31
+ def ignore_parser_cache
32
+ false
33
+ end
34
+ end
35
+
36
+ class DummyConf
37
+ def database_url
38
+ 'sqlite://./tdiary_test.db'
39
+ end
40
+
41
+ def cache_path
42
+ nil
43
+ end
44
+ end
45
+
46
+ class DummyStyle
47
+ attr_accessor :title, :to_src
48
+
49
+ def initialize(id, title, body, last_modified)
50
+ @title = title
51
+ @to_src = body
52
+ end
53
+
54
+ def style
55
+ "dummy"
56
+ end
57
+
58
+ def last_modified
59
+ Time.now
60
+ end
61
+
62
+ def visible?
63
+ true
64
+ end
65
+
66
+ def show(dummy); end
67
+ end
68
+
69
+ RSpec.configure do |c|
70
+ c.after(:suite) do
71
+ FileUtils.rm_rf File.expand_path('../../tdiary_test.db', __FILE__)
72
+ end
73
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe TDiary::IO::Rdb do
4
+ it 'is_a TDiary::IO::Base' do
5
+ expect { TDiary::IO::Rdb.is_a?(TDiary::IO::Base) }.to be_true
6
+ end
7
+
8
+ describe "#save_cgi_conf and #load_cgi_conf" do
9
+ let(:conf) { DummyConf.new }
10
+
11
+ it { expect(TDiary::IO::Rdb.load_cgi_conf(conf)).to be_empty }
12
+
13
+ context "given body" do
14
+ before do
15
+ TDiary::IO::Rdb.save_cgi_conf(conf, 'foo')
16
+ end
17
+
18
+ it { expect(TDiary::IO::Rdb.load_cgi_conf(conf)).to eq 'foo' }
19
+
20
+ context "update" do
21
+ before do
22
+ TDiary::IO::Rdb.save_cgi_conf(conf, 'bar')
23
+ end
24
+ it { expect(TDiary::IO::Rdb.load_cgi_conf(conf)).to eq 'bar' }
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "#transaction" do
30
+ let(:io) { TDiary::IO::Rdb.new(DummyTDiary.new) }
31
+ let(:today) { Time.now.strftime( '%Y%m%d' ) }
32
+ let(:diary) { DummyStyle.new('', "foo", "bar", '') }
33
+
34
+ before do
35
+ io.transaction( Time.now ) do |diaries|
36
+ @diaries = diaries
37
+ @diaries[today] = diary
38
+ TDiary::TDiaryBase::DIRTY_DIARY
39
+ end
40
+ end
41
+
42
+ subject { io.send(:db)[:diaries].filter(today).first }
43
+
44
+ it "insert diary" do
45
+ expect(subject).to_not be_nil
46
+ expect(subject[:title]).to eq "foo"
47
+ expect(subject[:body]).to eq "bar"
48
+ end
49
+
50
+ it "restore diary" do
51
+ io.transaction( Time.now ) do |diaries|
52
+ @diaries = diaries
53
+ expect(@diaries[today].title).to eq "foo"
54
+ expect(@diaries[today].to_src).to eq "bar"
55
+ TDiary::TDiaryBase::DIRTY_DIARY
56
+ end
57
+ end
58
+
59
+ context "update diary" do
60
+ let(:diary2) { DummyStyle.new('' , "bar", "foo", '') }
61
+
62
+ before do
63
+ io.transaction( Time.now ) do |diaries|
64
+ @diaries = diaries
65
+ @diaries[today] = diary2
66
+ TDiary::TDiaryBase::DIRTY_DIARY
67
+ end
68
+ end
69
+
70
+ subject { io.send(:db)[:diaries].filter(today) }
71
+
72
+ it "update contents of diary" do
73
+ expect(subject).to_not be_nil
74
+ expect(subject.count).to eq 1
75
+ expect(subject.first[:title]).to eq "bar"
76
+ expect(subject.first[:body]).to eq "foo"
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "tdiary-io-rdb"
5
+ spec.version = "0.0.1"
6
+ spec.authors = ["SHIBATA Hiroshi"]
7
+ spec.email = ["shibata.hiroshi@gmail.com"]
8
+ spec.summary = %q{rdb adapter for tDiary}
9
+ spec.description = %q{rdb adapter for tDiary}
10
+ spec.homepage = "https://github.com/tdiary/tdiary-io-rdb"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "sequel"
19
+
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tdiary-io-rdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - SHIBATA Hiroshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
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: rake
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: rspec
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
+ description: rdb adapter for tDiary
70
+ email:
71
+ - shibata.hiroshi@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/tdiary/io/rdb.rb
84
+ - spec/spec_helper.rb
85
+ - spec/tdiary/io/rdb_spec.rb
86
+ - tdiary-io-rdb.gemspec
87
+ homepage: https://github.com/tdiary/tdiary-io-rdb
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.0
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: rdb adapter for tDiary
111
+ test_files:
112
+ - spec/spec_helper.rb
113
+ - spec/tdiary/io/rdb_spec.rb