parseline 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ #shairon.toledo@gmail.com
2
+ #http://www.hashcode.eti.br
3
+ module ParseLine
4
+ require 'parseline/parser_line_csv'
5
+ require 'parseline/parser_line_fixed_width'
6
+ end
7
+
@@ -0,0 +1,58 @@
1
+ #shairon.toledo@gmail.com
2
+ #http://www.hashcode.eti.br
3
+ module ParseLine::CSV
4
+
5
+ self.class_eval do
6
+
7
+ def csv_layout(options={:delimiter => ';'},&block)
8
+ self.send(:class_variable_set,:@@parse_delimiter,Regexp.new(options[:delimiter])) unless self.class_variable_defined?(:@@parse_values)
9
+ yield self
10
+ end
11
+
12
+ def parse_field(field,block=nil)
13
+ self.send(:class_variable_set,:@@parse_values,[]) unless self.class_variable_defined?(:@@parse_values)
14
+ self.send(:class_variable_get,:@@parse_values) << [field,block]
15
+ end
16
+
17
+ def field(field,block=nil)
18
+ parse_field(field,block)
19
+ end
20
+ def ignore_field
21
+
22
+ parse_field(:__ignored__)
23
+ end
24
+
25
+ def load_lines(filepath)
26
+
27
+ File.open(filepath).map{|line| load_line(line)}
28
+
29
+ end
30
+ def load_line(line)
31
+ begin
32
+ this=self.new
33
+ #puts this.inspect
34
+ bits_line=line.to_s.split(self.send(:class_variable_get,:@@parse_delimiter)).flatten
35
+
36
+
37
+ self.send(:class_variable_get,:@@parse_values).each_with_index do |field,i|
38
+ if field[0] != :__ignored__
39
+ if field[1] # it's block
40
+ this.send "#{field[0]}=", field[1].call(bits_line[i])
41
+ else
42
+ this.send "#{field[0]}=", bits_line[i].to_s.strip
43
+ end
44
+ else
45
+ end
46
+
47
+ end
48
+ #puts this.inspect
49
+ this
50
+ rescue => e
51
+ raise "[#{e}]ParseLine::MalformedLayoutOrLine: '#{line.to_s.strip}', size: #{line.to_s.size}"
52
+ end
53
+ #puts bits_line
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,44 @@
1
+ #shairon.toledo@gmail.com
2
+ #http://www.hashcode.eti.br
3
+ module ParseLine::FixedWidth
4
+
5
+ self.class_eval do
6
+
7
+ def fixed_width_layout(&block)
8
+ yield self
9
+ end
10
+
11
+ def parse_field(field,range,block=nil)
12
+ self.send(:class_variable_set,:@@parse_values,[]) unless self.class_variable_defined?(:@@parse_values)
13
+ self.send(:class_variable_get,:@@parse_values) << [field,range,block]
14
+ end
15
+
16
+ def field(field,range,block=nil)
17
+ parse_field(field,range,block)
18
+ end
19
+
20
+ def load_lines(filepath)
21
+
22
+ File.open(filepath).map{|line| load_line(line)}
23
+
24
+ end
25
+ def load_line(line)
26
+ this=self.new
27
+ begin
28
+ self.send(:class_variable_get,:@@parse_values).each do |params|
29
+ if params[2] # it's block
30
+ this.send "#{params[0]}=", params[2].call(line[params[1]])
31
+ else
32
+ this.send "#{params[0]}=", line[params[1]].to_s.strip
33
+ end
34
+ end
35
+ this
36
+ rescue
37
+ raise "ParseLine::MalformedLayoutOrLine: '#{line.to_s.strip}', size: #{line.to_s.size}"
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parseline
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Shairon Toledo
8
+ autorequire: parseline
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-14 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This project aims to helps to load data from fixed width files and csv files to ActiveRecord, easier and faster.
17
+ email: shairon.toledo@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/parseline
26
+ - lib/parseline/parser_line_csv.rb
27
+ - lib/parseline/parser_line_fixed_width.rb
28
+ - lib/parseline.rb
29
+ has_rdoc: false
30
+ homepage: http://parseline.rubyforge.org/
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project: parseline
51
+ rubygems_version: 1.3.1
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: This project aims to helps to load data from fixed width files and csv files to ActiveRecord, easier and faster.
55
+ test_files: []
56
+