Module: Pinport
- Defined in:
- lib/pinport.rb,
lib/pinport/error.rb,
lib/pinport/parser.rb,
lib/pinport/version.rb,
lib/pinport/generators/generator.rb
Defined Under Namespace
Modules: Generator
Classes: DatabaseConnectionError, DirectoryArgumentError, Error, InvalidFileError, Parser, UnexpectedArgumentError
Constant Summary
collapse
- VERSION =
"0.0.3"
Class Method Summary
collapse
Class Method Details
.column_name ⇒ Object
49
50
51
|
# File 'lib/pinport.rb', line 49
def self.column_name
@settings['schema']['column'] ||= self.settings['schema']['column']
end
|
.database_name ⇒ Object
41
42
43
|
# File 'lib/pinport.rb', line 41
def self.database_name
@settings['database']['database'] ||= self.settings['database']['database']
end
|
.db ⇒ Object
25
26
27
|
# File 'lib/pinport.rb', line 25
def self.db
@db ||= db_connection(self.settings['database'])
end
|
.db_connection(db_config) ⇒ Sequel::Mysql2::Database
Establishes a connection to the database
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/pinport.rb', line 93
def self.db_connection(db_config)
db_config = db_config.symbolize_keys
db_config[:test] = true
begin
Sequel.mysql2(db_config)
rescue => e
puts e.message
if @verbose_errors
puts "Trace:\n"
puts e.backtrace
end
exit 1
end
end
|
.import_file(file, table = nil, column = nil, filter = nil, fix_newlines = true) ⇒ Object
Imports pins into database
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/pinport.rb', line 115
def self.import_file(file, table = nil, column = nil, filter = nil, fix_newlines = true)
file_original = "#{file}"
file = File.absolute_path("#{file}")
if !File.exists?(file)
raise Pinport::InvalidFileError
elsif File.directory?(file)
raise Pinport::DirectoryArgumentError
end
source = File.open(file)
source_dirname = File.dirname(file)
source_basename = File.basename(file, '.txt')
cleaned_filename = "#{source_dirname}/#{source_basename}-cleaned.txt"
sorted_filename = "#{source_dirname}/#{source_basename}-sorted.txt"
cleaned = Pinport::Parser.dos2unix(file) unless fix_newlines == false
cleaned = Pinport::Parser.strip_chars(cleaned, filter, cleaned_filename) unless filter == nil
if Pinport::Parser.sort_file_contents(cleaned, sorted_filename)
puts "Importing..."
table = self.table_name if table == nil
column = self.column_name if column == nil
pins = self.db[table.to_sym]
count = 0
start_time = Time.now
File.open(sorted_filename).each_line do |line|
pins.insert(column.to_sym => line.chomp)
count += 1
puts "Imported #{count} pins." if (count % self.notifications['import']['every'] == 0)
end
puts "Finished importing #{count} pins into #{table}."
File.delete(cleaned_filename) if File.exists?(cleaned_filename)
File.delete(sorted_filename) if File.exists?(sorted_filename)
else
puts "Error encountered while sorting pins in file."
return false
end
end
|
.import_folder(folder, extension = "txt", table = nil, column = nil, filter = nil, fix_newlines = true) ⇒ Object
Imports pins from folder containing pin files
Accepts the same parameters as ‘import_file` along with the following:
170
171
172
173
174
175
176
|
# File 'lib/pinport.rb', line 170
def self.import_folder(folder, extension = "txt", table = nil, column = nil, filter = nil, fix_newlines = true)
folder = File.absolute_path(folder)
Dir.glob("#{folder}/*.#{extension}").each do |file|
file = File.absolute_path("#{file}")
import_file(file, table, column, filter, fix_newlines)
end
end
|
.initialize(file) ⇒ Object
19
20
21
22
23
|
# File 'lib/pinport.rb', line 19
def self.initialize(file)
@db = db_connection(self.settings['database'])
@verbose_errors = self.settings['debug']['errors']['verbose']
self.settings_file = file
end
|
.load_config_file(file) ⇒ Hash
Loads a config file to use with pinport
56
57
58
59
60
61
62
63
|
# File 'lib/pinport.rb', line 56
def self.load_config_file(file)
if File.exists?(file)
puts "Loading config file: #{file}"
return YAML.load_file(file)
else
puts "Could not find config file: #{file}"
end
end
|
.notifications ⇒ Object
86
87
88
|
# File 'lib/pinport.rb', line 86
def self.notifications
@notifications ||= self.settings['notifications']
end
|
.root ⇒ Object
82
83
84
|
# File 'lib/pinport.rb', line 82
def self.root
File.expand_path '../..', __FILE__
end
|
.schema ⇒ Object
37
38
39
|
# File 'lib/pinport.rb', line 37
def self.schema
@settings['schema'] ||= self.settings['schema']
end
|
.settings ⇒ Object
29
30
31
|
# File 'lib/pinport.rb', line 29
def self.settings
@settings ||= YAML.load_file(self.settings_file)
end
|
.settings=(new_settings) ⇒ Object
33
34
35
|
# File 'lib/pinport.rb', line 33
def self.settings=(new_settings)
@settings = new_settings
end
|
.settings_file ⇒ Object
78
79
80
|
# File 'lib/pinport.rb', line 78
def self.settings_file
@settings_file ||= 'config.yml'
end
|
.settings_file=(file) ⇒ Object
Updates settings_file and settings vars if specified config file can be found
68
69
70
71
72
73
74
75
76
|
# File 'lib/pinport.rb', line 68
def self.settings_file=(file)
if File.exists?(file)
@settings_file = file
self.settings = load_config_file(@settings_file)
return true
else
raise "Specified settings file could assigned."
end
end
|
.table_name ⇒ Object
45
46
47
|
# File 'lib/pinport.rb', line 45
def self.table_name
@settings['schema']['table'] ||= self.settings['schema']['table']
end
|