Class: NewsCrawler::Storage::YAMLStor::MongoStorage
- Inherits:
-
YAMLStorEngine
- Object
- YAMLStorEngine
- NewsCrawler::Storage::YAMLStor::MongoStorage
- Includes:
- Mongo
- Defined in:
- lib/news_crawler/storage/yaml_stor/mongo_storage.rb
Overview
YAML storage implement using MongoDB
Constant Summary collapse
- NAME =
'mongo'
Instance Method Summary collapse
-
#add(module_name, key, value) ⇒ Object
Add entry to yaml collection, overwrite old data.
- #clear ⇒ Object
-
#count ⇒ Object
Get number of raw data entries.
-
#get(module_name, key) ⇒ Object?
Find document with correspond key.
-
#initialize(*opts) ⇒ MongoStorage
constructor
A new instance of MongoStorage.
Methods inherited from YAMLStorEngine
Constructor Details
#initialize(*opts) ⇒ MongoStorage
Returns a new instance of MongoStorage.
39 40 41 42 43 44 45 |
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 39 def initialize(*opts) config = (SimpleConfig.for :application) client = MongoClient.new(config.mongodb.host, config.mongodb.port) db = client[config.mongodb.db_name] @coll = db[config.prefix + '_' + config.suffix.yaml] # @coll.ensure_index({:key => Mongo::ASCENDING}, {:unique => true}) end |
Instance Method Details
#add(module_name, key, value) ⇒ Object
Add entry to yaml collection, overwrite old data
51 52 53 54 55 56 57 58 |
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 51 def add(module_name, key, value) yaml_str = value.to_yaml yaml_str.encode!('utf-8', :invalid => :replace, :undef => :replace) @coll.update({:key => key, :m_name => module_name}, {:$set => {:value => yaml_str}}, {:upsert => true}) end |
#clear ⇒ Object
79 80 81 |
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 79 def clear @coll.remove end |
#count ⇒ Object
Get number of raw data entries
75 76 77 |
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 75 def count @coll.count end |
#get(module_name, key) ⇒ Object?
Find document with correspond key
64 65 66 67 68 69 70 71 72 |
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 64 def get(module_name, key) result = @coll.find_one({:key => key, :m_name => module_name}) if (!result.nil?) YAML.load(result['value']) else nil end end |