jgdavey-movable_erb 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/README.textile +49 -0
- data/Rakefile +40 -0
- data/bin/movable_erb +66 -0
- data/lib/movable_erb.rb +49 -0
- data/lib/movable_erb/csv.rb +30 -0
- data/lib/movable_erb/mtimport.rb +65 -0
- data/lib/movable_erb/templates/default.erb +21 -0
- data/movable_erb.gemspec +40 -0
- data/spec/csv_spec.rb +73 -0
- data/spec/fixtures/example.csv +5 -0
- data/spec/movable_erb_spec.rb +10 -0
- data/spec/mtimport_spec.rb +173 -0
- data/spec/spec_helper.rb +18 -0
- data/test/test_movable_erb.rb +0 -0
- metadata +89 -0
data/History.txt
ADDED
data/README.textile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
h1. Movable Erb
|
2
|
+
by Joshua Davey
|
3
|
+
http://github.com/jgdavey/movable_erb
|
4
|
+
|
5
|
+
h2. DESCRIPTION:
|
6
|
+
|
7
|
+
A simple CSV to MTImport conversion utility
|
8
|
+
|
9
|
+
h2. FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Commandline access
|
12
|
+
|
13
|
+
h2. SYNOPSIS:
|
14
|
+
|
15
|
+
@movable_erb [options] yourfile.csv@
|
16
|
+
|
17
|
+
h2. REQUIREMENTS:
|
18
|
+
|
19
|
+
* fastercsv gem
|
20
|
+
|
21
|
+
h2. INSTALL:
|
22
|
+
|
23
|
+
@sudo gem install movable_erb@
|
24
|
+
(PENDING release)
|
25
|
+
|
26
|
+
h2. LICENSE:
|
27
|
+
|
28
|
+
(The MIT License)
|
29
|
+
|
30
|
+
Copyright (c) 2009
|
31
|
+
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
33
|
+
a copy of this software and associated documentation files (the
|
34
|
+
'Software'), to deal in the Software without restriction, including
|
35
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
36
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
37
|
+
permit persons to whom the Software is furnished to do so, subject to
|
38
|
+
the following conditions:
|
39
|
+
|
40
|
+
The above copyright notice and this permission notice shall be
|
41
|
+
included in all copies or substantial portions of the Software.
|
42
|
+
|
43
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
44
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
45
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
46
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
47
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
48
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
49
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Look in the tasks/setup.rb file for the various options that can be
|
2
|
+
# configured in this Rakefile. The .rake files in the tasks directory
|
3
|
+
# are where the options are used.
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'bones'
|
7
|
+
Bones.setup
|
8
|
+
rescue LoadError
|
9
|
+
begin
|
10
|
+
load 'tasks/setup.rb'
|
11
|
+
rescue LoadError
|
12
|
+
raise RuntimeError, '### please install the "bones" gem ###'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ensure_in_path 'lib'
|
17
|
+
require 'movable_erb'
|
18
|
+
|
19
|
+
task :default => 'spec:run'
|
20
|
+
|
21
|
+
PROJ.name = 'movable_erb'
|
22
|
+
PROJ.authors = 'Joshua Davey'
|
23
|
+
PROJ.email = 'josh@joshuadavey.com'
|
24
|
+
PROJ.summary = 'A simple CSV to MTImport conversion utility'
|
25
|
+
PROJ.description = 'Usage: movable_erb [options]'
|
26
|
+
PROJ.url = 'http://github.com/jgdavey/movable_erb/'
|
27
|
+
PROJ.version = MovableErb::VERSION
|
28
|
+
PROJ.rubyforge.name = 'movable_erb'
|
29
|
+
|
30
|
+
PROJ.gem.dependencies = ['fastercsv']
|
31
|
+
|
32
|
+
PROJ.readme_file = 'README.textile'
|
33
|
+
PROJ.exclude << %w(\.git ^pkg tmp$)
|
34
|
+
|
35
|
+
PROJ.notes.exclude = %w(^README\.txt$ ^data/)
|
36
|
+
PROJ.rdoc.exclude = %w(^README\.txt$ ^data/)
|
37
|
+
|
38
|
+
PROJ.spec.opts << '--color'
|
39
|
+
|
40
|
+
# EOF
|
data/bin/movable_erb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'optparse'
|
3
|
+
require 'ostruct'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
require File.expand_path(
|
7
|
+
File.join(File.dirname(__FILE__), %w[.. lib movable_erb]))
|
8
|
+
|
9
|
+
class MovableErbCLI
|
10
|
+
def self.parse(args)
|
11
|
+
# The options specified on the command line will be collected in *options*.
|
12
|
+
# We set default values here.
|
13
|
+
options = OpenStruct.new
|
14
|
+
options.template = nil
|
15
|
+
options.file = ''
|
16
|
+
|
17
|
+
opts = OptionParser.new do |opts|
|
18
|
+
opts.banner = "Usage: movable_erb [options]"
|
19
|
+
|
20
|
+
opts.separator ""
|
21
|
+
opts.separator "It is often useful to pipe the output from STDOUT to a file"
|
22
|
+
opts.separator "e.g. # movable_erb -f yourfile.csv > import.txt"
|
23
|
+
|
24
|
+
opts.separator ""
|
25
|
+
opts.separator "Specific options:"
|
26
|
+
|
27
|
+
opts.on("-f", "--file FILE", "CSV file to parse") do |f|
|
28
|
+
options.file = f
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on("-t", "--template TEMPLATE", "Template to use (default is MTImport)") do |t|
|
32
|
+
options.template = t
|
33
|
+
end
|
34
|
+
|
35
|
+
opts.separator ""
|
36
|
+
opts.separator "Common options:"
|
37
|
+
|
38
|
+
# No argument, shows at tail. This will print an options summary.
|
39
|
+
# Try it and see!
|
40
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
41
|
+
puts opts
|
42
|
+
exit
|
43
|
+
end
|
44
|
+
|
45
|
+
# Another typical switch to print the version.
|
46
|
+
opts.on_tail("--version", "Show version") do
|
47
|
+
puts MovableErb::VERSION
|
48
|
+
exit
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
opts.parse!(args)
|
53
|
+
if options.file == ''
|
54
|
+
opts
|
55
|
+
else
|
56
|
+
mtnew = MovableErb::MTImport.new(:csv => {:file => options.file,:template => options.template })
|
57
|
+
mtnew.setup_column_nums
|
58
|
+
mtnew.render_with_template
|
59
|
+
end
|
60
|
+
end # parse()
|
61
|
+
end
|
62
|
+
|
63
|
+
result = MovableErbCLI.parse(ARGV)
|
64
|
+
puts result
|
65
|
+
|
66
|
+
# EOF
|
data/lib/movable_erb.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
|
2
|
+
module MovableErb
|
3
|
+
|
4
|
+
# :stopdoc:
|
5
|
+
VERSION = '0.1.2'
|
6
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
7
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
8
|
+
# :startdoc:
|
9
|
+
|
10
|
+
# Returns the version string for the library.
|
11
|
+
#
|
12
|
+
def self.version
|
13
|
+
VERSION
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the library path for the module. If any arguments are given,
|
17
|
+
# they will be joined to the end of the libray path using
|
18
|
+
# <tt>File.join</tt>.
|
19
|
+
#
|
20
|
+
def self.libpath( *args )
|
21
|
+
args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the lpath for the module. If any arguments are given,
|
25
|
+
# they will be joined to the end of the path using
|
26
|
+
# <tt>File.join</tt>.
|
27
|
+
#
|
28
|
+
def self.path( *args )
|
29
|
+
args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Utility method used to require all files ending in .rb that lie in the
|
33
|
+
# directory below this file that has the same name as the filename passed
|
34
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
35
|
+
# the _filename_ does not have to be equivalent to the directory.
|
36
|
+
#
|
37
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
38
|
+
dir ||= ::File.basename(fname, '.*')
|
39
|
+
search_me = ::File.expand_path(
|
40
|
+
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
41
|
+
|
42
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
43
|
+
end
|
44
|
+
|
45
|
+
end # module MovableErb
|
46
|
+
|
47
|
+
MovableErb.require_all_libs_relative_to(__FILE__)
|
48
|
+
|
49
|
+
# EOF
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'fastercsv'
|
3
|
+
|
4
|
+
module MovableErb
|
5
|
+
class Csv
|
6
|
+
attr_reader :file
|
7
|
+
def initialize(args)
|
8
|
+
if args[:file]
|
9
|
+
args[:file].gsub!(/^([^\.\/])/,'./\1')
|
10
|
+
@file = args[:file]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def rows
|
15
|
+
FasterCSV.read(file)
|
16
|
+
end
|
17
|
+
|
18
|
+
def header
|
19
|
+
rows.first
|
20
|
+
end
|
21
|
+
|
22
|
+
def data
|
23
|
+
rows[1..rows.length]
|
24
|
+
end
|
25
|
+
|
26
|
+
def body
|
27
|
+
data
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module MovableErb
|
2
|
+
class MTImport
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
COLUMNNAMES = ['title','body','extended', 'category', 'tags']
|
6
|
+
|
7
|
+
attr_accessor :csv, :template, :columns
|
8
|
+
|
9
|
+
def initialize(args = {})
|
10
|
+
if args[:csv]
|
11
|
+
@csv = Csv.new(args[:csv])
|
12
|
+
@columns = {:body => nil, :title => nil}
|
13
|
+
end
|
14
|
+
@template = args[:template] || File.join(File.dirname(__FILE__), 'templates', 'default.erb')
|
15
|
+
end
|
16
|
+
|
17
|
+
def header_rows
|
18
|
+
if @csv
|
19
|
+
@csv.header
|
20
|
+
else
|
21
|
+
raise "No CSV file"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup_column_nums
|
26
|
+
COLUMNNAMES.each do |colname|
|
27
|
+
@columns[colname.to_sym] = header_rows.to_enum(:each_with_index).collect do |x,i|
|
28
|
+
i if x.downcase == colname
|
29
|
+
end.compact
|
30
|
+
end
|
31
|
+
# Useful defaults
|
32
|
+
@columns[:title] = [0] if @columns[:title].empty?
|
33
|
+
@columns[:body] = [1] if @columns[:body].empty?
|
34
|
+
@columns
|
35
|
+
end
|
36
|
+
|
37
|
+
def column_nums_for(column)
|
38
|
+
@columns[column.to_sym]
|
39
|
+
end
|
40
|
+
|
41
|
+
def content_for(column, row = 0)
|
42
|
+
content = column_nums_for(column).map do |i|
|
43
|
+
csv.body[row][i]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def render_with_template(template = @template)
|
49
|
+
rendered = []
|
50
|
+
csv.body.each_with_index do |row, i|
|
51
|
+
title = content_for('title', i).join(" ")
|
52
|
+
body = content_for('body', i).join("\n")
|
53
|
+
extended = content_for('extended', i).join("\n")
|
54
|
+
category = content_for('category', i).join(" ")
|
55
|
+
tags = content_for('tags', i).join(", ")
|
56
|
+
erb = File.open(template, "rb").read
|
57
|
+
r = ERB.new(erb, 0, '<>') if erb
|
58
|
+
b = binding
|
59
|
+
rendered << r.result(b)
|
60
|
+
end
|
61
|
+
rendered.join("--------\n")
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
TITLE: <%= title %>
|
2
|
+
<% if category && !category.empty? %>
|
3
|
+
CATEGORY: <%= category %>
|
4
|
+
<% end %>
|
5
|
+
<% if tags && !tags.empty? %>
|
6
|
+
TAGS: <%= tags %>
|
7
|
+
<% end %>
|
8
|
+
-----
|
9
|
+
BODY:
|
10
|
+
|
11
|
+
<%= body %>
|
12
|
+
|
13
|
+
|
14
|
+
<% unless extended.nil? || extended.empty? %>
|
15
|
+
-----
|
16
|
+
EXTENDED:
|
17
|
+
|
18
|
+
<%= extended %>
|
19
|
+
|
20
|
+
|
21
|
+
<% end %>
|
data/movable_erb.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{movable_erb}
|
5
|
+
s.version = "0.1.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Joshua Davey"]
|
9
|
+
s.date = %q{2009-04-18}
|
10
|
+
s.default_executable = %q{movable_erb}
|
11
|
+
s.description = %q{Usage: movable_erb [options]}
|
12
|
+
s.email = %q{josh@joshuadavey.com}
|
13
|
+
s.executables = ["movable_erb"]
|
14
|
+
s.extra_rdoc_files = ["History.txt", "bin/movable_erb", "lib/movable_erb/templates/default.erb"]
|
15
|
+
s.files = ["History.txt", "README.textile", "Rakefile", "bin/movable_erb", "lib/movable_erb.rb", "lib/movable_erb/csv.rb", "lib/movable_erb/mtimport.rb", "lib/movable_erb/templates/default.erb", "movable_erb.gemspec", "spec/csv_spec.rb", "spec/fixtures/example.csv", "spec/movable_erb_spec.rb", "spec/mtimport_spec.rb", "spec/spec_helper.rb", "test/test_movable_erb.rb"]
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.homepage = %q{http://github.com/jgdavey/movable_erb/}
|
18
|
+
s.rdoc_options = ["--main", "README.textile"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubyforge_project = %q{movable_erb}
|
21
|
+
s.rubygems_version = %q{1.3.2}
|
22
|
+
s.summary = %q{A simple CSV to MTImport conversion utility}
|
23
|
+
s.test_files = ["test/test_movable_erb.rb"]
|
24
|
+
|
25
|
+
if s.respond_to? :specification_version then
|
26
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
27
|
+
s.specification_version = 3
|
28
|
+
|
29
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
30
|
+
s.add_runtime_dependency(%q<fastercsv>, [">= 0"])
|
31
|
+
s.add_development_dependency(%q<bones>, [">= 2.5.0"])
|
32
|
+
else
|
33
|
+
s.add_dependency(%q<fastercsv>, [">= 0"])
|
34
|
+
s.add_dependency(%q<bones>, [">= 2.5.0"])
|
35
|
+
end
|
36
|
+
else
|
37
|
+
s.add_dependency(%q<fastercsv>, [">= 0"])
|
38
|
+
s.add_dependency(%q<bones>, [">= 2.5.0"])
|
39
|
+
end
|
40
|
+
end
|
data/spec/csv_spec.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
|
2
|
+
require File.join(File.dirname(__FILE__), %w[spec_helper])
|
3
|
+
|
4
|
+
describe MovableErb::Csv do
|
5
|
+
describe "intialization" do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@csv = MovableErb::Csv.new({:file => 'example.csv'})
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should accept arguments" do
|
12
|
+
@csv.should_not be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have @file attribute match passed in value" do
|
16
|
+
@csv.file.should eql('./example.csv')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should allow files in sub-folders" do
|
20
|
+
@csv = MovableErb::Csv.new({:file => 'subfolder/example.csv'})
|
21
|
+
@csv.file.should eql('./subfolder/example.csv')
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should recognize the dot as current folder" do
|
25
|
+
@csv = MovableErb::Csv.new({:file => './example.csv'})
|
26
|
+
@csv.file.should eql('./example.csv')
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "opening the file" do
|
32
|
+
before(:each) do
|
33
|
+
@file_to_open = File.join(File.dirname(__FILE__), 'fixtures', 'example.csv')
|
34
|
+
@csv = MovableErb::Csv.new({:file => @file_to_open})
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should find the file" do
|
38
|
+
File.open(@file_to_open).should_not be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should populate rows" do
|
42
|
+
@csv.rows.should_not be_nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "rows" do
|
47
|
+
before(:each) do
|
48
|
+
@file_to_open = File.join(File.dirname(__FILE__), 'fixtures', 'example.csv')
|
49
|
+
@csv = MovableErb::Csv.new({:file => @file_to_open})
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should be an array of arrays" do
|
53
|
+
@csv.rows.should be_instance_of(Array)
|
54
|
+
@csv.rows.each do |row|
|
55
|
+
row.should be_instance_of(Array)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should have a header row" do
|
60
|
+
@csv.header.should eql(['Name','Phone','Email'])
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should exclude header from data" do
|
64
|
+
@csv.data.first.should_not eql(['Name','Phone','Email'])
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should should have data" do
|
68
|
+
@csv.data.first.should eql(['John','773-123-1234','john@example.com'])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# EOF
|
@@ -0,0 +1,173 @@
|
|
1
|
+
|
2
|
+
require File.join(File.dirname(__FILE__), %w[spec_helper])
|
3
|
+
|
4
|
+
describe MovableErb::MTImport do
|
5
|
+
|
6
|
+
it "should exist" do
|
7
|
+
MovableErb::MTImport.should_not be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "initialization" do
|
11
|
+
before(:each) do
|
12
|
+
@mt = MovableErb::MTImport.new(:csv => {:file => 'does_not_exist.csv'})
|
13
|
+
@mt.csv.stub!(:header).and_return(['Title','Body'])
|
14
|
+
@mt.setup_column_nums
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should have a title column in first column by default" do
|
18
|
+
@mt.columns[:title].should eql([0])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should have a body column in second column by default" do
|
22
|
+
@mt.columns[:body].should eql([1])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "with non-default order of header rows" do
|
27
|
+
before(:each) do
|
28
|
+
@mt = MovableErb::MTImport.new(:csv => {:file => 'does_not_exist.csv'})
|
29
|
+
@mt.csv.stub!(:header).and_return(['Body','Title'])
|
30
|
+
@mt.setup_column_nums
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should find and correctly assign the title column" do
|
34
|
+
@mt.columns[:title].should eql([1])
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should find and correctly assign the body column" do
|
38
|
+
@mt.columns[:body].should eql([0])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "joining field titles with same name" do
|
43
|
+
before(:each) do
|
44
|
+
@mt = MovableErb::MTImport.new(:csv => {:file => 'does_not_exist.csv'})
|
45
|
+
@mt.csv.stub!(:header).and_return(['Title','Body','Body'])
|
46
|
+
@mt.csv.stub!(:body).and_return([['A Title', 'Part of the body','is right here'],['Title 2', 'Body 2','is right here']])
|
47
|
+
@mt.setup_column_nums
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should find more than one element with Body if given" do
|
51
|
+
@mt.columns[:body].should eql([1,2])
|
52
|
+
@mt.content_for('body',0).should eql(['Part of the body','is right here'])
|
53
|
+
@mt.content_for('body',1).should eql(['Body 2','is right here'])
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
it "should render with a template file" do
|
58
|
+
@mt.render_with_template.should eql(
|
59
|
+
%q{TITLE: A Title
|
60
|
+
-----
|
61
|
+
BODY:
|
62
|
+
|
63
|
+
Part of the body
|
64
|
+
is right here
|
65
|
+
|
66
|
+
--------
|
67
|
+
TITLE: Title 2
|
68
|
+
-----
|
69
|
+
BODY:
|
70
|
+
|
71
|
+
Body 2
|
72
|
+
is right here
|
73
|
+
|
74
|
+
}.gsub(/^ +/,'') )
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "non-default fields" do
|
79
|
+
before(:each) do
|
80
|
+
@mt = MovableErb::MTImport.new(:csv => {:file => 'spec/fixtures/example.csv'})
|
81
|
+
@mt.csv.stub!(:header).and_return(['Title','Body','Category',"Extended"])
|
82
|
+
@mt.csv.stub!(:body).and_return([['A Title', 'Part of the body','Articles','Another field'],['Title 2', 'Body 2','Articles','field 2']])
|
83
|
+
@mt.setup_column_nums
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should recognize an extended field" do
|
87
|
+
@mt.columns[:extended].should_not be_nil
|
88
|
+
@mt.columns[:extended].should be_instance_of(Array)
|
89
|
+
@mt.columns[:extended].should eql([3])
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should recognize the category field" do
|
93
|
+
@mt.columns[:category].should_not be_nil
|
94
|
+
@mt.columns[:category].should be_instance_of(Array)
|
95
|
+
@mt.columns[:category].should eql([2])
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should correctly render" do
|
99
|
+
@mt.render_with_template.should eql(
|
100
|
+
%q{TITLE: A Title
|
101
|
+
CATEGORY: Articles
|
102
|
+
-----
|
103
|
+
BODY:
|
104
|
+
|
105
|
+
Part of the body
|
106
|
+
|
107
|
+
-----
|
108
|
+
EXTENDED:
|
109
|
+
|
110
|
+
Another field
|
111
|
+
|
112
|
+
--------
|
113
|
+
TITLE: Title 2
|
114
|
+
CATEGORY: Articles
|
115
|
+
-----
|
116
|
+
BODY:
|
117
|
+
|
118
|
+
Body 2
|
119
|
+
|
120
|
+
-----
|
121
|
+
EXTENDED:
|
122
|
+
|
123
|
+
field 2
|
124
|
+
|
125
|
+
}.gsub(/^ +/,''))
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "tags" do
|
130
|
+
before(:each) do
|
131
|
+
@mt = MovableErb::MTImport.new(:csv => {:file => 'spec/fixtures/example.csv'})
|
132
|
+
@mt.csv.stub!(:header).and_return(['Title','Body','Tags'])
|
133
|
+
@mt.csv.stub!(:body).and_return([['A Title', 'Body content','dog, cats, mice']])
|
134
|
+
@mt.setup_column_nums
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should recognize tags" do
|
138
|
+
@mt.columns[:tags].should_not be_nil
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should render correctly" do
|
142
|
+
@mt.render_with_template.should eql(
|
143
|
+
%q{TITLE: A Title
|
144
|
+
TAGS: dog, cats, mice
|
145
|
+
-----
|
146
|
+
BODY:
|
147
|
+
|
148
|
+
Body content
|
149
|
+
|
150
|
+
}.gsub(/^ +/,''))
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "when header column is not named/misnamed" do
|
155
|
+
before(:each) do
|
156
|
+
@mt = MovableErb::MTImport.new(:csv => {:file => 'spec/fixtures/example.csv'})
|
157
|
+
@mt.csv.stub!(:header).and_return(['Name','Address','Nothing'])
|
158
|
+
@mt.csv.stub!(:body).and_return([['John Boy', '123 Nowhere Lane','dog, cats, mice']])
|
159
|
+
@mt.setup_column_nums
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should default the first field to title" do
|
163
|
+
@mt.columns[:title].should_not be_nil
|
164
|
+
@mt.columns[:title].should eql([0])
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should default the second field to body" do
|
168
|
+
@mt.columns[:body].should_not be_nil
|
169
|
+
@mt.columns[:body].should eql([1])
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'fastercsv'
|
3
|
+
|
4
|
+
require File.expand_path(
|
5
|
+
File.join(File.dirname(__FILE__), %w[.. lib movable_erb]))
|
6
|
+
|
7
|
+
Spec::Runner.configure do |config|
|
8
|
+
# == Mock Framework
|
9
|
+
#
|
10
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
11
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
12
|
+
#
|
13
|
+
# config.mock_with :mocha
|
14
|
+
# config.mock_with :flexmock
|
15
|
+
# config.mock_with :rr
|
16
|
+
end
|
17
|
+
|
18
|
+
# EOF
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jgdavey-movable_erb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Davey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-18 00:00:00 -07:00
|
13
|
+
default_executable: movable_erb
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: fastercsv
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bones
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.5.0
|
34
|
+
version:
|
35
|
+
description: "Usage: movable_erb [options]"
|
36
|
+
email: josh@joshuadavey.com
|
37
|
+
executables:
|
38
|
+
- movable_erb
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- History.txt
|
43
|
+
- bin/movable_erb
|
44
|
+
- lib/movable_erb/templates/default.erb
|
45
|
+
files:
|
46
|
+
- History.txt
|
47
|
+
- README.textile
|
48
|
+
- Rakefile
|
49
|
+
- bin/movable_erb
|
50
|
+
- lib/movable_erb.rb
|
51
|
+
- lib/movable_erb/csv.rb
|
52
|
+
- lib/movable_erb/mtimport.rb
|
53
|
+
- lib/movable_erb/templates/default.erb
|
54
|
+
- movable_erb.gemspec
|
55
|
+
- spec/csv_spec.rb
|
56
|
+
- spec/fixtures/example.csv
|
57
|
+
- spec/movable_erb_spec.rb
|
58
|
+
- spec/mtimport_spec.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
- test/test_movable_erb.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/jgdavey/movable_erb/
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --main
|
66
|
+
- README.textile
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: movable_erb
|
84
|
+
rubygems_version: 1.2.0
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: A simple CSV to MTImport conversion utility
|
88
|
+
test_files:
|
89
|
+
- test/test_movable_erb.rb
|