mafti 0.0.1

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.
@@ -0,0 +1,10 @@
1
+ Autotest.add_hook(:initialize) {|at|
2
+ at.add_exception %r{^\.git} # ignore Version Control System
3
+ # at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
4
+ at.clear_mappings # take out the default (test/test*rb)
5
+ at.add_mapping(%r{^lib/.*\.rb$}) {|f, _|
6
+ Dir['spec/**/*.rb']
7
+ }
8
+ nil
9
+ }
10
+
@@ -0,0 +1 @@
1
+ pkg/
data/README ADDED
@@ -0,0 +1,45 @@
1
+ Stupid text formats! Everybody loves stupid text formats! Right?
2
+
3
+ What, you don't? Well, Microsoft sure does:
4
+
5
+ From http://office.microsoft.com/en-us/access/HA100069051033.aspx
6
+ > Formatted files
7
+ >
8
+ > In a formatted file, hyphens (-) and pipe characters (|) are used to organize
9
+ > the content in a grid. The records appear as rows, and fields appear as
10
+ > columns. The field names appear in the first row.
11
+ >
12
+ > --------------------------------------------
13
+ > | ID | E-mail Address |
14
+ > --------------------------------------------
15
+ > | 1 | nancy@northwindtraders.com |
16
+ > --------------------------------------------
17
+ > | 2 | andrew@northwindtraders.com |
18
+ > --------------------------------------------
19
+ > | 3 | jan@northwindtraders.com |
20
+ > --------------------------------------------
21
+ > | 4 | mariya@northwindtraders.com |
22
+ > --------------------------------------------
23
+ > | 5 | steven@northwindtraders.com |
24
+ > --------------------------------------------
25
+ > | 6 | michael@northwindtraders.com |
26
+ > --------------------------------------------
27
+ > | 7 | robert@northwindtraders.com |
28
+ > --------------------------------------------
29
+ > | 8 | laura@northwindtraders.com |
30
+ > --------------------------------------------
31
+ > | 9 | anne@northwindtraders.com |
32
+ > --------------------------------------------
33
+ >
34
+ > You only have the option of creating a formatted file when you choose to
35
+ > export the data in a table, query, form, or report along with the formatting.
36
+ > A formatted file includes only those records and fields that are included in
37
+ > the source object or displayed in the current view. Hidden columns and
38
+ > filtered rows are not exported.
39
+
40
+ This library is tiny and simple. Import files in the above format into
41
+ something sane.
42
+
43
+
44
+ Ideas:
45
+ - Generic text format converter.....?
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "mafti"
5
+ gemspec.summary = "Microsoft Access Formatted Text Importer"
6
+ gemspec.description = "Get the stupid 'formatted text' output from Microsoft Access imported to something sane"
7
+ gemspec.email = "paul.t.hinze@gmail.com"
8
+ gemspec.homepage = "http://github.com/phinze/mafti"
9
+ gemspec.authors = ["Paul Hinze"]
10
+ end
11
+ Jeweler::GemcutterTasks.new
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
14
+ end
15
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,66 @@
1
+ class Mafti
2
+
3
+ attr_accessor :lines
4
+ attr_accessor :data
5
+ attr_accessor :headers
6
+
7
+ def initialize(new_lines=[])
8
+ @lines = new_lines
9
+ @data = []
10
+ @headers = []
11
+ parse
12
+ end
13
+
14
+ def parse
15
+ @lines.each_with_index do |line, n|
16
+ line = line.strip
17
+ case line
18
+
19
+ when /^$/ # '' (empty line)
20
+ next
21
+ when /^#.*$/ # '# Foo bar baz!' (a comment)
22
+ next
23
+ when /^-+$/ # '---------------------' (divider)
24
+ next
25
+ when /^(\|[^\|]*)*\|$/ # '| Foo | Bar | Baz |' (valid data)
26
+ new_data = parse_valid_line(line)
27
+ unless headers_recorded?
28
+ @headers = new_data
29
+ else
30
+ @data << new_data
31
+ end
32
+ else
33
+ raise "Line #{n} was invalid: #{line}"
34
+ end
35
+ end
36
+ end
37
+
38
+ def as_hashes
39
+ @data.inject([]) do |hashes, d|
40
+ d = d.dup
41
+ hashes << @headers.inject({}) do |hash, head|
42
+ hash[head] = d.shift
43
+ hash
44
+ end
45
+ hashes
46
+ end
47
+ end
48
+
49
+ def self.open(filename)
50
+ f = File.open(filename)
51
+ lines = f.readlines
52
+ f.close
53
+ Mafti.new(lines)
54
+ end
55
+
56
+ protected
57
+
58
+ def parse_valid_line(line)
59
+ line.split('|').reject { |x| x.empty? }.map(&:strip)
60
+ end
61
+
62
+ def headers_recorded?
63
+ !@headers.empty?
64
+ end
65
+
66
+ end
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mafti}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Hinze"]
12
+ s.date = %q{2009-11-06}
13
+ s.description = %q{Get the stupid 'formatted text' output from Microsoft Access imported to something sane}
14
+ s.email = %q{paul.t.hinze@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".autotest",
20
+ ".gitignore",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/mafti.rb",
25
+ "mafti.gemspec",
26
+ "spec/lib/mafti_spec.rb",
27
+ "spec/spec.opts",
28
+ "spec/spec_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/phinze/mafti}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{Microsoft Access Formatted Text Importer}
35
+ s.test_files = [
36
+ "spec/lib/mafti_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
50
+
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+ require 'mafti'
3
+
4
+ describe Mafti do
5
+ describe '.open' do
6
+ describe 'for an empty file' do
7
+ it 'outputs a Mafti instance with no data' do
8
+ filename = 'empty_file.txt'
9
+ io = StringIO.new('')
10
+ File.should_receive(:open).with(filename).and_return(io)
11
+ ret = Mafti.open(filename)
12
+ ret.should be_an_instance_of(Mafti)
13
+ ret.data.should ==([])
14
+ end
15
+ end
16
+
17
+ describe 'for a valid file' do
18
+ it 'returns an instance with data parsed and available' do
19
+ filename = 'valid_file.txt'
20
+ io = StringIO.new(<<-END.gsub(/^ */,''))
21
+ ----------------------------------------
22
+ | Foo | Bar | Baz |
23
+ ----------------------------------------
24
+ | A1 | B1 | C1 |
25
+ ----------------------------------------
26
+ | A2 | B2 | C2 |
27
+ ----------------------------------------
28
+ END
29
+ File.should_receive(:open).and_return(io)
30
+ m = Mafti.open(@valid_file)
31
+ m.data.should ==([
32
+ ['A1' ,'B1' ,'C1' ],
33
+ ['A2' ,'B2' ,'C2' ]
34
+ ])
35
+ m.headers.should ==(['Foo','Bar','Baz'])
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#as_hashes' do
41
+ it 'returns data as an array of hashes' do
42
+ m = Mafti.new
43
+ m.headers = ['a','b','c']
44
+ m.data = [ ['1','2','3'], ['4','5','6'], ]
45
+ hashes = m.as_hashes
46
+ hashes.should ==([
47
+ {'a' => '1', 'b' => '2', 'c' => '3'},
48
+ {'a' => '4', 'b' => '5', 'c' => '6'},
49
+ ])
50
+ end
51
+
52
+ # whoops, regression test for something stupid i did
53
+ it 'does not delete data' do
54
+ m = Mafti.new
55
+ m.headers = ['a','b','c']
56
+ m.data = [ ['1','2','3'], ['4','5','6'] ]
57
+ data_before = m.data
58
+ first_hashes = m.as_hashes
59
+ second_hashes = m.as_hashes
60
+ data_after = m.data
61
+
62
+ second_hashes.should ==(first_hashes)
63
+ second_hashes.should_not equal(first_hashes)
64
+ data_before.should ==(data_after)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format specdoc
3
+ --backtrace
@@ -0,0 +1 @@
1
+ require 'spec'
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mafti
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Hinze
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-06 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Get the stupid 'formatted text' output from Microsoft Access imported to something sane
17
+ email: paul.t.hinze@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - .autotest
26
+ - .gitignore
27
+ - README
28
+ - Rakefile
29
+ - VERSION
30
+ - lib/mafti.rb
31
+ - mafti.gemspec
32
+ - spec/lib/mafti_spec.rb
33
+ - spec/spec.opts
34
+ - spec/spec_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/phinze/mafti
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Microsoft Access Formatted Text Importer
63
+ test_files:
64
+ - spec/lib/mafti_spec.rb
65
+ - spec/spec_helper.rb