class TDiary::IO::MongoDB

Public Class Methods

db(conf) click to toggle source
# File lib/tdiary/io/mongodb.rb, line 133
def db(conf)
        @@_db ||= Mongoid::Config.load_configuration(
                {clients:{default:{uri:(conf.database_url || 'mongodb://localhost:27017/tdiary')}}}
        )
end
load_cgi_conf(conf) click to toggle source
# File lib/tdiary/io/mongodb.rb, line 114
def load_cgi_conf(conf)
        db(conf)
        if cgi_conf = Conf.all.first
                cgi_conf.body
        else
                ""
        end
end
plugin_close(storage) click to toggle source
# File lib/tdiary/io/mongodb.rb, line 143
def plugin_close(storage)
        # do nothing
end
plugin_open(conf) click to toggle source
# File lib/tdiary/io/mongodb.rb, line 139
def plugin_open(conf)
        return nil
end
plugin_transaction(storage, plugin_name) { |db| ... } click to toggle source
# File lib/tdiary/io/mongodb.rb, line 147
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) click to toggle source
# File lib/tdiary/io/mongodb.rb, line 123
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

Public Instance Methods

cache_dir() click to toggle source
# File lib/tdiary/io/mongodb.rb, line 198
def cache_dir
        @tdiary.conf.cache_path || "#{Dir.tmpdir}/cache"
end
calendar() click to toggle source
# File lib/tdiary/io/mongodb.rb, line 184
def calendar
        mongo_project = {
                "$group" => {
                        "_id" => {"year" => "$year", "month" => "$month"},
                        "count" => {"$sum" => 1}
                }
        }
        calendar = Hash.new{|hash, key| hash[key] = []}
        Diary.collection.aggregate([mongo_project]).map do |cal|
                calendar[cal['_id']['year']] << cal['_id']['month']
        end
        calendar
end
transaction(date) { |diaries| ... } click to toggle source

block must be return boolean which dirty diaries.

# File lib/tdiary/io/mongodb.rb, line 168
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

Private Instance Methods

db() click to toggle source
# File lib/tdiary/io/mongodb.rb, line 277
def db
        self.class.db(@tdiary.conf)
end
restore(date, diaries, month = true) click to toggle source
# File lib/tdiary/io/mongodb.rb, line 204
def restore(date, diaries, month = true)
        query = if month && /(\d{4})(\d\d)(\d\d)/ =~ date
                                  Diary.where(year: $1, month: $2)
                          else
                                  Diary.where(diary_id: date)
                          end
        query.each do |d|
                style = (d.style.nil? || d.style.empty?) ? 'wiki' : d.style.downcase
                diary = eval("#{style(style)}::new(d.diary_id, d.title, d.body, Time::at(d.last_modified.to_i))")
                diary.show(d.visible)
                d.comments.each do |c|
                        comment = TDiary::Comment.new(c.name, c.mail, c.body, Time.at(c.last_modified.to_i))
                        comment.show = c.visible
                        diary.add_comment(comment)
                end
                diaries[d.diary_id] = diary
        end
end
store(diaries, dirty) click to toggle source
# File lib/tdiary/io/mongodb.rb, line 223
def store(diaries, dirty)
        if dirty
                diaries.each do |diary_id, diary|
                        year, month, day = diary_id.scan(/(\d{4})(\d\d)(\d\d)/).flatten
        
                        entry = Diary.where(diary_id: diary_id).first

                        if (dirty & TDiary::TDiaryBase::DIRTY_DIARY) != 0
                                if entry
                                        entry.title = diary.title
                                        entry.last_modified = diary.last_modified.to_i
                                        entry.style = diary.style
                                        entry.visible = diary.visible?
                                        entry.body = diary.to_src
                                else
                                        entry = Diary.create(
                                                diary_id: diary_id,
                                                year: year, month: month, day: day,
                                                title: diary.title,
                                                last_modified: diary.last_modified,
                                                style: diary.style,
                                                visible: diary.visible?,
                                                body: diary.to_src
                                        )
                                end
                                entry.save
                        end
                        if entry && ((dirty & TDiary::TDiaryBase::DIRTY_COMMENT) != 0)
                                exist_comments = entry.comments.size
                                no = 0
                                diary.each_comment(diary.count_comments(true)) do |com|
                                        if no < exist_comments
                                                entry.comments[no].name = com.name
                                                entry.comments[no].mail = com.mail
                                                entry.comments[no].body = com.body
                                                entry.comments[no].last_modified = com.date.to_i
                                                entry.comments[no].visible = com.visible?
                                                no += 1
                                        else
                                                entry.comments.build(
                                                        name: com.name,
                                                        mail: com.mail,
                                                        body: com.body,
                                                        last_modified: com.date.to_i,
                                                        visible: com.visible?
                                                )
                                        end
                                        entry.save
                                end
                        end
                end
        end
end