Skip to content

Commit 676e48f

Browse files
committed
Refactoring store plugins #13
- introduce Automatic::Plugin::StoreDatabase module
1 parent 8ca7133 commit 676e48f

3 files changed

Lines changed: 92 additions & 65 deletions

File tree

plugins/store/bookmark.rb

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,41 @@
66
# Updated:: Feb 22, 2012
77
# Copyright:: 774 Copyright (c) 2012
88
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
9-
require 'active_record'
9+
require 'plugins/store/store_database'
1010

11-
module Automatic::Plugin
11+
module Automatic::Plugin
1212
class Bookmark < ActiveRecord::Base
1313
end
1414

1515
class StoreBookmark
16-
attr_accessor :hb
16+
include Automatic::Plugin::StoreDatabase
1717

1818
def initialize(config, pipeline=[])
1919
@config = config
2020
@pipeline = pipeline
2121
end
2222

23-
def create_table
24-
ActiveRecord::Migration.create_table :bookmarks do |t|
25-
t.column :url, :string
26-
t.column :created_at, :string
27-
end
23+
def column_definition
24+
return {
25+
:url => :string,
26+
:created_at => :string
27+
}
28+
end
29+
30+
def unique_key
31+
return :url
2832
end
2933

34+
def model_class
35+
return Automatic::Plugin::Bookmark
36+
end
37+
3038
def run
31-
ActiveRecord::Base.establish_connection(
32-
:adapter => "sqlite3",
33-
:database => (File.join(File.dirname(__FILE__),
34-
'..', '..', 'db', @config['db']))
35-
)
36-
create_table unless Bookmark.table_exists?()
37-
38-
bookmarks = Bookmark.find(:all)
39-
return_feeds = []
40-
@pipeline.each {|feeds|
41-
unless feeds.nil?
42-
new_feed = false
43-
feeds.items.each {|feed|
44-
unless bookmarks.detect {|b|b.url == feed.link}
45-
new_bookmark = Bookmark.new(:url => feed.link,
46-
:created_at => Time.now.strftime("%Y/%m/%d %X"))
47-
new_bookmark.save
48-
new_feed = true
49-
end
50-
}
51-
return_feeds << feeds if new_feed
52-
end
39+
return for_each_new_feed { |feed|
40+
Bookmark.create(
41+
:url => feed.link,
42+
:created_at => Time.now.strftime("%Y/%m/%d %X"))
5343
}
54-
return_feeds
5544
end
5645
end
5746
end

plugins/store/full_text.rb

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,47 @@
66
# Updated:: Feb 26, 2012
77
# Copyright:: 774 Copyright (c) 2012
88
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
9-
require 'active_record'
9+
require 'plugins/store/store_database'
1010

1111
module Automatic::Plugin
1212
class Blog < ActiveRecord::Base
1313
end
1414

1515
class StoreFullText
16+
include Automatic::Plugin::StoreDatabase
17+
1618
def initialize(config, pipeline=[])
1719
@config = config
1820
@pipeline = pipeline
1921
end
2022

21-
def create_table
22-
ActiveRecord::Migration.create_table :blogs do |t|
23-
t.column :title, :string
24-
t.column :link, :string
25-
t.column :description, :string
26-
t.column :content, :string
27-
t.column :created_at, :string
28-
end
23+
def column_definition
24+
return {
25+
:title => :string,
26+
:link => :string,
27+
:description => :string,
28+
:content => :string,
29+
:created_at => :string,
30+
}
31+
end
32+
33+
def unique_key
34+
return :link
35+
end
36+
37+
def model_class
38+
return Automatic::Plugin::Blog
2939
end
3040

3141
def run
32-
ActiveRecord::Base.establish_connection(
33-
:adapter => "sqlite3",
34-
:database => (File.join(File.dirname(__FILE__),
35-
'..', '..', 'db', @config['db'])))
36-
create_table unless Blog.table_exists?()
37-
blogs = Blog.find(:all)
38-
return_feeds = []
39-
@pipeline.each {|feeds|
40-
unless feeds.nil?
41-
new_feed = false
42-
feeds.items.each {|feed|
43-
unless blogs.detect {|b|b.link == feed.link}
44-
new_blog = Blog.new(
45-
:title => feed.title,
46-
:link => feed.link,
47-
:description => feed.description,
48-
:content => feed.content_encoded,
49-
:created_at => Time.now.strftime("%Y/%m/%d %X"))
50-
new_blog.save
51-
new_feed = true
52-
end
53-
}
54-
return_feeds << feeds if new_feed
55-
end
42+
return for_each_new_feed { |feed|
43+
Blog.create(
44+
:title => feed.title,
45+
:link => feed.link,
46+
:description => feed.description,
47+
:content => feed.content_encoded,
48+
:created_at => Time.now.strftime("%Y/%m/%d %X"))
5649
}
57-
return_feeds
5850
end
5951
end
6052
end

plugins/store/store_database.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'active_record'
2+
3+
module Automatic::Plugin
4+
module StoreDatabase
5+
def for_each_new_feed
6+
prepare_database
7+
existing_records = model_class.find(:all)
8+
return_feeds = []
9+
@pipeline.each { |feeds|
10+
unless feeds.nil?
11+
new_feed = false
12+
feeds.items.each { |feed|
13+
unless existing_records.detect { |b| b.try(unique_key) == feed.link }
14+
yield(feed)
15+
new_feed = true
16+
end
17+
}
18+
return_feeds << feeds if new_feed
19+
end
20+
}
21+
return_feeds
22+
end
23+
24+
private
25+
26+
def create_table
27+
ActiveRecord::Migration.create_table(model_class.table_name) do |t|
28+
column_definition.each_pair do |column_name, column_type|
29+
t.column column_name, column_type
30+
end
31+
end
32+
end
33+
34+
def db_dir
35+
return File.join(File.dirname(__FILE__), '..', '..', 'db')
36+
end
37+
38+
def prepare_database
39+
ActiveRecord::Base.establish_connection(
40+
:adapter => "sqlite3",
41+
:database => File.join(db_dir, @config['db']))
42+
create_table unless model_class.table_exists?
43+
end
44+
end
45+
end
46+

0 commit comments

Comments
 (0)