Class: TDiary::IO::MongoDB

Inherits:
Base
  • Object
show all
Includes:
Cache
Defined in:
lib/tdiary/io/mongodb.rb

Defined Under Namespace

Classes: Comment, Conf, Diary, Plugin, Referer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.db(conf) ⇒ Object



132
133
134
135
136
# File 'lib/tdiary/io/mongodb.rb', line 132

def db(conf)
	@@_db ||= Mongoid::Config.load_configuration(
		{sessions:{default:{uri:(conf.database_url || 'mongodb://localhost:27017/tdiary')}}}
	)
end

.load_cgi_conf(conf) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/tdiary/io/mongodb.rb', line 113

def load_cgi_conf(conf)
	db(conf)
	if cgi_conf = Conf.all.first
		cgi_conf.body
	else
		""
	end
end

.plugin_close(storage) ⇒ Object



142
143
144
# File 'lib/tdiary/io/mongodb.rb', line 142

def plugin_close(storage)
	# do nothing
end

.plugin_open(conf) ⇒ Object



138
139
140
# File 'lib/tdiary/io/mongodb.rb', line 138

def plugin_open(conf)
	return nil
end

.plugin_transaction(storage, plugin_name) {|db| ... } ⇒ Object

Yields:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tdiary/io/mongodb.rb', line 146

def plugin_transaction(storage, plugin_name)
	db = plugin_name.dup
	def db.get(key)
		Plugin.get(self, key)
	end
	def db.set(key, value)
		Plugin.set(self, key, value)
	end
	def db.delete(key)
		Plugin.delete(self, key)
	end
	def db.keys
		Plugin.keys(self)
	end
	yield db
end

.save_cgi_conf(conf, result) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/tdiary/io/mongodb.rb', line 122

def save_cgi_conf(conf, result)
	db(conf)
	if cgi_conf = Conf.all.first
		cgi_conf.body = result
		cgi_conf.save
	else
		Conf.create(body: result).save
	end
end

Instance Method Details

#cache_dirObject



191
192
193
# File 'lib/tdiary/io/mongodb.rb', line 191

def cache_dir
	@tdiary.conf.cache_path || "#{Dir.tmpdir}/cache"
end

#calendarObject



183
184
185
186
187
188
189
# File 'lib/tdiary/io/mongodb.rb', line 183

def calendar
	calendar = Hash.new{|hash, key| hash[key] = []}
	Diary.all.map{|d|[d.year, d.month]}.sort.uniq.each do |ym|
		calendar[ym[0]] << ym[1]
	end
	calendar
end

#transaction(date) ⇒ Object

block must be return boolean which dirty diaries.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tdiary/io/mongodb.rb', line 167

def transaction(date)
	diaries = {}

	if cache = restore_parser_cache(date)
		diaries.update(cache)
	else
		restore(date.strftime("%Y%m%d"), diaries)
	end

	dirty = yield(diaries) if iterator?

	store(diaries, dirty)

	store_parser_cache(date, diaries) if dirty || !cache
end