Class: AfdParser
- Inherits:
-
Object
show all
- Defined in:
- lib/afd_parser.rb,
lib/afd_parser/version.rb
Overview
Parser para a PORTARIA No 1.510, DE 21 DE AGOSTO DE 2009, do Ministério do Trabalho;
Defined Under Namespace
Classes: AfdParserException, ClockInOut, Header, RecordParser, SetEmployee, SetEmployer, SetTime, Trailer
Constant Summary
collapse
- VERSION =
"0.2.1"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ AfdParser
Returns a new instance of AfdParser.
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/afd_parser.rb', line 37
def initialize(*args)
if args.size == 1
initialize_variables(args[0])
elsif args.size == 2
initialize_variables(args[1])
File.open(args[0], "r") do |file|
@raw_data = file.readlines
end
else
raise AfdParser::AfdParserException.new("wrong number of arguments, should be 1 or 2")
end
end
|
Instance Attribute Details
Returns the value of attribute header.
35
36
37
|
# File 'lib/afd_parser.rb', line 35
def
@header
end
|
#records ⇒ Object
Returns the value of attribute records.
35
36
37
|
# File 'lib/afd_parser.rb', line 35
def records
@records
end
|
#trailer ⇒ Object
Returns the value of attribute trailer.
35
36
37
|
# File 'lib/afd_parser.rb', line 35
def trailer
@trailer
end
|
Instance Method Details
#==(other) ⇒ Object
138
139
140
|
# File 'lib/afd_parser.rb', line 138
def ==(other)
return self.class == other.class && @records == other.records
end
|
95
96
97
98
99
100
101
|
# File 'lib/afd_parser.rb', line 95
def (employer_type, employer_document, employer_cei, employer_name, rep_serial_number, afd_start_date,afd_end_date, afd_creation_time)
if
raise AfdParser::AfdParserException.new("Cannot add a second AFD header")
else
@header = Header.new(employer_type, employer_document, employer_cei, employer_name, rep_serial_number, afd_start_date,afd_end_date, afd_creation_time)
end
end
|
#create_trailer ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/afd_parser.rb', line 103
def create_trailer
if trailer_found?
raise AfdParser::AfdParserException.new("Cannot add a second AFD trailer")
else
@trailer = Trailer.new(count_records)
end
end
|
#export ⇒ Object
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/afd_parser.rb', line 111
def export
exported_data = ""
(exported_data += @header.export + "\r\n") if @header
@records.each do |record|
exported_data += record.export + "\r\n"
end
(exported_data += @trailer.export + "\r\n") if @trailer
exported_data
end
|
#first_creation_date ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/afd_parser.rb', line 122
def first_creation_date
record = @records[0]
if record
time = record.creation_time
return Date.civil(time.year, time.month, time.day)
end
end
|
#first_id ⇒ Object
get the first id after the AFD header
143
144
145
146
|
# File 'lib/afd_parser.rb', line 143
def first_id
first_record = @records[0]
first_record ? first_record.line_id : nil
end
|
#last_creation_date ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/afd_parser.rb', line 130
def last_creation_date
record = @records[-1]
if record
time = record.creation_time
return Date.civil(time.year, time.month, time.day)
end
end
|
#last_id ⇒ Object
get the last id before the AFD trailer
149
150
151
152
|
# File 'lib/afd_parser.rb', line 149
def last_id
last_record = @records[-1]
last_record ? last_record.line_id : nil
end
|
#merge(other) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/afd_parser.rb', line 154
def merge(other)
other_first_id, other_last_id = other.first_id, other.last_id
if other_first_id.nil? || other_last_id.nil?
raise AfdParser::AfdParserException.new("Cannot merge with a empty parser")
end
@header = other. if other.
this_records_by_line_id = @records.group_by{|record| record.line_id}
other_records_by_line_id = other.records.group_by{|record| record.line_id}
other_records_by_line_id.keys.each do |key|
this_records_by_line_id[key] = other_records_by_line_id[key].dup
end
@records = this_records_by_line_id.keys.sort.collect{|key| this_records_by_line_id[key]}.flatten
@trailer = nil
create_trailer
end
|
#parse ⇒ Object
50
51
52
53
54
55
56
57
58
|
# File 'lib/afd_parser.rb', line 50
def parse
@raw_data.each_with_index do |line, index|
parse_line(line, index)
end
if @validate_structure and not trailer_found?
raise AfdParser::AfdParserException.new("AFD ended without a trailer record")
end
end
|
#parse_line(line, index) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/afd_parser.rb', line 60
def parse_line(line, index)
line_id, record_type_id = line.unpack("A9A").collect{|id| id.to_i}
record_type = get_record_type(line_id, record_type_id, line)
@previous_line_id = @current_line_id unless @current_line_id == 0
@current_line_id = line_id
if @validate_structure
validate_afd(line, line_id, @previous_line_id, index, record_type)
end
case record_type
when :header
@header = Header.new(line)
return @header
when :set_employer
@records << SetEmployer.new(line)
when :clock_in_out
@records << ClockInOut.new(line)
when :set_time
@records << SetTime.new(line)
when :set_employee
@records << SetEmployee.new(line)
when :trailer
@trailer = Trailer.new(line, count_records)
return @trailer
else
if @validate_structure
raise AfdParser::AfdParserException.new("Unknown record type found in AFD file, line #{index.to_s}: '#{line}'")
end
end
return @records.last
end
|