Permalink
Browse files

Documentation using YARD

  • Loading branch information...
1 parent 20ddcaa commit 8bdbd81d3eb9e143396e57d5335146fc171b451f @jgdavey committed Oct 8, 2009
Showing with 65 additions and 70 deletions.
  1. +1 −0 .gitignore
  2. +1 −2 LICENSE
  3. +1 −1 Manifest
  4. +8 −15 README.md → README.rdoc
  5. +51 −16 lib/movable_erb.rb
  6. +2 −2 movable_erb.gemspec
  7. +1 −34 spec/csv_spec.rb
View
@@ -1,3 +1,4 @@
coverage/
pkg/
doc/
+.yardoc
View
@@ -1,5 +1,4 @@
-The MIT License
----------------
+== The MIT License
Copyright (c) 2009
View
@@ -1,7 +1,7 @@
CHANGELOG
LICENSE
Manifest
-README.md
+README.rdoc
Rakefile
bin/movable_erb
cucumber.yml
View
@@ -1,39 +1,32 @@
-Movable Erb
-===========
+= Movable Erb
by Joshua Davey
http://github.com/jgdavey/movable_erb
-Description
------------
+== Description
A simple CSV to MTImport conversion utility.
Useful for mapping contacts data into individual entries in Movable Type.
MTImport format is simple enough that most blogging platforms can import it, so this utility could ease the pain of moving some older data structure in CSV format into a blog platform.
-Requirements
-------------
+== Requirements
* fastercsv gem
-Installation
-------------
+== Installation
- [sudo] gem install jgdavey-movable_erb
+ [sudo] gem install jgdavey-movable_erb
-Usage
------
+== Usage
After installing the gem, just run
- movable_erb [options] yourfile.csv
+ movable_erb [options] yourfile.csv
-
-The MIT License
----------------
+== The MIT License
Copyright (c) 2009
View
@@ -1,9 +1,14 @@
class MovableErb
- VERSION = "0.2.0"
+ VERSION = "0.2.4"
attr_accessor :csv, :erb, :separator
DEFAULT_TEMPLATE = File.expand_path(File.dirname(__FILE__) + '/templates/mtimport.erb')
+ ##
+ # @param [Hash] options
+ # @option options [String] :template ("mtimport.erb") Path to the ERB template file to use
+ # @option options [String] :csv <b>Required.</b> Path to the CSV file to convert
+ # @option options [String] :separator ("") Defaults to empty String, ""
def initialize(options = {})
@erb = MovableErb::Erb.setup do |erb|
erb.template = options[:template] || DEFAULT_TEMPLATE
@@ -16,6 +21,9 @@ def initialize(options = {})
@separator = options[:separator] || ""
end
+ ##
+ # Converts each row of the CSV file and collects it into @results
+ # @return [String] parsed results joined by the separator
def convert
@results = []
csv.parse!
@@ -27,28 +35,35 @@ def convert
end
end
+##
+# Loads a CSV document into an array of hashes
class MovableErb::CSV
require 'fastercsv'
attr_accessor :filename, :hashes
- def setup(&block)
- yield self
- parse! if @filename
- self
- end
-
- def self.setup
+ ##
+ # Initializes and yields a new instance
+ # @yield [csv] a new instance of {MovableErb::CSV}
+ # @return [MovableErb::CSV]
+ def self.setup(&block)
csv = self.new
yield csv
csv.parse!
end
-
+
+ ##
+ # Internally calls {#to_hashes}, but returns self
+ # @see to_hashes
+ # @return [MovableErb::CSV] self
def parse!
@hashes = self.to_hashes
self
end
-
+
+ ##
+ # Reads the CSV file into an array of hashes
+ # @return [Array] an Array of Hashes
def to_hashes
array_of_arrays = FasterCSV.read(filename)
headers = array_of_arrays.shift
@@ -66,27 +81,47 @@ def to_hashes
end
end
+##
+# Convenience class to setup and parse data with an ERB template.
class MovableErb::Erb
require 'erb'
attr_accessor :template, :parsed_string, :data
-
+
+ ##
+ # Creates a new instance and allow manipulation of it via block.
+ #
+ # This can be used to initialize and parse quickly.
+ #
+ # @yield [erb] a new instance of {MovableErb::Erb}
+ #
+ # @example Create a new instance, setup and build the template
+ # @erb = MovableErb::Erb.setup do |erb|
+ # erb.data = {'hash' => 'of', 'meaningful' => 'values'}
+ # erb.template = "path/to/template.erb"
+ # erb.build!
+ # end
+ # @return [MovableErb::Erb]
def self.setup
erb = self.new
yield erb
erb
end
+ ##
+ # Sets the template file to use and reads it into a string
+ #
+ # Note: this is required before {#build!} can be called.
+ # @return [String] the unparsed template
def template=(template_file)
@template = File.read template_file
end
- def setup
- yield self
- end
-
+ ##
+ # Using the specified template, this will parse the template
+ # @return [String] the parsed template
def build!
erb = ERB.new(template, nil, '<>')
@parsed_string = erb.result(binding) if erb
end
-end
+end
View
@@ -6,15 +6,15 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Joshua Davey"]
- s.date = %q{2009-09-26}
+ s.date = %q{2009-10-07}
s.default_executable = %q{movable_erb}
s.description = %q{A General-purpose CSV to ERB template formatter}
s.email = %q{josh@joshuadavey.com}
s.executables = ["movable_erb"]
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "bin/movable_erb", "lib/movable_erb.rb", "lib/templates/mtimport.erb", "tasks/rspec.rake"]
s.files = ["CHANGELOG", "LICENSE", "Manifest", "README.md", "Rakefile", "bin/movable_erb", "cucumber.yml", "features/csv.feature", "features/step_definitions/csv_steps.rb", "features/step_definitions/tmp.csv", "features/support/env.rb", "lib/movable_erb.rb", "lib/templates/mtimport.erb", "movable_erb.gemspec", "spec/csv_spec.rb", "spec/fixtures/advanced.csv", "spec/fixtures/example.csv", "spec/fixtures/template.erb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/rspec.rake"]
s.homepage = %q{http://github.com/jgdavey/movable_erb}
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Movable_erb", "--main", "README.md"]
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Movable_erb", "--main", "README.rdoc"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{movable_erb}
s.rubygems_version = %q{1.3.5}
View
@@ -117,28 +117,7 @@
@csv = MovableErb::CSV.new
end
- it "should setup with a block" do
- @csv.setup {|csv| false }.should == @csv
- end
-
- it "should accept a file argument" do
- FasterCSV.stubs(:read).returns([])
- @csv.setup do |csv|
- csv.filename = "test.csv"
- end.should == @csv
- end
-
- it "should call parse if filename given in block" do
- @csv.expects(:parse!).returns('')
- @csv.setup { |csv| csv.filename = "test.csv" }
- end
-
- it "should not call parse if filename not given in block" do
- @csv.expects(:parse!).never
- @csv.setup { |csv| false }
- end
-
- context "shortcut setup class method" do
+ context "shortcut setup class method" do
before(:each) do
MovableErb::CSV.any_instance.stubs(:filename).returns("fake")
FasterCSV.stubs(:read).returns([])
@@ -257,18 +236,6 @@
end
end
- context "setting up with block" do
- before(:each) do
- @erb = MovableErb::Erb.new
- end
-
- it "should accept a block that yields itself" do
- @erb.setup do |erb|
- erb.should == @erb
- end
- end
- end
-
context "shortcut setup class method" do
it "should create a new instance" do
MovableErb::Erb.setup do |erb|

0 comments on commit 8bdbd81

Please sign in to comment.