forked from id774/automaticruby
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.rb
More file actions
69 lines (63 loc) · 1.98 KB
/
database.rb
File metadata and controls
69 lines (63 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
# Name:: Automatic::Plugin::Store::Database
# Author:: kzgs
# 774 <http://id774.net>
# soramugi <http://soramugi.net>
# Created:: Feb 27, 2012
# Updated:: Oct 09, 2014
# Copyright:: Copyright (c) 2012-2014 Automatic Ruby Developers.
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
require 'active_record'
module Automatic::Plugin
module Database
def for_each_new_feed
prepare_database
existing_records = model_class.all
@return_feeds = []
@pipeline.each {|feeds|
unless feeds.nil?
new_feeds = []
feeds.items.each {|feed|
unless feed.link.nil?
if unique_keys.length > 1
detection = existing_records.detect {|b| b.try(unique_keys[0]) == feed.link || b.try(unique_keys[1]) == feed.title }
else
detection = existing_records.detect {|b| b.try(unique_keys[0]) == feed.link }
end
unless detection
yield(feed)
new_feeds << feed
end
end
}
@return_feeds << Automatic::FeedMaker.create_pipeline(new_feeds) if new_feeds.length > 0
end
}
@return_feeds
end
private
def create_table
ActiveRecord::Migration.create_table(model_class.table_name) {|t|
column_definition.each_pair {|column_name, column_type|
t.column column_name, column_type
}
}
end
def db_dir
dir = (File.expand_path('~/.automatic/db'))
if File.directory?(dir)
dir
else
File.join(File.dirname(__FILE__), '..', '..', 'db')
end
end
def prepare_database
db = File.join(db_dir, @config['db'])
Automatic::Log.puts("info", "Using Database: #{db}")
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => db)
create_table unless model_class.table_exists?
end
end
end