movable_erb 0.2.5 → 0.2.6
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/Rakefile +6 -2
- data/VERSION +1 -1
- data/lib/movable_erb.rb +35 -18
- data/movable_erb.gemspec +5 -4
- data/spec/csv_spec.rb +16 -9
- data/spec/fixtures/template.erb +2 -2
- data/spec/spec_helper.rb +8 -0
- metadata +2 -2
data/Rakefile
CHANGED
@@ -16,6 +16,10 @@ rescue LoadError
|
|
16
16
|
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
17
17
|
end
|
18
18
|
|
19
|
+
require 'spec/rake/spectask'
|
20
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
21
|
+
spec.libs << 'lib' << 'spec'
|
22
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
23
|
+
end
|
19
24
|
|
20
|
-
|
21
|
-
|
25
|
+
task :default => :spec
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.6
|
data/lib/movable_erb.rb
CHANGED
@@ -39,20 +39,28 @@ end
|
|
39
39
|
##
|
40
40
|
# Loads a CSV document into an array of hashes
|
41
41
|
class MovableErb::CSV
|
42
|
-
|
42
|
+
if RUBY_VERSION =~ /1.9/
|
43
|
+
require 'csv'
|
44
|
+
CSV_PARSER = ::CSV
|
45
|
+
else
|
46
|
+
require 'fastercsv'
|
47
|
+
CSV_PARSER = FasterCSV
|
48
|
+
end
|
43
49
|
|
44
50
|
attr_accessor :filename, :hashes
|
51
|
+
attr_reader :headers
|
45
52
|
|
46
53
|
##
|
47
54
|
# Initializes and yields a new instance
|
48
55
|
# @yield [csv] a new instance of {MovableErb::CSV}
|
49
56
|
# @return [MovableErb::CSV]
|
50
57
|
def self.setup(&block)
|
58
|
+
raise "no block given" unless block_given?
|
51
59
|
csv = self.new
|
52
60
|
yield csv
|
53
61
|
csv.parse!
|
54
62
|
end
|
55
|
-
|
63
|
+
|
56
64
|
##
|
57
65
|
# Internally calls {#to_hashes}, but returns self
|
58
66
|
# @see to_hashes
|
@@ -61,24 +69,32 @@ class MovableErb::CSV
|
|
61
69
|
@hashes = self.to_hashes
|
62
70
|
self
|
63
71
|
end
|
64
|
-
|
72
|
+
|
65
73
|
##
|
66
74
|
# Reads the CSV file into an array of hashes
|
67
75
|
# @return [Array] an Array of Hashes
|
68
76
|
def to_hashes
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
77
|
+
raw_arrays = CSV_PARSER.read(filename)
|
78
|
+
extract_headers!(raw_arrays)
|
79
|
+
raw_arrays.map {|row| to_hash(row) }
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
|
84
|
+
def extract_headers!(array)
|
85
|
+
first_row = array.shift
|
86
|
+
first_row.each { |h| h.downcase! && h.gsub!(/\s/,"_") } if first_row
|
87
|
+
@headers = first_row
|
88
|
+
end
|
89
|
+
|
90
|
+
def to_hash(cells)
|
91
|
+
hash = Hash.new
|
92
|
+
headers.each_with_index do |column_name, column_index|
|
93
|
+
next unless cells[column_index]
|
94
|
+
hash[column_name] ||= []
|
95
|
+
hash[column_name] << cells[column_index]
|
80
96
|
end
|
81
|
-
|
97
|
+
hash
|
82
98
|
end
|
83
99
|
end
|
84
100
|
|
@@ -88,14 +104,14 @@ class MovableErb::Erb
|
|
88
104
|
require 'erb'
|
89
105
|
|
90
106
|
attr_accessor :template, :parsed_string, :data
|
91
|
-
|
107
|
+
|
92
108
|
##
|
93
109
|
# Creates a new instance and allow manipulation of it via block.
|
94
|
-
#
|
110
|
+
#
|
95
111
|
# This can be used to initialize and parse quickly.
|
96
112
|
#
|
97
113
|
# @yield [erb] a new instance of {MovableErb::Erb}
|
98
|
-
#
|
114
|
+
#
|
99
115
|
# @example Create a new instance, setup and build the template
|
100
116
|
# @erb = MovableErb::Erb.setup do |erb|
|
101
117
|
# erb.data = {'hash' => 'of', 'meaningful' => 'values'}
|
@@ -104,6 +120,7 @@ class MovableErb::Erb
|
|
104
120
|
# end
|
105
121
|
# @return [MovableErb::Erb]
|
106
122
|
def self.setup
|
123
|
+
raise "no block given" unless block_given?
|
107
124
|
erb = self.new
|
108
125
|
yield erb
|
109
126
|
erb
|
data/movable_erb.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{movable_erb}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Joshua Davey"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-12-20}
|
13
13
|
s.default_executable = %q{movable_erb}
|
14
14
|
s.description = %q{A General-purpose CSV to ERB template formatter. Useful for converting legacy CSV data to an importable blog format.}
|
15
15
|
s.email = %q{josh@joshuadavey.com}
|
@@ -66,3 +66,4 @@ Gem::Specification.new do |s|
|
|
66
66
|
s.add_dependency(%q<fastercsv>, [">= 1.5.0"])
|
67
67
|
end
|
68
68
|
end
|
69
|
+
|
data/spec/csv_spec.rb
CHANGED
@@ -3,6 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
TEMPLATE_FIXTURE = File.expand_path(File.dirname(__FILE__) + '/fixtures/template.erb')
|
4
4
|
CSV_FIXTURE = File.expand_path(File.dirname(__FILE__) + '/fixtures/example.csv')
|
5
5
|
|
6
|
+
|
7
|
+
if RUBY_VERSION =~ /1.9/
|
8
|
+
CSV_PARSER = ::CSV
|
9
|
+
else
|
10
|
+
CSV_PARSER = FasterCSV
|
11
|
+
end
|
12
|
+
|
6
13
|
describe MovableErb do
|
7
14
|
it "should have a CSV instance" do
|
8
15
|
m = MovableErb.new
|
@@ -120,7 +127,7 @@ describe MovableErb::CSV do
|
|
120
127
|
context "shortcut setup class method" do
|
121
128
|
before(:each) do
|
122
129
|
MovableErb::CSV.any_instance.stubs(:filename).returns("fake")
|
123
|
-
|
130
|
+
CSV_PARSER.stubs(:read).returns([])
|
124
131
|
end
|
125
132
|
|
126
133
|
it "should create a new instance" do
|
@@ -153,7 +160,7 @@ describe MovableErb::CSV do
|
|
153
160
|
|
154
161
|
describe "parsing" do
|
155
162
|
before(:each) do
|
156
|
-
|
163
|
+
CSV_PARSER.stubs(:read).returns([["row1"], ["row2"]])
|
157
164
|
@csv = MovableErb::CSV.new
|
158
165
|
@csv.filename = "test.csv"
|
159
166
|
end
|
@@ -169,7 +176,7 @@ describe MovableErb::CSV do
|
|
169
176
|
|
170
177
|
context "#to_hashes" do
|
171
178
|
it "should read from file" do
|
172
|
-
|
179
|
+
CSV_PARSER.expects(:read).with("test.csv").returns([[]])
|
173
180
|
@csv.to_hashes
|
174
181
|
end
|
175
182
|
|
@@ -178,17 +185,17 @@ describe MovableErb::CSV do
|
|
178
185
|
end
|
179
186
|
|
180
187
|
it "should downcase the header row" do
|
181
|
-
|
188
|
+
CSV_PARSER.stubs(:read).returns([["Name"],["Billy Bob"]])
|
182
189
|
@csv.to_hashes.should == [{'name' => ['Billy Bob']}]
|
183
190
|
end
|
184
191
|
|
185
192
|
it "should convert the header row to snake_case" do
|
186
|
-
|
193
|
+
CSV_PARSER.stubs(:read).returns([["Extended Body"],["Billy Bob"]])
|
187
194
|
@csv.to_hashes.should == [{'extended_body' => ['Billy Bob']}]
|
188
195
|
end
|
189
196
|
|
190
197
|
it "should return an array of hashes (3 rows, 3 columns)" do
|
191
|
-
|
198
|
+
CSV_PARSER.stubs(:read).returns([["Name", "Phone", "Email"],
|
192
199
|
["John", "773-123-1234", "john@example.com"],
|
193
200
|
["Abigail", nil, "abby@example.com"],
|
194
201
|
["Casius", nil, nil]])
|
@@ -200,12 +207,12 @@ describe MovableErb::CSV do
|
|
200
207
|
end
|
201
208
|
|
202
209
|
it "should collect values with the same key" do
|
203
|
-
|
210
|
+
CSV_PARSER.stubs(:read).returns([["Name", "Name", "Email"], ["John", "James", "john@example.com"]])
|
204
211
|
@csv.to_hashes.should == [{'name' => ['John', 'James'],'email' => ['john@example.com']}]
|
205
212
|
end
|
206
213
|
|
207
214
|
it "should collect values with the same key (more than 2)" do
|
208
|
-
|
215
|
+
CSV_PARSER.stubs(:read).returns([["Name", "Name", "Name"], ["John", "James", "Jill"]])
|
209
216
|
@csv.to_hashes.should == [{'name' => ['John', 'James', 'Jill']}]
|
210
217
|
end
|
211
218
|
end
|
@@ -278,7 +285,7 @@ describe MovableErb::Erb do
|
|
278
285
|
|
279
286
|
it "should parse a template with data given" do
|
280
287
|
@erb.template = TEMPLATE_FIXTURE
|
281
|
-
@erb.data = {'name' => 'Johnny', 'email' => 'john@example.com'}
|
288
|
+
@erb.data = {'name' => ['Johnny'], 'email' => ['john@example.com']}
|
282
289
|
@erb.build!
|
283
290
|
@erb.parsed_string.should == <<-ERB.gsub(/^\s+/,'')
|
284
291
|
Name: Johnny
|
data/spec/fixtures/template.erb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
+
if RUBY_VERSION =~ /1.9/
|
4
|
+
require 'csv'
|
5
|
+
$csv_library = ::CSV
|
6
|
+
else
|
7
|
+
require 'fastercsv'
|
8
|
+
$csv_library = FasterCSV
|
9
|
+
end
|
10
|
+
|
3
11
|
require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib movable_erb]))
|
4
12
|
|
5
13
|
Spec::Runner.configure do |config|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: movable_erb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Davey
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-12-20 00:00:00 -06:00
|
13
13
|
default_executable: movable_erb
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|