smilodon 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/smilodon/fakes.rb +10 -0
- data/lib/smilodon.rb +30 -20
- data/smilodon.gemspec +3 -4
- data/spec/lib/smilodon_spec.rb +16 -1
- metadata +62 -29
- data/populator.gemspec +0 -70
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/lib/smilodon/fakes.rb
CHANGED
@@ -27,3 +27,13 @@ module FakePopulatorWithOverriddenDirectory
|
|
27
27
|
# Populate the test file.
|
28
28
|
populates 'TestFile', :directory => 'db/populate/files'
|
29
29
|
end
|
30
|
+
|
31
|
+
# A fake populator module with overridden directory for testing.
|
32
|
+
module FakePopulatorWithMultipleFiles
|
33
|
+
|
34
|
+
# Extend it with the Populator module.
|
35
|
+
extend Smilodon::Populator
|
36
|
+
|
37
|
+
# Populate the test file.
|
38
|
+
populates 'TestFile1', 'TestFile2', 'TestFile3', :directory => 'db/populate/files'
|
39
|
+
end
|
data/lib/smilodon.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'smilodon/errors'
|
2
2
|
require 'smilodon/logger'
|
3
|
+
require 'csv'
|
3
4
|
|
4
5
|
# Smilodon includes helper methods to ease parsing data files.
|
5
6
|
# Assigning a header and iterating over rows is handled by the
|
@@ -30,22 +31,24 @@ module Smilodon
|
|
30
31
|
# Default data file type is CSV.
|
31
32
|
TYPE = 'csv'
|
32
33
|
|
33
|
-
# Attribute accessors for the directory, file name, header,
|
34
|
-
attr_accessor :logger, :directory, :
|
34
|
+
# Attribute accessors for the directory, file name, header, rows and before hook.
|
35
|
+
attr_accessor :logger, :directory, :files, :type, :header, :rows, :before
|
35
36
|
|
36
37
|
# Configuration helper.
|
37
38
|
#
|
38
|
-
# @param [
|
39
|
+
# @param [Array] args an array of strings with an optional options hash
|
39
40
|
# @option options [String] :directory ('db/populate/data_files') The location of the data file.
|
40
41
|
# @option options [String] :type ('csv') The data file type.
|
41
42
|
# @option options [Boolean] :header Set true if the file has a header.
|
42
43
|
# @option options [String] :before The method to call before the run.
|
43
|
-
def populates(
|
44
|
+
def populates(*args)
|
44
45
|
# Setup the logger to log populator warnings and messages.
|
45
46
|
self.logger = PopulateLogger.setup
|
46
47
|
|
48
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
49
|
+
|
50
|
+
self.files = args
|
47
51
|
self.directory = options[:directory] || DIRECTORY
|
48
|
-
self.file = file
|
49
52
|
self.type = options[:type] || TYPE
|
50
53
|
self.header = options[:header]
|
51
54
|
self.before = options[:before]
|
@@ -56,19 +59,20 @@ module Smilodon
|
|
56
59
|
# @return [Boolean] Returns true if all rows are processed successfully.
|
57
60
|
def run
|
58
61
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
62
|
+
files.each do |f|
|
63
|
+
# Call the before hook if defined.
|
64
|
+
#
|
65
|
+
# @usage
|
66
|
+
# populates 'TestFile', :before => :inactivate
|
67
|
+
send(before) if before
|
64
68
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
+
rows = parser.parse(read(f))
|
70
|
+
rows.shift if header
|
71
|
+
rows.count.times { process(rows.shift) }
|
72
|
+
end
|
69
73
|
|
70
74
|
# Return true when all rows are processed.
|
71
|
-
|
75
|
+
true
|
72
76
|
end
|
73
77
|
|
74
78
|
# Stub method to be defined in the extended module.
|
@@ -82,6 +86,7 @@ module Smilodon
|
|
82
86
|
|
83
87
|
# The parser to use based on the type of data file.
|
84
88
|
#
|
89
|
+
# @param [String] Name of file to be read
|
85
90
|
# @return [Parser, #parse] Returns the parser class to use.
|
86
91
|
def parser
|
87
92
|
if type == 'csv'
|
@@ -93,19 +98,24 @@ module Smilodon
|
|
93
98
|
# Absolute path for the data file.
|
94
99
|
#
|
95
100
|
# @return [String] The absolute path.
|
96
|
-
def path
|
97
|
-
|
101
|
+
def path(file)
|
102
|
+
if defined?(Rails)
|
103
|
+
"#{Rails.root}/#{directory}/#{file}.#{type}"
|
104
|
+
else
|
105
|
+
"#{directory}/#{file}.#{type}"
|
106
|
+
end
|
98
107
|
end
|
99
108
|
|
100
109
|
# Reads the data file.
|
101
110
|
#
|
111
|
+
# @param [String] Name of file to be read
|
102
112
|
# @return [File] The data file contents.
|
103
113
|
# @raise [DataFileNotConfigured] Raises an exception when the file is not configured.
|
104
114
|
# @raise [MissingDataFile] Raises an exception when the configured file is missing.
|
105
|
-
def read
|
115
|
+
def read(file)
|
106
116
|
raise DataFileNotConfigured unless file
|
107
|
-
raise MissingDataFile unless File.exist?(path)
|
108
|
-
File.read path
|
117
|
+
raise MissingDataFile unless File.exist?(path(file))
|
118
|
+
File.read path(file)
|
109
119
|
end
|
110
120
|
end
|
111
121
|
end
|
data/smilodon.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{smilodon}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Umang Chouhan"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2012-02-08}
|
13
13
|
s.description = %q{Smilodon is a utility to parse data files.}
|
14
14
|
s.email = %q{uchouhan@optimiscorp.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,7 +32,6 @@ Gem::Specification.new do |s|
|
|
32
32
|
"lib/smilodon/tasks.rb",
|
33
33
|
"lib/tasks/populate.rake",
|
34
34
|
"lib/tasks/populate.yml",
|
35
|
-
"populator.gemspec",
|
36
35
|
"smilodon.gemspec",
|
37
36
|
"spec/lib/smilodon_spec.rb",
|
38
37
|
"spec/lib/tasks/populate_spec.rb",
|
@@ -41,7 +40,7 @@ Gem::Specification.new do |s|
|
|
41
40
|
s.homepage = %q{http://github.com/optimis/smilodon}
|
42
41
|
s.licenses = ["MIT"]
|
43
42
|
s.require_paths = ["lib"]
|
44
|
-
s.rubygems_version = %q{1.
|
43
|
+
s.rubygems_version = %q{1.6.2}
|
45
44
|
s.summary = %q{Smilodon is a utility to parse data files.}
|
46
45
|
|
47
46
|
if s.respond_to? :specification_version then
|
data/spec/lib/smilodon_spec.rb
CHANGED
@@ -23,7 +23,7 @@ end
|
|
23
23
|
|
24
24
|
describe FakePopulator, '.file' do
|
25
25
|
it 'should return the configured file name' do
|
26
|
-
FakePopulator.
|
26
|
+
FakePopulator.files.should == ['TestFile']
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -48,3 +48,18 @@ describe FakePopulator, '.process(row)' do
|
|
48
48
|
}.should raise_exception(MethodNotOverridden)
|
49
49
|
end
|
50
50
|
end
|
51
|
+
|
52
|
+
describe FakePopulator, '.files' do
|
53
|
+
it 'should return the configured file names' do
|
54
|
+
FakePopulatorWithMultipleFiles.files.should == ['TestFile1', 'TestFile2', 'TestFile3']
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should handle an options hash' do
|
58
|
+
FakePopulatorWithMultipleFiles.directory.should == 'db/populate/files'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'calls read for each file passed to populate' do
|
62
|
+
FakePopulatorWithMultipleFiles.files.each { |f| FakePopulatorWithMultipleFiles.should_receive(:read).with(f).and_return('') }
|
63
|
+
FakePopulatorWithMultipleFiles.run
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smilodon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 3
|
10
|
+
version: 0.2.3
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Umang Chouhan
|
@@ -10,75 +15,101 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date:
|
18
|
+
date: 2012-02-08 00:00:00 -08:00
|
14
19
|
default_executable:
|
15
20
|
dependencies:
|
16
21
|
- !ruby/object:Gem::Dependency
|
17
22
|
name: logging
|
18
|
-
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
19
26
|
none: false
|
20
27
|
requirements:
|
21
28
|
- - ">="
|
22
29
|
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
23
33
|
version: "0"
|
24
|
-
|
25
|
-
prerelease: false
|
26
|
-
version_requirements: *id001
|
34
|
+
requirement: *id001
|
27
35
|
- !ruby/object:Gem::Dependency
|
28
36
|
name: jeweler
|
29
|
-
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
30
40
|
none: false
|
31
41
|
requirements:
|
32
42
|
- - ~>
|
33
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 15
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 6
|
48
|
+
- 0
|
34
49
|
version: 1.6.0
|
35
|
-
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: *id002
|
50
|
+
requirement: *id002
|
38
51
|
- !ruby/object:Gem::Dependency
|
39
52
|
name: bundler
|
40
|
-
|
53
|
+
type: :development
|
54
|
+
prerelease: false
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
41
56
|
none: false
|
42
57
|
requirements:
|
43
58
|
- - ~>
|
44
59
|
- !ruby/object:Gem::Version
|
60
|
+
hash: 23
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 0
|
64
|
+
- 0
|
45
65
|
version: 1.0.0
|
46
|
-
|
47
|
-
prerelease: false
|
48
|
-
version_requirements: *id003
|
66
|
+
requirement: *id003
|
49
67
|
- !ruby/object:Gem::Dependency
|
50
68
|
name: rspec
|
51
|
-
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
52
72
|
none: false
|
53
73
|
requirements:
|
54
74
|
- - ~>
|
55
75
|
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 2
|
79
|
+
- 3
|
80
|
+
- 0
|
56
81
|
version: 2.3.0
|
57
|
-
|
58
|
-
prerelease: false
|
59
|
-
version_requirements: *id004
|
82
|
+
requirement: *id004
|
60
83
|
- !ruby/object:Gem::Dependency
|
61
84
|
name: rcov
|
62
|
-
|
85
|
+
type: :development
|
86
|
+
prerelease: false
|
87
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
63
88
|
none: false
|
64
89
|
requirements:
|
65
90
|
- - ">="
|
66
91
|
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
67
95
|
version: "0"
|
68
|
-
|
69
|
-
prerelease: false
|
70
|
-
version_requirements: *id005
|
96
|
+
requirement: *id005
|
71
97
|
- !ruby/object:Gem::Dependency
|
72
98
|
name: yard
|
73
|
-
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
74
102
|
none: false
|
75
103
|
requirements:
|
76
104
|
- - ~>
|
77
105
|
- !ruby/object:Gem::Version
|
106
|
+
hash: 7
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
- 6
|
110
|
+
- 0
|
78
111
|
version: 0.6.0
|
79
|
-
|
80
|
-
prerelease: false
|
81
|
-
version_requirements: *id006
|
112
|
+
requirement: *id006
|
82
113
|
description: Smilodon is a utility to parse data files.
|
83
114
|
email: uchouhan@optimiscorp.com
|
84
115
|
executables: []
|
@@ -104,7 +135,6 @@ files:
|
|
104
135
|
- lib/smilodon/tasks.rb
|
105
136
|
- lib/tasks/populate.rake
|
106
137
|
- lib/tasks/populate.yml
|
107
|
-
- populator.gemspec
|
108
138
|
- smilodon.gemspec
|
109
139
|
- spec/lib/smilodon_spec.rb
|
110
140
|
- spec/lib/tasks/populate_spec.rb
|
@@ -123,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
153
|
requirements:
|
124
154
|
- - ">="
|
125
155
|
- !ruby/object:Gem::Version
|
126
|
-
hash:
|
156
|
+
hash: 3
|
127
157
|
segments:
|
128
158
|
- 0
|
129
159
|
version: "0"
|
@@ -132,11 +162,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
162
|
requirements:
|
133
163
|
- - ">="
|
134
164
|
- !ruby/object:Gem::Version
|
165
|
+
hash: 3
|
166
|
+
segments:
|
167
|
+
- 0
|
135
168
|
version: "0"
|
136
169
|
requirements: []
|
137
170
|
|
138
171
|
rubyforge_project:
|
139
|
-
rubygems_version: 1.
|
172
|
+
rubygems_version: 1.6.2
|
140
173
|
signing_key:
|
141
174
|
specification_version: 3
|
142
175
|
summary: Smilodon is a utility to parse data files.
|
data/populator.gemspec
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{populator}
|
8
|
-
s.version = "0.1.1"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Umang Chouhan"]
|
12
|
-
s.date = %q{2011-05-12}
|
13
|
-
s.description = %q{Populator is a utility to parse data files.}
|
14
|
-
s.email = %q{uchouhan@optimiscorp.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE.txt",
|
17
|
-
"README.rdoc"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".document",
|
21
|
-
".rspec",
|
22
|
-
"Gemfile",
|
23
|
-
"Gemfile.lock",
|
24
|
-
"LICENSE.txt",
|
25
|
-
"README.rdoc",
|
26
|
-
"Rakefile",
|
27
|
-
"VERSION",
|
28
|
-
"lib/populator.rb",
|
29
|
-
"lib/populator/errors.rb",
|
30
|
-
"lib/populator/fakes.rb",
|
31
|
-
"lib/populator/logger.rb",
|
32
|
-
"populator.gemspec",
|
33
|
-
"spec/populator_spec.rb",
|
34
|
-
"spec/spec_helper.rb"
|
35
|
-
]
|
36
|
-
s.homepage = %q{http://github.com/optimis/populator}
|
37
|
-
s.licenses = ["MIT"]
|
38
|
-
s.require_paths = ["lib"]
|
39
|
-
s.rubygems_version = %q{1.3.7}
|
40
|
-
s.summary = %q{Populator is a utility to parse data files.}
|
41
|
-
|
42
|
-
if s.respond_to? :specification_version then
|
43
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
-
s.specification_version = 3
|
45
|
-
|
46
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
-
s.add_runtime_dependency(%q<logging>, [">= 0"])
|
48
|
-
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
49
|
-
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
50
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
51
|
-
s.add_development_dependency(%q<rcov>, [">= 0"])
|
52
|
-
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
53
|
-
else
|
54
|
-
s.add_dependency(%q<logging>, [">= 0"])
|
55
|
-
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
56
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
-
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
58
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
59
|
-
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
60
|
-
end
|
61
|
-
else
|
62
|
-
s.add_dependency(%q<logging>, [">= 0"])
|
63
|
-
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
64
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
65
|
-
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
66
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
67
|
-
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|