Module: SimpleImporter
- Defined in:
- lib/simple_importer.rb
Constant Summary collapse
- VERSION =
:stopdoc:
'1.0.1'
- LIBPATH =
::File.(::File.dirname(__FILE__)) + ::File::SEPARATOR
- PATH =
::File.dirname(LIBPATH) + ::File::SEPARATOR
Class Method Summary collapse
-
.libpath(*args) ⇒ Object
Returns the library path for the module.
-
.path(*args) ⇒ Object
Returns the lpath for the module.
-
.require_all_libs_relative_to(fname, dir = nil) ⇒ Object
Utility method used to rquire all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in.
-
.version ⇒ Object
Returns the version string for the library.
Instance Method Summary collapse
- #csv(path, *args, &block) ⇒ Object
- #file(path) {|file| ... } ⇒ Object
- #process_rows(parsed_file, ignore_header, &block) ⇒ Object
- #run(reset_flag = false) ⇒ Object
- #tsv(path, *args, &block) ⇒ Object
- #xml(path) ⇒ Object
Class Method Details
.libpath(*args) ⇒ Object
Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join
.
67 68 69 |
# File 'lib/simple_importer.rb', line 67 def self.libpath( *args ) args.empty? ? LIBPATH : ::File.join(LIBPATH, *args) end |
.path(*args) ⇒ Object
Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join
.
75 76 77 |
# File 'lib/simple_importer.rb', line 75 def self.path( *args ) args.empty? ? PATH : ::File.join(PATH, *args) end |
.require_all_libs_relative_to(fname, dir = nil) ⇒ Object
Utility method used to rquire all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.
84 85 86 87 88 89 90 |
# File 'lib/simple_importer.rb', line 84 def self.require_all_libs_relative_to( fname, dir = nil ) dir ||= ::File.basename(fname, '.*') search_me = ::File.( ::File.join(::File.dirname(fname), dir, '**', '*.rb')) Dir.glob(search_me).sort.each {|rb| require rb} end |
.version ⇒ Object
Returns the version string for the library.
59 60 61 |
# File 'lib/simple_importer.rb', line 59 def self.version VERSION end |
Instance Method Details
#csv(path, *args, &block) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/simple_importer.rb', line 16 def csv(path, *args, &block) file(path) do |f| parsed_file = CSV::Reader.parse(f) process_rows(parsed_file, !args.empty?, &block) end end |
#file(path) {|file| ... } ⇒ Object
10 11 12 13 14 |
# File 'lib/simple_importer.rb', line 10 def file(path) file = File.open(path) yield file if block_given? file.close end |
#process_rows(parsed_file, ignore_header, &block) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/simple_importer.rb', line 30 def process_rows(parsed_file, ignore_header, &block) parsed_file.shift if ignore_header parsed_file.each do |row| row.each {|v| v.trim! if v.respond_to? :trim! } yield row if block_given? end end |
#run(reset_flag = false) ⇒ Object
46 47 48 49 |
# File 'lib/simple_importer.rb', line 46 def run(reset_flag = false) reset if reset_flag && respond_to?(:reset) import end |
#tsv(path, *args, &block) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/simple_importer.rb', line 23 def tsv(path, *args, &block) file(path) do |f| parsed_file = CSV::Reader.parse(f, "\t") process_rows(parsed_file, !args.empty?, &block) end end |
#xml(path) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/simple_importer.rb', line 38 def xml(path) require 'hpricot' unless defined? Hpricot file(path) do |f| doc = Hpricot(f) yield doc if block_given? end end |