parse_me 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -2
- data/lib/parse_me.rb +7 -6
- data/lib/parse_me/fixed_width_parser.rb +0 -6
- data/lib/parse_me/input_splitting.rb +3 -3
- data/lib/parse_me/input_splitting/common.rb +7 -0
- data/lib/parse_me/input_splitting/fixed.rb +4 -10
- data/lib/parse_me/input_splitting/fixed/base.rb +1 -1
- data/lib/parse_me/input_splitting/var.rb +27 -0
- data/lib/parse_me/input_splitting/var/base.rb +26 -0
- data/lib/parse_me/parsed_object.rb +7 -7
- data/lib/parse_me/transformations.rb +1 -1
- data/lib/parse_me/validations.rb +12 -2
- data/lib/parse_me/var_width_parser.rb +1 -2
- data/lib/parse_me/version.rb +1 -1
- data/spec/fixed_width/input_splitting_spec.rb +1 -8
- data/spec/fixed_width/parser_spec.rb +1 -1
- data/spec/input_splitting/common_spec.rb +16 -0
- data/spec/parsed_object/parsed_object_spec.rb +12 -0
- data/spec/validation/basic_spec.rb +11 -11
- data/spec/var_width/input_splitting_spec.rb +349 -0
- data/spec/var_width/parser_spec.rb +52 -0
- metadata +13 -5
data/README.md
CHANGED
@@ -7,7 +7,6 @@ The `parse_me` gem aims to provide an easy to use plain text parser. Currently o
|
|
7
7
|
last_name: { required: true, length: 50 },
|
8
8
|
age: { numeric: :integer },
|
9
9
|
email: { required: true, email: true},
|
10
|
-
last_seen: { date: "YYYY-MM-DD" }
|
11
10
|
}
|
12
11
|
|
13
12
|
options = {
|
@@ -15,7 +14,7 @@ The `parse_me` gem aims to provide an easy to use plain text parser. Currently o
|
|
15
14
|
field_delimiter: "|"
|
16
15
|
}
|
17
16
|
|
18
|
-
result = ParseMe::VarWidthParser.parse(source_string, rules, options)
|
17
|
+
result = ParseMe::VarWidthParser.new.parse(source_string, rules, options)
|
19
18
|
result.each do |record|
|
20
19
|
record.valid? # => Runs the validations specified in rules, returns true if all pass
|
21
20
|
record.attributes # => Returns a Hash with the parsed attributes, using rules' keys
|
data/lib/parse_me.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
require "parse_me/version"
|
2
|
-
require "parse_me/fixed_width_parser"
|
3
|
-
require "parse_me/var_width_parser"
|
4
|
-
require "parse_me/parsed_object"
|
5
|
-
require "parse_me/transformations"
|
6
|
-
|
7
1
|
module ParseMe
|
2
|
+
autoload :VERSION, "parse_me/version"
|
3
|
+
autoload :FixedWidthParser, "parse_me/fixed_width_parser"
|
4
|
+
autoload :VarWidthParser, "parse_me/var_width_parser"
|
5
|
+
autoload :ParsedObject, "parse_me/parsed_object"
|
6
|
+
autoload :Transformations, "parse_me/transformations"
|
7
|
+
autoload :Validations, "parse_me/validations"
|
8
|
+
autoload :InputSplitting, "parse_me/input_splitting"
|
8
9
|
end
|
@@ -1,11 +1,5 @@
|
|
1
|
-
require 'parse_me/parsed_object'
|
2
|
-
require 'parse_me/input_splitting'
|
3
|
-
require 'parse_me/input_splitting/fixed'
|
4
|
-
require 'parse_me/input_splitting/var'
|
5
|
-
|
6
1
|
module ParseMe
|
7
2
|
class FixedWidthParser
|
8
|
-
extend InputSplitting::Fixed
|
9
3
|
include InputSplitting::Fixed
|
10
4
|
end
|
11
5
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'parse_me/input_splitting/fixed'
|
2
|
-
require 'parse_me/input_splitting/var'
|
3
|
-
|
4
1
|
module ParseMe
|
5
2
|
module InputSplitting
|
3
|
+
autoload :Common, "parse_me/input_splitting/common"
|
4
|
+
autoload :Fixed, "parse_me/input_splitting/fixed"
|
5
|
+
autoload :Var, "parse_me/input_splitting/var"
|
6
6
|
end
|
7
7
|
end
|
@@ -1,9 +1,10 @@
|
|
1
|
-
require 'parse_me/input_splitting/fixed/labels'
|
2
|
-
require 'parse_me/input_splitting/fixed/base'
|
3
|
-
|
4
1
|
module ParseMe
|
5
2
|
module InputSplitting
|
6
3
|
module Fixed
|
4
|
+
|
5
|
+
autoload :Base, "parse_me/input_splitting/fixed/base"
|
6
|
+
autoload :Labels, "parse_me/input_splitting/fixed/labels"
|
7
|
+
|
7
8
|
def self.included(base)
|
8
9
|
base.instance_eval do
|
9
10
|
include Base
|
@@ -11,13 +12,6 @@ module ParseMe
|
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
|
-
def self.extended(base)
|
15
|
-
base.instance_eval do
|
16
|
-
extend Base
|
17
|
-
extend Labels
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
15
|
def parse source, rules, options = {}
|
22
16
|
@options = options
|
23
17
|
@options[:labeled_rows] ||= false
|
@@ -25,7 +25,7 @@ module ParseMe
|
|
25
25
|
|
26
26
|
# Splits the input into lines, uses the file width for this.
|
27
27
|
def split_input
|
28
|
-
@split_input ||= @source.delete(
|
28
|
+
@split_input ||= @source.delete(@options[:row_delimiter].to_s).scan(/.{#{file_width}}/)
|
29
29
|
end
|
30
30
|
|
31
31
|
# Gets the list of attributes specified in @rules. Can optionally retrieve them from a labeled set of rules, the optional label argument should be supplied when the labeled_rows option is set.
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ParseMe
|
2
|
+
module InputSplitting
|
3
|
+
module Var
|
4
|
+
|
5
|
+
autoload :Base, "parse_me/input_splitting/var/base"
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.instance_eval do
|
9
|
+
include Base
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse source, rules, options = {}
|
14
|
+
@options = options
|
15
|
+
@options[:row_delimiter] ||= "\n"
|
16
|
+
@options[:field_delimiter] ||= "|"
|
17
|
+
|
18
|
+
@rules = rules
|
19
|
+
@source = source
|
20
|
+
|
21
|
+
collection.map do |record|
|
22
|
+
ParsedObject.new(record, @rules)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ParseMe
|
2
|
+
module InputSplitting
|
3
|
+
module Var
|
4
|
+
module Base
|
5
|
+
def collection
|
6
|
+
split_input.map do |encoded_record|
|
7
|
+
member(encoded_record)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def member encoded_record
|
12
|
+
decoded_record = encoded_record.split(@options[:field_delimiter])
|
13
|
+
Hash[attributes.zip decoded_record]
|
14
|
+
end
|
15
|
+
|
16
|
+
def split_input
|
17
|
+
@split_input ||= @source.split(@options[:row_delimiter])
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes
|
21
|
+
@rules.keys
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'parse_me/transformations'
|
2
|
-
require 'parse_me/validations'
|
3
|
-
|
4
1
|
module ParseMe
|
5
2
|
class ParsedObject
|
6
3
|
include ParseMe::Validations
|
@@ -27,7 +24,9 @@ module ParseMe
|
|
27
24
|
end
|
28
25
|
|
29
26
|
def validation_rules
|
30
|
-
@validation_rules ||= ParseMe::Validations.public_instance_methods
|
27
|
+
@validation_rules ||= ParseMe::Validations.public_instance_methods.map do |m|
|
28
|
+
m.to_s.gsub("_validation", "").to_sym
|
29
|
+
end
|
31
30
|
end
|
32
31
|
|
33
32
|
def apply_transformations
|
@@ -50,9 +49,10 @@ module ParseMe
|
|
50
49
|
end
|
51
50
|
end
|
52
51
|
|
53
|
-
def validate(attr,
|
54
|
-
|
55
|
-
|
52
|
+
def validate(attr, validation_name, val)
|
53
|
+
validation_method = "#{validation_name}_validation"
|
54
|
+
unless method(validation_method).call(@attributes[attr], val)
|
55
|
+
add_error("#{validation_name} validation failed for '#{attr}' => '#{val}'")
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ParseMe
|
2
2
|
module Transformations
|
3
3
|
def ghost_decimal_scale(input, rule_value)
|
4
|
-
raise Exception("ghost_decimal_scale should be able to
|
4
|
+
raise Exception("ghost_decimal_scale should be able to be converted to int") unless (val = rule_value.to_i)
|
5
5
|
|
6
6
|
input.to_f / (10**val)
|
7
7
|
end
|
data/lib/parse_me/validations.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
1
|
module ParseMe
|
2
2
|
module Validations
|
3
|
-
def
|
3
|
+
def length_validation input, rule_value
|
4
4
|
input.to_s.length <= rule_value.to_i
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
7
|
+
def required_validation input, rule_value
|
8
8
|
(not rule_value) || ((!input.nil?) && (input.to_s.strip.length > 0))
|
9
9
|
end
|
10
|
+
|
11
|
+
def numeric_validation input, rule_value
|
12
|
+
regex_pattern = /^-?\d*\.?\d+$/
|
13
|
+
input === regex_pattern
|
14
|
+
end
|
15
|
+
|
16
|
+
def email_validation input, rule_value
|
17
|
+
regex_pattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
|
18
|
+
input === regex_pattern
|
19
|
+
end
|
10
20
|
end
|
11
21
|
end
|
data/lib/parse_me/version.rb
CHANGED
@@ -17,13 +17,6 @@ describe 'FixedWidthParser' do
|
|
17
17
|
before :each do
|
18
18
|
class Dummy; include ParseMe::InputSplitting::Fixed; end
|
19
19
|
@dummy = Dummy.new
|
20
|
-
|
21
|
-
subject.public_instance_methods.each do |pim|
|
22
|
-
subject.module_eval do
|
23
|
-
module_function pim
|
24
|
-
public pim
|
25
|
-
end
|
26
|
-
end
|
27
20
|
end
|
28
21
|
|
29
22
|
it 'should be defined' do
|
@@ -33,7 +26,7 @@ describe 'FixedWidthParser' do
|
|
33
26
|
describe 'parse' do
|
34
27
|
|
35
28
|
it 'should define the parse method' do
|
36
|
-
subject.
|
29
|
+
subject.public_instance_method(:parse).should be
|
37
30
|
end
|
38
31
|
|
39
32
|
describe 'test case 1' do
|
@@ -15,7 +15,7 @@ describe 'FixedWidthParser' do
|
|
15
15
|
{name: 'Lorem Ipsum', last_name: 'Dolor sit amet'},
|
16
16
|
{name: 'Foo', last_name: 'Bar'}
|
17
17
|
]
|
18
|
-
@result = subject.parse(@source, @rules)
|
18
|
+
@result = subject.new.parse(@source, @rules)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'should return an array' do
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require './spec/spec_helper'
|
2
|
+
require "./lib/parse_me"
|
3
|
+
|
4
|
+
describe 'Fixed' do
|
5
|
+
subject {ParseMe::InputSplitting::Common}
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
class Dummy; include ParseMe::InputSplitting::Common; end
|
9
|
+
@dummy = Dummy.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should be defined' do
|
13
|
+
should be
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -96,6 +96,18 @@ describe 'ParsedObject' do
|
|
96
96
|
@inv = ParseMe::FixedWidthParser.new.parse(@inv_source, @rules)
|
97
97
|
end
|
98
98
|
|
99
|
+
it "should have a valid first name" do
|
100
|
+
@inv[2].first.should eq("John")
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should have a valid second name" do
|
104
|
+
@inv[2].second.should eq("Doe")
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should have an empty third name" do
|
108
|
+
@inv[2].third.should eq("")
|
109
|
+
end
|
110
|
+
|
99
111
|
it "should have it's fourth record invalid" do
|
100
112
|
@inv[2].valid?.should be(false)
|
101
113
|
end
|
@@ -18,7 +18,7 @@ describe 'Validations' do
|
|
18
18
|
|
19
19
|
describe 'length' do
|
20
20
|
it 'should be' do
|
21
|
-
subject.method(:
|
21
|
+
subject.method(:length_validation).should be
|
22
22
|
end
|
23
23
|
|
24
24
|
describe 'proper input' do
|
@@ -27,49 +27,49 @@ describe 'Validations' do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'should be truthy with length 25' do
|
30
|
-
subject.
|
30
|
+
subject.length_validation(@input, 25).should be(true)
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'should be falsy with length 24' do
|
34
|
-
subject.
|
34
|
+
subject.length_validation(@input, 24).should be(false)
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'should be truthy with length 35' do
|
38
|
-
subject.
|
38
|
+
subject.length_validation(@input, 35).should be(true)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
describe 'required' do
|
44
44
|
it 'should be' do
|
45
|
-
subject.method(:
|
45
|
+
subject.method(:required_validation).should be
|
46
46
|
end
|
47
47
|
|
48
48
|
describe 'true' do
|
49
49
|
it 'should be truthy with a regular string' do
|
50
|
-
subject.
|
50
|
+
subject.required_validation('FooBar', true).should be(true)
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'should be falsy with a whitespace only string' do
|
54
|
-
subject.
|
54
|
+
subject.required_validation(' ', true).should be(false)
|
55
55
|
end
|
56
56
|
|
57
57
|
it 'should be falsy with an empty string' do
|
58
|
-
subject.
|
58
|
+
subject.required_validation('', true).should be(false)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
describe 'false' do
|
63
63
|
it 'should be truthy with a regular string' do
|
64
|
-
subject.
|
64
|
+
subject.required_validation('FooBar', false).should be(true)
|
65
65
|
end
|
66
66
|
|
67
67
|
it 'should be truthy with a whitespace only string' do
|
68
|
-
subject.
|
68
|
+
subject.required_validation(' ', false).should be(true)
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'should be truthy with an empty string' do
|
72
|
-
subject.
|
72
|
+
subject.required_validation('', false).should be(true)
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -0,0 +1,349 @@
|
|
1
|
+
require './spec/spec_helper'
|
2
|
+
require "./lib/parse_me"
|
3
|
+
|
4
|
+
describe "VarWidthParser" do
|
5
|
+
subject {ParseMe::VarWidthParser}
|
6
|
+
|
7
|
+
describe "InputSplitting" do
|
8
|
+
subject {ParseMe::InputSplitting}
|
9
|
+
|
10
|
+
it "should be defined" do
|
11
|
+
should be
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "Var" do
|
15
|
+
subject {ParseMe::InputSplitting::Var}
|
16
|
+
|
17
|
+
before :each do
|
18
|
+
class Dummy; include ParseMe::InputSplitting::Var; end
|
19
|
+
@dummy = Dummy.new
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be defined" do
|
23
|
+
should be
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "attributes" do
|
27
|
+
|
28
|
+
it "should be defined" do
|
29
|
+
@dummy.method(:attributes).should be
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return the attributes collection" do
|
33
|
+
@dummy.instance_eval do
|
34
|
+
@rules = {name: {}, last_name: {}, age: {}, power: {}}
|
35
|
+
end
|
36
|
+
@dummy.attributes.should eq([:name, :last_name, :age, :power])
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "split_input" do
|
42
|
+
|
43
|
+
it "should be defined" do
|
44
|
+
@dummy.method(:split_input).should be
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should split in break line characters" do
|
48
|
+
@dummy.instance_eval do
|
49
|
+
@source = "First Line\nSecond Line\nThird Line"
|
50
|
+
@options = {row_delimiter: "\n"}
|
51
|
+
end
|
52
|
+
@dummy.split_input.should eq(["First Line", "Second Line", "Third Line"])
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should split in NL character combinations" do
|
56
|
+
@dummy.instance_eval do
|
57
|
+
@source = "First LineNLSecond LineNLThird Line"
|
58
|
+
@options = {row_delimiter: "NL"}
|
59
|
+
end
|
60
|
+
@dummy.split_input.should eq(["First Line", "Second Line", "Third Line"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "parse" do
|
65
|
+
|
66
|
+
it "should define the parse method" do
|
67
|
+
subject.public_instance_method(:parse).should be
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "test case 1" do
|
71
|
+
before :each do
|
72
|
+
@source = "Lorem Ipsum|Dolor sit amet\nFoo|Bar"
|
73
|
+
@rules = {
|
74
|
+
name: {required: true, length: 15},
|
75
|
+
last_name: {required: true, length: 20}
|
76
|
+
}
|
77
|
+
test_rules = @rules
|
78
|
+
test_source = @source
|
79
|
+
@dummy.instance_eval do
|
80
|
+
@source = test_source
|
81
|
+
@rules = test_rules
|
82
|
+
@options = {row_delimiter: "\n", field_delimiter: "|"}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should properly split the input" do
|
87
|
+
@dummy.split_input.should eq([
|
88
|
+
"Lorem Ipsum|Dolor sit amet",
|
89
|
+
"Foo|Bar"
|
90
|
+
])
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should parse valid attributes" do
|
94
|
+
@dummy.attributes.should eq([:name, :last_name])
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "member" do
|
98
|
+
it "should create a valid attributes hash for first string" do
|
99
|
+
@dummy.member(@dummy.split_input[0]).should eq({
|
100
|
+
name: "Lorem Ipsum",
|
101
|
+
last_name: "Dolor sit amet"
|
102
|
+
})
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should create a valid attributes hash for second string" do
|
106
|
+
@dummy.member(@dummy.split_input[1]).should eq({
|
107
|
+
name: "Foo",
|
108
|
+
last_name: "Bar"
|
109
|
+
})
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "collection" do
|
114
|
+
it "should map a valid collection of members" do
|
115
|
+
@dummy.collection.should eq([
|
116
|
+
{
|
117
|
+
name: "Lorem Ipsum",
|
118
|
+
last_name: "Dolor sit amet"
|
119
|
+
},
|
120
|
+
{
|
121
|
+
name: "Foo",
|
122
|
+
last_name: "Bar"
|
123
|
+
}
|
124
|
+
])
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
#describe 'test case 2 (labeled rows)' do
|
130
|
+
#before :each do
|
131
|
+
#@source = '0Lorem Ipsum Dolor sit amet '
|
132
|
+
#@source += '1Foo Bar Baz '
|
133
|
+
#@rules = {
|
134
|
+
#"0" => {
|
135
|
+
#name: {required: true, length: 15},
|
136
|
+
#last_name: {required: true, length: 20}
|
137
|
+
#},
|
138
|
+
#"1" => {
|
139
|
+
#first: {required: true, length: 15},
|
140
|
+
#second: {required: true, length: 10},
|
141
|
+
#third: {required: true, length: 10}
|
142
|
+
#}
|
143
|
+
#}
|
144
|
+
#test_rules = @rules
|
145
|
+
#test_source = @source
|
146
|
+
#@dummy.instance_eval do
|
147
|
+
#@source = test_source
|
148
|
+
#@rules = test_rules
|
149
|
+
#@options = {labeled_rows: true}
|
150
|
+
#end
|
151
|
+
#end
|
152
|
+
|
153
|
+
#it 'should have a 72 characters long source' do
|
154
|
+
#@source.length.should eq(72)
|
155
|
+
#end
|
156
|
+
|
157
|
+
#it 'should properly get length rules' do
|
158
|
+
#@dummy.get_length_rules.should eq([15,20,15,10,10])
|
159
|
+
#end
|
160
|
+
|
161
|
+
#it 'should acknowledge valid length rules' do
|
162
|
+
#@dummy.valid_length_rules?.should be
|
163
|
+
#end
|
164
|
+
|
165
|
+
#it 'should have labeled rows' do
|
166
|
+
#@dummy.labeled_rows?.should be
|
167
|
+
#end
|
168
|
+
|
169
|
+
#it 'should have labels 0 and 1' do
|
170
|
+
#@dummy.labels.should eq(['0','1'])
|
171
|
+
#end
|
172
|
+
|
173
|
+
#it 'should have label_lengths equal to 1 and 1' do
|
174
|
+
#@dummy.label_lengths.should eq([1,1])
|
175
|
+
#end
|
176
|
+
|
177
|
+
#it 'should have label_length equal to 1' do
|
178
|
+
#@dummy.label_length.should eq(1)
|
179
|
+
#end
|
180
|
+
|
181
|
+
#it 'should have a width of 36' do
|
182
|
+
#@dummy.file_width.should eq(36)
|
183
|
+
#end
|
184
|
+
|
185
|
+
#it 'should properly split the input' do
|
186
|
+
#@dummy.split_input.should eq([
|
187
|
+
#'0Lorem Ipsum Dolor sit amet ',
|
188
|
+
#'1Foo Bar Baz '
|
189
|
+
#])
|
190
|
+
#end
|
191
|
+
|
192
|
+
#it 'should create a proper unpack pattern for label "0"' do
|
193
|
+
#@dummy.unpack_pattern('0').should eq('A1A15A20')
|
194
|
+
#end
|
195
|
+
|
196
|
+
#it 'should create a proper unpack pattern for label "1"' do
|
197
|
+
#@dummy.unpack_pattern('1').should eq('A1A15A10A10')
|
198
|
+
#end
|
199
|
+
|
200
|
+
#describe 'label_for' do
|
201
|
+
#it 'should fetch the label of the first string' do
|
202
|
+
#@dummy.label_for(@dummy.split_input[0]).should eq('0')
|
203
|
+
#end
|
204
|
+
|
205
|
+
#it 'should fetch the label of the second string' do
|
206
|
+
#@dummy.label_for(@dummy.split_input[1]).should eq('1')
|
207
|
+
#end
|
208
|
+
#end
|
209
|
+
|
210
|
+
#describe 'labeled_member' do
|
211
|
+
#it 'should create a proper attributes hash for first string' do
|
212
|
+
#str = @dummy.split_input[0]
|
213
|
+
#label = @dummy.label_for str
|
214
|
+
#@dummy.labeled_member(str, label).should eq({
|
215
|
+
#name: 'Lorem Ipsum',
|
216
|
+
#last_name: 'Dolor sit amet',
|
217
|
+
#__label: '0'
|
218
|
+
#})
|
219
|
+
#end
|
220
|
+
|
221
|
+
#it 'should create a proper attributes hash for second string' do
|
222
|
+
#str = @dummy.split_input[1]
|
223
|
+
#label = @dummy.label_for str
|
224
|
+
#@dummy.labeled_member(str, label).should eq({
|
225
|
+
#first: 'Foo',
|
226
|
+
#second: 'Bar',
|
227
|
+
#third: 'Baz',
|
228
|
+
#__label: '1'
|
229
|
+
#})
|
230
|
+
#end
|
231
|
+
#end
|
232
|
+
|
233
|
+
#describe 'attributes' do
|
234
|
+
#it 'should parse valid attributes for first label' do
|
235
|
+
#@dummy.attributes('0').should eq([:name, :last_name])
|
236
|
+
#end
|
237
|
+
|
238
|
+
#it 'should parse valid attributes for second label' do
|
239
|
+
#@dummy.attributes('1').should eq([:first, :second, :third])
|
240
|
+
#end
|
241
|
+
#end
|
242
|
+
|
243
|
+
#describe 'collection' do
|
244
|
+
#it 'should map a valid collection of members' do
|
245
|
+
#@dummy.collection.should eq([
|
246
|
+
#{
|
247
|
+
#name: 'Lorem Ipsum',
|
248
|
+
#last_name: 'Dolor sit amet',
|
249
|
+
#__label: '0'
|
250
|
+
#},
|
251
|
+
#{
|
252
|
+
#first: 'Foo',
|
253
|
+
#second: 'Bar',
|
254
|
+
#third: 'Baz',
|
255
|
+
#__label: '1'
|
256
|
+
#}
|
257
|
+
#])
|
258
|
+
#end
|
259
|
+
#end
|
260
|
+
|
261
|
+
#end
|
262
|
+
|
263
|
+
#describe 'test case 4' do
|
264
|
+
#before :each do
|
265
|
+
#@source = "Lorem Ipsum Dolor sit amet \nFoo Bar \n"
|
266
|
+
#@rules = {
|
267
|
+
#name: {required: true, length: 15},
|
268
|
+
#last_name: {required: true, length: 20}
|
269
|
+
#}
|
270
|
+
#test_rules = @rules
|
271
|
+
#test_source = @source
|
272
|
+
#@dummy.instance_eval do
|
273
|
+
#@source = test_source
|
274
|
+
#@rules = test_rules
|
275
|
+
#@options = {}
|
276
|
+
#end
|
277
|
+
#end
|
278
|
+
|
279
|
+
#it 'should have a 72 characters long source' do
|
280
|
+
#@source.length.should eq(72)
|
281
|
+
#end
|
282
|
+
|
283
|
+
#it 'should properly get length rules' do
|
284
|
+
#@dummy.get_length_rules.should eq([15,20])
|
285
|
+
#end
|
286
|
+
|
287
|
+
#it 'should acknowledge valid length rules' do
|
288
|
+
#@dummy.valid_length_rules?.should be_true
|
289
|
+
#end
|
290
|
+
|
291
|
+
#it 'should have NO labeled rows' do
|
292
|
+
#@dummy.labeled_rows?.should be_false
|
293
|
+
#end
|
294
|
+
|
295
|
+
#it 'should have a width of 35' do
|
296
|
+
#@dummy.file_width.should eq(35)
|
297
|
+
#end
|
298
|
+
|
299
|
+
#it 'should properly split the input' do
|
300
|
+
#@dummy.split_input.should eq([
|
301
|
+
#'Lorem Ipsum Dolor sit amet ',
|
302
|
+
#'Foo Bar '
|
303
|
+
#])
|
304
|
+
#end
|
305
|
+
|
306
|
+
#it 'should create a proper unpack pattern' do
|
307
|
+
#@dummy.unpack_pattern.should eq('A15A20')
|
308
|
+
#end
|
309
|
+
|
310
|
+
#it 'should parse valid attributes' do
|
311
|
+
#@dummy.attributes.should eq([:name, :last_name])
|
312
|
+
#end
|
313
|
+
|
314
|
+
#describe 'member' do
|
315
|
+
#it 'should create a valid attributes hash for first string' do
|
316
|
+
#@dummy.member(@dummy.split_input[0]).should eq({
|
317
|
+
#name: 'Lorem Ipsum',
|
318
|
+
#last_name: 'Dolor sit amet'
|
319
|
+
#})
|
320
|
+
#end
|
321
|
+
|
322
|
+
#it 'should create a valid attributes hash for second string' do
|
323
|
+
#@dummy.member(@dummy.split_input[1]).should eq({
|
324
|
+
#name: 'Foo',
|
325
|
+
#last_name: 'Bar'
|
326
|
+
#})
|
327
|
+
#end
|
328
|
+
#end
|
329
|
+
|
330
|
+
#describe 'collection' do
|
331
|
+
#it 'should map a valid collection of members' do
|
332
|
+
#@dummy.collection.should eq([
|
333
|
+
#{
|
334
|
+
#name: 'Lorem Ipsum',
|
335
|
+
#last_name: 'Dolor sit amet'
|
336
|
+
#},
|
337
|
+
#{
|
338
|
+
#name: 'Foo',
|
339
|
+
#last_name: 'Bar'
|
340
|
+
#}
|
341
|
+
#])
|
342
|
+
#end
|
343
|
+
#end
|
344
|
+
#end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
end
|
349
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "./spec/spec_helper"
|
2
|
+
require "./lib/parse_me"
|
3
|
+
|
4
|
+
describe "VarWidthParser" do
|
5
|
+
subject {ParseMe::VarWidthParser}
|
6
|
+
|
7
|
+
describe 'Test case 1' do
|
8
|
+
before :each do
|
9
|
+
@source = "Lorem Ipsum|Dolor sit amet|23|lorem@dolor.com\n"
|
10
|
+
@source+= "Foo|Bar|34|foo@bar.com"
|
11
|
+
@rules = {
|
12
|
+
name: {required: true, length: 15},
|
13
|
+
last_name: {required: true, length: 20},
|
14
|
+
age: {required: true, length: 3, numeric: true},
|
15
|
+
email: {required: true, email: true}
|
16
|
+
}
|
17
|
+
@attributes = [
|
18
|
+
{
|
19
|
+
name: "Lorem Ipsum",
|
20
|
+
last_name: "Dolor sit amet",
|
21
|
+
age: "23",
|
22
|
+
email: "lorem@dolor.com"
|
23
|
+
},
|
24
|
+
{
|
25
|
+
name: "Foo",
|
26
|
+
last_name: "Bar",
|
27
|
+
age: "34",
|
28
|
+
email: "foo@bar.com"
|
29
|
+
}
|
30
|
+
]
|
31
|
+
@result = subject.new.parse(@source, @rules)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return an array" do
|
35
|
+
@result.class.should eq(Array)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should properly parse attributes for record with index 0" do
|
39
|
+
@result[0].name.should eq(@attributes[0][:name])
|
40
|
+
@result[0].last_name.should eq(@attributes[0][:last_name])
|
41
|
+
@result[0].age.should eq(@attributes[0][:age])
|
42
|
+
@result[0].email.should eq(@attributes[0][:email])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should properly parse attributes for record with index 1" do
|
46
|
+
@result[1].name.should eq(@attributes[1][:name])
|
47
|
+
@result[1].last_name.should eq(@attributes[1][:last_name])
|
48
|
+
@result[1].age.should eq(@attributes[1][:age])
|
49
|
+
@result[1].email.should eq(@attributes[1][:email])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parse_me
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-25 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Plain text file parser
|
15
15
|
email:
|
@@ -28,10 +28,12 @@ files:
|
|
28
28
|
- lib/parse_me.rb
|
29
29
|
- lib/parse_me/fixed_width_parser.rb
|
30
30
|
- lib/parse_me/input_splitting.rb
|
31
|
+
- lib/parse_me/input_splitting/common.rb
|
31
32
|
- lib/parse_me/input_splitting/fixed.rb
|
32
33
|
- lib/parse_me/input_splitting/fixed/base.rb
|
33
34
|
- lib/parse_me/input_splitting/fixed/labels.rb
|
34
35
|
- lib/parse_me/input_splitting/var.rb
|
36
|
+
- lib/parse_me/input_splitting/var/base.rb
|
35
37
|
- lib/parse_me/parsed_object.rb
|
36
38
|
- lib/parse_me/transformations.rb
|
37
39
|
- lib/parse_me/validations.rb
|
@@ -40,6 +42,7 @@ files:
|
|
40
42
|
- parse_me.gemspec
|
41
43
|
- spec/fixed_width/input_splitting_spec.rb
|
42
44
|
- spec/fixed_width/parser_spec.rb
|
45
|
+
- spec/input_splitting/common_spec.rb
|
43
46
|
- spec/namespace_spec.rb
|
44
47
|
- spec/parsed_object/parsed_object_spec.rb
|
45
48
|
- spec/spec_helper.rb
|
@@ -47,6 +50,8 @@ files:
|
|
47
50
|
- spec/validation/basic_spec.rb
|
48
51
|
- spec/validation/date_spec.rb
|
49
52
|
- spec/validation/numeric_spec.rb
|
53
|
+
- spec/var_width/input_splitting_spec.rb
|
54
|
+
- spec/var_width/parser_spec.rb
|
50
55
|
homepage: https://github.com/sergelerator/parse_me
|
51
56
|
licenses: []
|
52
57
|
post_install_message:
|
@@ -61,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
66
|
version: '0'
|
62
67
|
segments:
|
63
68
|
- 0
|
64
|
-
hash:
|
69
|
+
hash: 961821889
|
65
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
71
|
none: false
|
67
72
|
requirements:
|
@@ -70,10 +75,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
75
|
version: '0'
|
71
76
|
segments:
|
72
77
|
- 0
|
73
|
-
hash:
|
78
|
+
hash: 961821889
|
74
79
|
requirements: []
|
75
80
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.8.
|
81
|
+
rubygems_version: 1.8.25
|
77
82
|
signing_key:
|
78
83
|
specification_version: 3
|
79
84
|
summary: Parse your fixed or variable width plain text files using your own custom
|
@@ -81,6 +86,7 @@ summary: Parse your fixed or variable width plain text files using your own cust
|
|
81
86
|
test_files:
|
82
87
|
- spec/fixed_width/input_splitting_spec.rb
|
83
88
|
- spec/fixed_width/parser_spec.rb
|
89
|
+
- spec/input_splitting/common_spec.rb
|
84
90
|
- spec/namespace_spec.rb
|
85
91
|
- spec/parsed_object/parsed_object_spec.rb
|
86
92
|
- spec/spec_helper.rb
|
@@ -88,3 +94,5 @@ test_files:
|
|
88
94
|
- spec/validation/basic_spec.rb
|
89
95
|
- spec/validation/date_spec.rb
|
90
96
|
- spec/validation/numeric_spec.rb
|
97
|
+
- spec/var_width/input_splitting_spec.rb
|
98
|
+
- spec/var_width/parser_spec.rb
|