|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Name:: Automatic::Plugin::Store::Database |
| 3 | +# Author:: kzgs |
| 4 | +# Created:: Feb 27, 2012 |
| 5 | +# Updated:: Jun 14, 2012 |
| 6 | +# Copyright:: kzgs Copyright (c) 2012 |
| 7 | +# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. |
| 8 | + |
| 9 | +require 'active_record' |
| 10 | + |
| 11 | +module Automatic::Plugin |
| 12 | + module Database |
| 13 | + def for_each_new_link |
| 14 | + prepare_database |
| 15 | + existing_records = model_class.find(:all) |
| 16 | + @return_html = [] |
| 17 | + @pipeline.each { |link| |
| 18 | + unless link.nil? |
| 19 | + new_link = false |
| 20 | + unless existing_records.detect { |b| b.try(unique_key) == link } |
| 21 | + yield(link) |
| 22 | + new_link = true |
| 23 | + end |
| 24 | + @return_html << link if new_link |
| 25 | + end |
| 26 | + } |
| 27 | + @return_html |
| 28 | + end |
| 29 | + |
| 30 | + def for_each_new_feed |
| 31 | + prepare_database |
| 32 | + existing_records = model_class.find(:all) |
| 33 | + return_feeds = [] |
| 34 | + @pipeline.each { |feeds| |
| 35 | + unless feeds.nil? |
| 36 | + new_feed = false |
| 37 | + feeds.items.each { |feed| |
| 38 | + unless existing_records.detect { |b| b.try(unique_key) == feed.link } |
| 39 | + yield(feed) |
| 40 | + new_feed = true |
| 41 | + end |
| 42 | + } |
| 43 | + return_feeds << feeds if new_feed |
| 44 | + end |
| 45 | + } |
| 46 | + return_feeds |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + def create_table |
| 52 | + ActiveRecord::Migration.create_table(model_class.table_name) { |t| |
| 53 | + column_definition.each_pair { |column_name, column_type| |
| 54 | + t.column column_name, column_type |
| 55 | + } |
| 56 | + } |
| 57 | + end |
| 58 | + |
| 59 | + def db_dir |
| 60 | + dir = (File.expand_path('~/.automatic/db')) |
| 61 | + if File.directory?(dir) |
| 62 | + return dir |
| 63 | + else |
| 64 | + return File.join(File.dirname(__FILE__), '..', '..', 'db') |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def prepare_database |
| 69 | + ActiveRecord::Base.establish_connection( |
| 70 | + :adapter => "sqlite3", |
| 71 | + :database => File.join(db_dir, @config['db'])) |
| 72 | + create_table unless model_class.table_exists? |
| 73 | + end |
| 74 | + end |
| 75 | +end |
0 commit comments