rstore 0.3.2 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.md +5 -0
- data/Rakefile +4 -3
- data/lib/rstore/base_db.rb +21 -17
- data/lib/rstore/configuration.rb +3 -0
- data/lib/rstore/converter.rb +2 -1
- data/lib/rstore/data.rb +7 -7
- data/lib/rstore/file_crawler.rb +5 -7
- data/lib/rstore/logger.rb +10 -10
- data/lib/rstore/version.rb +1 -1
- metadata +23 -15
data/ChangeLog.md
CHANGED
data/Rakefile
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# require 'spec/rake/spectask' # depreciated
|
2
2
|
require 'rspec/core/rake_task'
|
3
|
-
require 'rake/gempackagetask'
|
3
|
+
# require 'rake/gempackagetask' # depreciated
|
4
|
+
require 'rubygems/package_task'
|
4
5
|
require 'rdoc/task'
|
5
6
|
|
6
7
|
# Build gem: rake gem
|
@@ -27,7 +28,7 @@ end
|
|
27
28
|
|
28
29
|
gem_spec = eval(File.read('rstore.gemspec'))
|
29
30
|
|
30
|
-
|
31
|
+
Gem::PackageTask.new( gem_spec ) do |t|
|
31
32
|
t.need_zip = true
|
32
33
|
end
|
33
34
|
|
@@ -36,5 +37,5 @@ end
|
|
36
37
|
#end
|
37
38
|
|
38
39
|
task :push => :gem do |t|
|
39
|
-
sh "gem push pkg/#{gem_spec.name}-#{gem_spec.version}.gem"
|
40
|
+
sh "gem push -v pkg/#{gem_spec.name}-#{gem_spec.version}.gem"
|
40
41
|
end
|
data/lib/rstore/base_db.rb
CHANGED
@@ -7,7 +7,7 @@ module RStore
|
|
7
7
|
|
8
8
|
class << self
|
9
9
|
# A Hash holding subclasses of {RStore::BaseDB}
|
10
|
-
# @return [Hash{Symbol=>BaseDB}] All subclasses of {RStore::BaseDB} defined in the current namespace.
|
10
|
+
# @return [Hash{Symbol=>BaseDB}] All subclasses of {RStore::BaseDB} defined in the current namespace.
|
11
11
|
# Subclasses are added automatically via _self.inherited_.
|
12
12
|
# @example
|
13
13
|
# class CompanyDB < RStore::BaseDB
|
@@ -30,17 +30,17 @@ module RStore
|
|
30
30
|
BaseDB.db_classes[subclass.name] = subclass
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
|
34
34
|
# Define the database connection.
|
35
35
|
# @note To be called when defining a _subclass_ of {RStore::BaseDB}
|
36
36
|
# Accepts the same _one_ _arity_ parameters as [Sequel.connect](http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html)
|
37
37
|
# @overload info(options)
|
38
38
|
# @param [String, Hash] connection_info Either a connection string such as _postgres://user:password@localhost/blog_, or a `Hash` with the following options:
|
39
|
-
# @option options [String] :adapter The SQL database used, such as _mysql_ or _postgres_
|
39
|
+
# @option options [String] :adapter The SQL database used, such as _mysql_ or _postgres_
|
40
40
|
# @option options [String] :host Example: 'localhost'
|
41
|
-
# @option options [String] :user
|
42
|
-
# @option options [String] :password
|
43
|
-
# @option options [String] :database The database name. You don't need to provide this option, as its value will be inferred from the class name.
|
41
|
+
# @option options [String] :user
|
42
|
+
# @option options [String] :password
|
43
|
+
# @option options [String] :database The database name. You don't need to provide this option, as its value will be inferred from the class name.
|
44
44
|
# @return [void]
|
45
45
|
# @example
|
46
46
|
# # Using a connection string
|
@@ -50,29 +50,33 @@ module RStore
|
|
50
50
|
#
|
51
51
|
# # Using an options hash
|
52
52
|
# class CompanyDB < RStore::BaseDB
|
53
|
-
# info(adapter: 'mysql',
|
54
|
-
# host: 'localhost',
|
55
|
-
# user: 'root',
|
53
|
+
# info(adapter: 'mysql',
|
54
|
+
# host: 'localhost',
|
55
|
+
# user: 'root',
|
56
56
|
# password: 'xxx')
|
57
57
|
# end
|
58
58
|
def self.info hash_or_string
|
59
|
-
|
59
|
+
# self = CompanyDB
|
60
|
+
|
61
|
+
class << self
|
62
|
+
# self = #<Class:CompanyDB>
|
60
63
|
attr_reader :connection_info
|
61
64
|
end
|
62
|
-
|
65
|
+
|
66
|
+
# Instance variables always belong to self.
|
63
67
|
@connection_info = hash_or_string.is_a?(Hash) ? hash_or_string.merge(:database => self.name.to_s): hash_or_string
|
64
68
|
end
|
65
69
|
|
66
70
|
|
67
|
-
# Uses the connection info from {.info} to connect to the database.
|
71
|
+
# Uses the connection info from {.info} to connect to the database.
|
68
72
|
# @note To be called when defining a _subclass_ of {RStore::BaseDB}
|
69
73
|
# @yieldparam [Sequel::Database] db The opened Sequel {http://sequel.rubyforge.org/rdoc/classes/Sequel/Database.html Database} object, which is closed when the block exits.
|
70
74
|
# @return [void]
|
71
75
|
# @example
|
72
76
|
# class CompanyDB < RStore::BaseDB
|
73
|
-
# info(adapter: 'mysql',
|
74
|
-
# host: 'localhost',
|
75
|
-
# user: 'root',
|
77
|
+
# info(adapter: 'mysql',
|
78
|
+
# host: 'localhost',
|
79
|
+
# user: 'root',
|
76
80
|
# password: 'xxx')
|
77
81
|
# end
|
78
82
|
#
|
@@ -110,10 +114,10 @@ module RStore
|
|
110
114
|
#
|
111
115
|
# CompanyDB.name
|
112
116
|
# #=> :company
|
113
|
-
def self.name
|
117
|
+
def self.name
|
114
118
|
super.gsub!(/DB/,'').downcase.to_sym
|
115
119
|
end
|
116
120
|
|
117
121
|
end
|
118
122
|
end
|
119
|
-
|
123
|
+
|
data/lib/rstore/configuration.rb
CHANGED
@@ -36,6 +36,9 @@ module RStore
|
|
36
36
|
|
37
37
|
@path = path
|
38
38
|
self.options = new_options
|
39
|
+
# options= deletes all valid options already processed from new_options,
|
40
|
+
# leaving only options that are not valid.
|
41
|
+
# Therefore, new_options.size > 0 indicates that options not recognized where passed.
|
39
42
|
raise ArgumentError, arg_error_message(@path, new_options) if new_options.size > 0
|
40
43
|
|
41
44
|
@file_options = extract_with(Configuration.default_file_options)
|
data/lib/rstore/converter.rb
CHANGED
@@ -34,9 +34,10 @@ module RStore
|
|
34
34
|
# where the error occured.
|
35
35
|
Converters = Hash.new {|h,k| h[k] = lambda { |field| field }}.
|
36
36
|
merge!({string: lambda { |field| field },
|
37
|
-
date: lambda { |field| Date.parse(field).
|
37
|
+
date: lambda { |field| Date.parse(field).strftime("%Y-%m-%d") },
|
38
38
|
datetime: lambda { |field| DateTime.parse(field).to_s },
|
39
39
|
# Convert to DateTime, because DateTime also checks if the argument is valid
|
40
|
+
# Sequel handles Time as DateTime
|
40
41
|
time: lambda { |field| DateTime.parse(field).to_s },
|
41
42
|
integer: lambda { |field| Integer(field) },
|
42
43
|
float: lambda { |field| Float(field) },
|
data/lib/rstore/data.rb
CHANGED
@@ -20,14 +20,14 @@ module RStore
|
|
20
20
|
|
21
21
|
def initialize path, content, state, options
|
22
22
|
error_message = "#{path}: The following options are not valid as an argument to #{self.class}:\n#{options}"
|
23
|
-
raise ArgumentError, error_message unless options.is_a?(Hash)
|
23
|
+
raise ArgumentError, error_message unless options.is_a?(Hash)
|
24
24
|
@path = path
|
25
25
|
@content = content
|
26
26
|
self.state = state
|
27
27
|
@options = options
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
|
31
31
|
#def extract_type path
|
32
32
|
# path, filename = File.split(path)
|
33
33
|
# filename.match(/\.(?<type>.*)$/)[:type].to_sym
|
@@ -41,9 +41,9 @@ module RStore
|
|
41
41
|
|
42
42
|
begin
|
43
43
|
csv = CSVWrapper.parse(@content, parse_options)
|
44
|
-
csv = csv.drop(1) if file_options[:has_headers] == true # drop the first row if it is a header
|
44
|
+
csv = csv.drop(1) if file_options[:has_headers] == true # drop the first row if it is a header
|
45
45
|
rescue => e
|
46
|
-
Logger.new(@options).print(@
|
46
|
+
Logger.new(@options).print(@path, :parse, e)
|
47
47
|
end
|
48
48
|
|
49
49
|
@state = :parsed
|
@@ -64,7 +64,7 @@ module RStore
|
|
64
64
|
|
65
65
|
|
66
66
|
def state= state
|
67
|
-
error_message = "#{state.inspect} is not a valid state. The following states are valid: #{print_valid_states}"
|
67
|
+
error_message = "#{state.inspect} is not a valid state. The following states are valid: #{print_valid_states}"
|
68
68
|
raise ArgumentError, error_message unless KnownStates.include?(state)
|
69
69
|
@state = state
|
70
70
|
end
|
@@ -76,5 +76,5 @@ module RStore
|
|
76
76
|
end
|
77
77
|
|
78
78
|
end
|
79
|
-
end
|
80
|
-
|
79
|
+
end
|
80
|
+
|
data/lib/rstore/file_crawler.rb
CHANGED
@@ -11,7 +11,7 @@ module RStore
|
|
11
11
|
#attr_reader :file_options_hash
|
12
12
|
attr_reader :data_hash
|
13
13
|
|
14
|
-
attr_reader :file_options, :parse_options
|
14
|
+
attr_reader :file_options, :parse_options
|
15
15
|
attr_reader :path
|
16
16
|
attr_reader :file_paths, :file_type
|
17
17
|
attr_reader :config
|
@@ -31,7 +31,7 @@ module RStore
|
|
31
31
|
|
32
32
|
|
33
33
|
def file_paths= path
|
34
|
-
return @file_paths unless @file_paths.nil?
|
34
|
+
return @file_paths unless @file_paths.nil? # @file_path can only be set once on initialization
|
35
35
|
|
36
36
|
@file_paths = []
|
37
37
|
files = []
|
@@ -45,7 +45,7 @@ module RStore
|
|
45
45
|
end
|
46
46
|
else # Either a file or a non-existing directory path
|
47
47
|
file = File.expand_path(path)
|
48
|
-
raise ArgumentError, "'#{path}' is not a valid path" unless File.exists?(file)
|
48
|
+
raise ArgumentError, "'#{path}' is not a valid path" unless File.exists?(file) # File.exist?(“/path/to/file_or_dir”)
|
49
49
|
|
50
50
|
error_message = <<-MESSAGE.gsub(/^\s+/,'')
|
51
51
|
Not a #{@file_type} file.
|
@@ -79,8 +79,6 @@ module RStore
|
|
79
79
|
|
80
80
|
|
81
81
|
def file_options_hash= file_paths
|
82
|
-
@file_options_hash unless @file_options_hash.nil?
|
83
|
-
|
84
82
|
hash = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = nil}}
|
85
83
|
file_paths.each do |path|
|
86
84
|
hash[path][:file_options] = @file_options
|
@@ -122,10 +120,10 @@ module RStore
|
|
122
120
|
when /^www/ # open-uri does not recognize URLs starting with 'www'
|
123
121
|
address = 'http://' + address
|
124
122
|
retry
|
125
|
-
when /^http:/ # open-uri does not redirect from http to https on a valid https URL
|
123
|
+
when /^http:/ # open-uri does not redirect from http to https on a valid https URL
|
126
124
|
address = address.gsub(/http/,'https')
|
127
125
|
retry
|
128
|
-
else
|
126
|
+
else
|
129
127
|
raise ArgumentError, "Could not connect to #{url}. Please check if this URL is correct."
|
130
128
|
end
|
131
129
|
end
|
data/lib/rstore/logger.rb
CHANGED
@@ -7,10 +7,10 @@ module RStore
|
|
7
7
|
|
8
8
|
attr_accessor :data
|
9
9
|
attr_accessor :message
|
10
|
-
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
|
12
|
+
KnownStates =
|
13
|
+
{:fetch => "loading files",
|
14
14
|
:parse => "parsing file content",
|
15
15
|
:convert => "converting field values into their corresponding datatypes",
|
16
16
|
:store => "storing file content into database"}
|
@@ -24,19 +24,19 @@ module RStore
|
|
24
24
|
|
25
25
|
|
26
26
|
def log state, error, loc={}
|
27
|
-
raise ArgumentError "#{state} is an invalid state
|
27
|
+
raise ArgumentError "#{state} is an invalid state for #{self.class}" unless valid_state? state
|
28
28
|
|
29
29
|
loc = correct_location(loc)
|
30
30
|
|
31
31
|
type_of_error = error.class
|
32
32
|
error_message = error.to_s
|
33
|
-
location = "Location : #{location_to_s(loc)}"
|
33
|
+
location = "Location : #{location_to_s(loc)}"
|
34
34
|
location = loc.empty? ? '' : location
|
35
35
|
|
36
36
|
report = <<-TEXT.gsub(/^\s+/, '')
|
37
37
|
An error occured while #{KnownStates[state]}:
|
38
|
-
File : #{@data.path}
|
39
|
-
Type of error: #{type_of_error}
|
38
|
+
File : #{@data.path}
|
39
|
+
Type of error: #{type_of_error}
|
40
40
|
Error message: #{error_message}
|
41
41
|
#{location}
|
42
42
|
=============
|
@@ -61,9 +61,9 @@ module RStore
|
|
61
61
|
end
|
62
62
|
|
63
63
|
|
64
|
-
|
64
|
+
|
65
65
|
def correct_location location
|
66
|
-
|
66
|
+
|
67
67
|
if location[:row] # row_index
|
68
68
|
row = correct_row(location[:row])
|
69
69
|
if location[:col] # col_index
|
@@ -88,7 +88,7 @@ module RStore
|
|
88
88
|
|
89
89
|
|
90
90
|
def valid_state? state
|
91
|
-
KnownStates.keys.any? { |val| val == state }
|
91
|
+
KnownStates.keys.any? { |val| val == state }
|
92
92
|
end
|
93
93
|
|
94
94
|
|
data/lib/rstore/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,30 +9,38 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
|
16
|
-
|
15
|
+
type: :runtime
|
16
|
+
name:
|
17
|
+
- open-uri
|
18
|
+
- nokogiri
|
19
|
+
- bigdecimal
|
20
|
+
- sequel
|
21
|
+
- csv
|
22
|
+
prerelease: false
|
23
|
+
requirement: &16180 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
25
|
requirements:
|
19
26
|
- - ! '>='
|
20
27
|
- !ruby/object:Gem::Version
|
21
28
|
version: '0'
|
22
|
-
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *23883620
|
29
|
+
version_requirements: *16180
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
|
-
|
27
|
-
|
31
|
+
type: :development
|
32
|
+
name:
|
33
|
+
- rspec
|
34
|
+
- mysql
|
35
|
+
- sqlite3-ruby
|
36
|
+
prerelease: false
|
37
|
+
requirement: &16244 !ruby/object:Gem::Requirement
|
28
38
|
none: false
|
29
39
|
requirements:
|
30
40
|
- - ! '>='
|
31
41
|
- !ruby/object:Gem::Version
|
32
42
|
version: '0'
|
33
|
-
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *23883160
|
43
|
+
version_requirements: *16244
|
36
44
|
description: ! " RStore makes batch processing of csv files a breeze.\n Automatically
|
37
45
|
fetches data files, directories, URLs\n :: Customizable using additional options\n
|
38
46
|
\ :: Validation of field values\n :: Descriptive error messages\n :: Safe and
|
@@ -42,6 +50,7 @@ executables: []
|
|
42
50
|
extensions: []
|
43
51
|
extra_rdoc_files: []
|
44
52
|
files:
|
53
|
+
- lib/rstore.rb
|
45
54
|
- lib/rstore/version.rb
|
46
55
|
- lib/rstore/csv.rb
|
47
56
|
- lib/rstore/configuration.rb
|
@@ -52,13 +61,12 @@ files:
|
|
52
61
|
- lib/rstore/logger.rb
|
53
62
|
- lib/rstore/storage.rb
|
54
63
|
- lib/rstore/base_table.rb
|
64
|
+
- lib/rstore/exceptions.rb
|
55
65
|
- lib/rstore/core_ext/hash.rb
|
56
66
|
- lib/rstore/core_ext/csv_wrapper.rb
|
57
67
|
- lib/rstore/core_ext/string.rb
|
58
68
|
- lib/rstore/core_ext/object.rb
|
59
|
-
- lib/rstore/exceptions.rb
|
60
69
|
- lib/rstore/modules/helper_methods.rb
|
61
|
-
- lib/rstore.rb
|
62
70
|
- ChangeLog.md
|
63
71
|
- README.md
|
64
72
|
- Rakefile
|
@@ -83,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
91
|
version: '0'
|
84
92
|
requirements: []
|
85
93
|
rubyforge_project: rstore
|
86
|
-
rubygems_version: 1.8.
|
94
|
+
rubygems_version: 1.8.16
|
87
95
|
signing_key:
|
88
96
|
specification_version: 3
|
89
97
|
summary: RStore - A library for easy batch storage of csv data into a database
|