tabular_data 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tabular_data.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 mrodrigues
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # TabularData
2
+
3
+ A library for parsing tabular data.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tabular_data'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tabular_data
18
+
19
+ ## Usage
20
+
21
+
22
+ ```ruby
23
+ class Person
24
+ attr_accessor :name, :age
25
+
26
+ ATTRIBUTES = [:name, :age]
27
+ end
28
+
29
+ reader = TabularData::Reader.new(Person::ATTRIBUTES)
30
+ person = reader.read(Person.new, "Marcos;23")
31
+
32
+ person.name
33
+ => "Marcos"
34
+
35
+ person.age
36
+ => "23"
37
+ ```
38
+
39
+ ### Changing strategies
40
+
41
+ ```ruby
42
+ reader.strategy = :array
43
+ person = reader.read(Person.new, ["Marcos", 23])
44
+
45
+ person.name
46
+ => "Marcos"
47
+
48
+ person.age
49
+ => 23
50
+
51
+ custom_csv = TabularData::Strategies::CSVStrategy.new
52
+ custom_csv.column_delimiter = "|"
53
+ reader.strategy = custom_csv
54
+
55
+ person = reader.read(Person.new, "Marcos|23")
56
+
57
+ person.name
58
+ => "Marcos"
59
+
60
+ person.age
61
+ => "23"
62
+
63
+ reader.strategy = lambda do |context, attributes_to_parse, attributes|
64
+ attributes_to_parse = [attributes[0..3], attributes_to_parse[4..5]]
65
+ attributes.each_with_index do |attribute, i|
66
+ context.send "#{attribute}=", attributes_to_parse[i]
67
+ end
68
+ end
69
+
70
+ person = reader.read(Person.new, "Marc23")
71
+
72
+ person.name
73
+ => "Marc"
74
+
75
+ person.age
76
+ => "23"
77
+ ```
78
+
79
+ ### Useful methods
80
+
81
+ ```ruby
82
+ people = TabularData.parse_csv("people_data.csv", Person::ATTRIBUTES, lambda{ Person.new })
83
+
84
+ people = TabularData.parse_lines("Marcos;23\nFelipe;25", Person::ATTRIBUTES, lambda{ Person.new })
85
+ ```
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module TabularData
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,88 @@
1
+ module TabularData
2
+
3
+ def self.parse_lines(lines, attributes, factory, column_delimiter = ";")
4
+ lines = lines.split("\n") if lines.is_a? String
5
+
6
+ reader = TabularData::Reader.new(attributes)
7
+ reader.strategy = TabularData::Strategies::CSVStrategy.new(column_delimiter)
8
+
9
+ entries = []
10
+ lines.each do |line|
11
+ entries << reader.read(factory.call, line.strip)
12
+ end
13
+ entries
14
+ end
15
+
16
+ def self.parse_csv(path, attributes, factory, column_delimiter = ";")
17
+ parse_lines File.open(path, "r").readlines, attributes, factory, column_delimiter
18
+ end
19
+
20
+ module Strategies
21
+ class ReaderStrategy
22
+
23
+ def call(context, attributes_to_parse, attributes)
24
+ attributes_to_parse = parse_attributes(attributes_to_parse)
25
+ attributes.each_with_index do |attribute, i|
26
+ context.send "#{attribute}=", attributes_to_parse[i]
27
+ end
28
+ end
29
+
30
+ def parse_attributes(attributes)
31
+ raise NotImplementedError.new("Not implemented yet.")
32
+ end
33
+ end
34
+
35
+ class CSVStrategy < ReaderStrategy
36
+
37
+ attr_accessor :column_delimiter
38
+
39
+ def initialize(column_delimiter = ";")
40
+ @column_delimiter = column_delimiter
41
+ end
42
+
43
+ def column_delimiter
44
+ @column_delimiter
45
+ end
46
+
47
+ def parse_attributes(line)
48
+ line.split(column_delimiter)
49
+ end
50
+ end
51
+
52
+ class ArrayStrategy < ReaderStrategy
53
+
54
+ def parse_attributes(attributes)
55
+ attributes
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ class Reader
62
+
63
+ def strategy
64
+ @strategy ||= TabularData::Strategies::CSVStrategy.new
65
+ end
66
+
67
+ def strategy=(str)
68
+ if str.is_a? Symbol
69
+ case str
70
+ when :csv then @strategy = TabularData::Strategies::CSVStrategy.new
71
+ when :array then @strategy = TabularData::Strategies::ArrayStrategy.new
72
+ end
73
+ else
74
+ @strategy = str
75
+ end
76
+ end
77
+
78
+ def initialize(attributes)
79
+ @attributes = attributes
80
+ end
81
+
82
+ def read(entry, attributes_to_parse)
83
+ @strategy.call(entry, attributes_to_parse, @attributes)
84
+ entry
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tabular_data/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["mrodrigues"]
6
+ gem.email = ["mrodrigues.uff@gmail.com"]
7
+ gem.description = %q{A library for parsing tabular data.}
8
+ gem.summary = %q{A library for parsing tabular data.}
9
+ gem.homepage = "https://github.com/mrodrigues/TabularData"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "tabular_data"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = TabularData::VERSION
17
+ end
@@ -0,0 +1,80 @@
1
+ $:.unshift File.expand_path("../../lib")
2
+ require 'tabular_data'
3
+ require 'test/unit'
4
+
5
+ class Entry
6
+ attr_accessor :a, :b
7
+ ATTRIBUTES = [:a, :b]
8
+ end
9
+
10
+ class TestEntry < Test::Unit::TestCase
11
+
12
+ def setup
13
+ @reader = TabularData::Reader.new(Entry::ATTRIBUTES)
14
+ @entry = Entry.new
15
+ end
16
+
17
+ def test_csv_strategy
18
+ @reader.strategy = :csv
19
+ @reader.read(@entry, "first;second")
20
+ assert_equal "first", @entry.a
21
+ assert_equal "second", @entry.b
22
+ end
23
+
24
+ def test_csv_strategy_setting_delimiter
25
+ strategy = TabularData::Strategies::CSVStrategy.new
26
+ strategy.column_delimiter = "|"
27
+ @reader.strategy = strategy
28
+
29
+ @reader.read(@entry, "first|second")
30
+ assert_equal "first", @entry.a
31
+ assert_equal "second", @entry.b
32
+ end
33
+
34
+ def test_array_strategy
35
+ @reader.strategy = :array
36
+ @reader.read(@entry, ["first","second"])
37
+ assert_equal "first", @entry.a
38
+ assert_equal "second", @entry.b
39
+ end
40
+
41
+ def test_anonymous_strategy
42
+ @reader.strategy = lambda do |context, attributes_to_parse, attributes|
43
+ attributes_to_parse = [attributes_to_parse[0..2], attributes_to_parse[3..5]]
44
+ attributes.each_with_index do |attribute, i|
45
+ context.send "#{attribute}=", attributes_to_parse[i]
46
+ end
47
+ end
48
+ @reader.read(@entry, "1st2nd")
49
+ assert_equal "1st", @entry.a
50
+ assert_equal "2nd", @entry.b
51
+ end
52
+
53
+ def test_parse_lines
54
+ entries = TabularData.parse_lines("first;second\n1st;2nd",
55
+ Entry::ATTRIBUTES,
56
+ lambda{ Entry.new })
57
+ assert_equal "first", entries[0].a
58
+ assert_equal "second", entries[0].b
59
+ assert_equal "1st", entries[1].a
60
+ assert_equal "2nd", entries[1].b
61
+ end
62
+
63
+ def test_parse_csv
64
+ require 'fileutils'
65
+ FileUtils.mkdir("temp") unless File.exists?("temp")
66
+ File.open("temp/entries.csv", "w") do |f|
67
+ f << "first;second\n1st;2nd"
68
+ end
69
+
70
+ entries = TabularData.parse_csv("temp/entries.csv",
71
+ Entry::ATTRIBUTES,
72
+ lambda { Entry.new })
73
+ assert_equal "first", entries[0].a
74
+ assert_equal "second", entries[0].b
75
+ assert_equal "1st", entries[1].a
76
+ assert_equal "2nd", entries[1].b
77
+
78
+ FileUtils.rm_rf("temp")
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabular_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mrodrigues
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-30 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A library for parsing tabular data.
15
+ email:
16
+ - mrodrigues.uff@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/tabular_data.rb
27
+ - lib/tabular_data/version.rb
28
+ - tabular_data.gemspec
29
+ - test/integration/test_reader.rb
30
+ homepage: https://github.com/mrodrigues/TabularData
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.17
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: A library for parsing tabular data.
54
+ test_files:
55
+ - test/integration/test_reader.rb