Class: MovableErb

Inherits:
Object
  • Object
show all
Defined in:
lib/movable_erb.rb

Overview

Author:

  • Joshua Davey

Defined Under Namespace

Classes: CSV, Erb

Constant Summary collapse

VERSION =
File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp
DEFAULT_TEMPLATE =
File.expand_path(File.dirname(__FILE__) + '/templates/mtimport.erb')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MovableErb

Returns a new instance of MovableErb.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :template (String) — default: "mtimport.erb"

    Path to the ERB template file to use

  • :csv (String)

    Required. Path to the CSV file to convert

  • :separator (String) — default: "") Defaults to empty String, ""

    “”) Defaults to empty String, “”



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/movable_erb.rb', line 13

def initialize(options = {})
  @erb = MovableErb::Erb.setup do |erb|
    erb.template = options[:template] || DEFAULT_TEMPLATE
  end
  if options[:csv]
    @csv = MovableErb::CSV.setup do |csv|
      csv.filename = options[:csv]
    end
  end
  @separator = options[:separator] || ""
end

Instance Attribute Details

#csvObject

Returns the value of attribute csv.



4
5
6
# File 'lib/movable_erb.rb', line 4

def csv
  @csv
end

#erbObject

Returns the value of attribute erb.



4
5
6
# File 'lib/movable_erb.rb', line 4

def erb
  @erb
end

#separatorObject

Returns the value of attribute separator.



4
5
6
# File 'lib/movable_erb.rb', line 4

def separator
  @separator
end

Instance Method Details

#convertString

Converts each row of the CSV file and collects it into @results

Returns:

  • (String)

    parsed results joined by the separator



28
29
30
31
32
33
34
35
36
# File 'lib/movable_erb.rb', line 28

def convert
  @results = []
  csv.parse!
  csv.hashes.each do |hash_data|
    erb.data = hash_data
    @results << erb.build!
  end
  @results.join(separator)
end