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



134
135
136
137
138
# File 'lib/tdiary/io/mongodb.rb', line 134

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

.load_cgi_conf(conf) ⇒ Object



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

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

.plugin_close(storage) ⇒ Object



144
145
146
# File 'lib/tdiary/io/mongodb.rb', line 144

def plugin_close(storage)
  # do nothing
end

.plugin_open(conf) ⇒ Object



140
141
142
# File 'lib/tdiary/io/mongodb.rb', line 140

def plugin_open(conf)
  return nil
end

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

Yields:



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

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



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

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



199
200
201
# File 'lib/tdiary/io/mongodb.rb', line 199

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

#calendarObject



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/tdiary/io/mongodb.rb', line 185

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) ⇒ Object

block must be return boolean which dirty diaries.



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

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