jcapote-fieldy 1.0.0

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.
data/README.markdown ADDED
@@ -0,0 +1,74 @@
1
+ # Fieldy
2
+
3
+ Fieldy is a ruby library for reading and writing fixed width records
4
+
5
+
6
+ ## Installation
7
+
8
+ Add the github gem source if you haven't already:
9
+
10
+ gem sources -a http://gems.github.com
11
+
12
+ Install the gem:
13
+
14
+ gem install jcapote-fieldy
15
+
16
+
17
+
18
+ ## Examples
19
+
20
+ ### Writing Records
21
+
22
+ require 'fieldy'
23
+
24
+ class AnotherFile
25
+ include Fieldy::Writer
26
+
27
+ field :first_name, 5
28
+ field :last_name, 5
29
+
30
+ end
31
+
32
+ AnotherFile.write(:first_name => "jimmy", :last_name => "fall")
33
+ #=> "jimmyfall "
34
+
35
+ ### Reading Records
36
+
37
+ require 'fieldy'
38
+
39
+ class ExampleFile
40
+ include Fieldy::Reader
41
+
42
+ field :first_name, 12
43
+ field :last_name, 12
44
+ skip 5
45
+ field :grade, 2
46
+
47
+ end
48
+ a = ExampleFile.read("TESTY MCTESTER 11")
49
+
50
+ a[:first_name] => "TESTY"
51
+
52
+
53
+ ## License and Author
54
+
55
+ Copyright 2009 Julio Capote (jcapote@gmail.com)
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining
58
+ a copy of this software and associated documentation files (the
59
+ "Software"), to deal in the Software without restriction, including
60
+ without limitation the rights to use, copy, modify, merge, publish,
61
+ distribute, sublicense, and/or sell copies of the Software, and to
62
+ permit persons to whom the Software is furnished to do so, subject to
63
+ the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be
66
+ included in all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
71
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
72
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
73
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
74
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 1
4
+ :minor: 0
data/lib/fieldy.rb ADDED
@@ -0,0 +1,4 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'ostruct'
3
+ require 'reader'
4
+ require 'writer'
data/lib/reader.rb ADDED
@@ -0,0 +1,45 @@
1
+ module Fieldy
2
+
3
+ module Reader
4
+
5
+ def self.included(base)
6
+ base.extend(ReaderMethods)
7
+ end
8
+
9
+ module ReaderMethods
10
+
11
+ def read(str)
12
+ res = {}
13
+ unpack_str = ""
14
+ @fields.each do |h|
15
+ h.each do |k,v|
16
+ unpack_str << "#{v[:type]}#{v[:length]}"
17
+ end
18
+ end
19
+ arr = str.unpack(unpack_str)
20
+ count = 0
21
+ @fields.each do |h|
22
+ h.each do |k,v|
23
+ unless k == :null
24
+ res.store(k, arr[count])
25
+ count = count + 1
26
+ end
27
+ end
28
+ end
29
+ res
30
+ end
31
+
32
+ def field(sym, length, type = "A")
33
+ @fields ||= []
34
+ @fields << { sym => { :length => length, :type => type }}
35
+ end
36
+
37
+ def skip(length)
38
+ self.field(:null, length, "x" )
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+
45
+ end
data/lib/writer.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Fieldy
2
+
3
+ module Writer
4
+
5
+ def self.included(base)
6
+ base.extend(WriterMethods)
7
+ end
8
+
9
+
10
+ module WriterMethods
11
+
12
+ def write(hash)
13
+ res = []
14
+ pack_str = ""
15
+ @fields.each do |h|
16
+ h.each do |k,v|
17
+ hash.each do |x,y|
18
+ unless k == :null
19
+ res << y
20
+ end
21
+ end
22
+ pack_str << "#{v[:type]}#{v[:length]}"
23
+ end
24
+ end
25
+ res.pack(pack_str)
26
+ end
27
+
28
+ def field(sym, length, type = "A")
29
+ @fields ||= []
30
+ @fields << { sym => { :length => length, :type => type }}
31
+ end
32
+
33
+ def skip(length)
34
+ self.field(:null, length, "x" )
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,4 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'fieldy'
3
+ require 'test/unit'
4
+
@@ -0,0 +1,21 @@
1
+ require 'test/helper'
2
+
3
+ class ExampleFile
4
+ include Fieldy::Reader
5
+
6
+ field :first_name, 12
7
+ field :last_name, 12
8
+ skip 5
9
+ field :grade, 2
10
+
11
+ end
12
+
13
+
14
+ class TestReader < Test::Unit::TestCase
15
+
16
+ def test_reader
17
+ a = ExampleFile.read("TESTY MCTESTER 11")
18
+ assert_equal a[:first_name], "TESTY"
19
+ end
20
+
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/helper'
2
+
3
+ class AnotherFile
4
+ include Fieldy::Writer
5
+
6
+ field :first_name, 5
7
+ field :last_name, 5
8
+
9
+ end
10
+
11
+ class TestWriter < Test::Unit::TestCase
12
+ def test_writer
13
+ a = AnotherFile.write(:first_name => "jimmy", :last_name => "fall")
14
+ assert_equal 10, a.length
15
+ end
16
+
17
+
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcapote-fieldy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - jcapote
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-17 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Declartive DSL for handling fixed width records, read and write with ease
17
+ email: jcapote@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.markdown
26
+ - VERSION.yml
27
+ - lib/fieldy.rb
28
+ - lib/reader.rb
29
+ - lib/writer.rb
30
+ - test/fixtures
31
+ - test/helper.rb
32
+ - test/test_reader.rb
33
+ - test/test_writer.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/jcapote/fieldy
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --inline-source
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Read and write fixed width record with ease
61
+ test_files: []
62
+