pixxxer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.md +52 -0
- data/lib/pixxxer.rb +1 -0
- data/lib/pixxxer/field.rb +47 -0
- data/lib/pixxxer/field_depixxxitter.rb +22 -0
- data/lib/pixxxer/field_pixxxitter.rb +45 -0
- data/lib/pixxxer/field_pixxxitter.rb~ +46 -0
- data/lib/pixxxer/pixxxer.rb +19 -0
- data/lib/pixxxer/template.rb +26 -0
- metadata +92 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2011 Guy Royse & Alyssa Diaz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
Pixxxer
|
2
|
+
=======
|
3
|
+
|
4
|
+
Installation
|
5
|
+
------------
|
6
|
+
Installation is super easy. Gems works like that.
|
7
|
+
|
8
|
+
gem install pixxxer
|
9
|
+
|
10
|
+
Usage
|
11
|
+
-----
|
12
|
+
|
13
|
+
Pixxxer provide a simple DSL for defining fixed with data and then extensions to String and Hash to build and parse
|
14
|
+
those records. It can handle floats, integers, and strings.
|
15
|
+
|
16
|
+
Assuming a record like this:
|
17
|
+
|
18
|
+
Title Author Qty Price
|
19
|
+
----- ------ --- -----
|
20
|
+
Dune Herbert 0012001295
|
21
|
+
012345678901234567890123456789
|
22
|
+
1 2
|
23
|
+
|
24
|
+
You can define the follwing template:
|
25
|
+
|
26
|
+
define_pixxx_template(:book)
|
27
|
+
.add_field(:title).as_string.at_position(0).with_width(10).and
|
28
|
+
.add_field(:author).as_string.at_position(10).with_width(10).and
|
29
|
+
.add_field(:quantity).as_integer.at_position(20).with_width(5).and
|
30
|
+
.add_field(:price).as_float.at_position(25).with_width(5).with_precision(2)
|
31
|
+
|
32
|
+
To use the template to parse:
|
33
|
+
|
34
|
+
record = 'Dune Herbert 0012001295'.pixxxit(:book)
|
35
|
+
record[:title].should == 'Dune '
|
36
|
+
record[:author].should == 'Herbert '
|
37
|
+
record[:quantity].should == 120
|
38
|
+
record[:price].should == 12.95
|
39
|
+
|
40
|
+
To use the template to build a record:
|
41
|
+
|
42
|
+
s = record.depixxxit(:book)
|
43
|
+
s.should == 'Dune Herbert 0012001295'
|
44
|
+
|
45
|
+
Note:
|
46
|
+
- Most things default to zero
|
47
|
+
- Fields default to strings
|
48
|
+
- at_position is zero-based
|
49
|
+
|
50
|
+
Copyright
|
51
|
+
---------
|
52
|
+
Copyright(c) 2011 Guy Royse & Alyssa Diaz. See LICENSE for further details.
|
data/lib/pixxxer.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pixxxer/pixxxer'
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'pixxxer/field_pixxxitter'
|
2
|
+
require 'pixxxer/field_depixxxitter'
|
3
|
+
|
4
|
+
class PixxxerField
|
5
|
+
attr_reader :width, :name, :position, :type, :precision
|
6
|
+
def initialize(field_name, template)
|
7
|
+
@template = template
|
8
|
+
@name = field_name
|
9
|
+
@position = 0
|
10
|
+
@precision = 0
|
11
|
+
@pixxxitter = FieldPixxxitter.new self
|
12
|
+
@depixxxitter = FieldDepixxxitter.new self
|
13
|
+
end
|
14
|
+
def with_width(width)
|
15
|
+
@width = width
|
16
|
+
self
|
17
|
+
end
|
18
|
+
def at_position(position)
|
19
|
+
@position = position
|
20
|
+
self
|
21
|
+
end
|
22
|
+
def as_string
|
23
|
+
self
|
24
|
+
end
|
25
|
+
def as_integer
|
26
|
+
@type = Integer
|
27
|
+
self
|
28
|
+
end
|
29
|
+
def as_float
|
30
|
+
@type = Float
|
31
|
+
self
|
32
|
+
end
|
33
|
+
def with_precision(precision)
|
34
|
+
@precision = precision
|
35
|
+
self
|
36
|
+
end
|
37
|
+
def and
|
38
|
+
@template
|
39
|
+
end
|
40
|
+
def pixxxit(hash, record)
|
41
|
+
@pixxxitter.pixxxit hash, record
|
42
|
+
end
|
43
|
+
def depixxxit(record)
|
44
|
+
@depixxxitter.depixxxit record
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class FieldDepixxxitter
|
2
|
+
def initialize(field)
|
3
|
+
@field = field
|
4
|
+
end
|
5
|
+
def depixxxit(record)
|
6
|
+
field = extract_field record
|
7
|
+
coerce_field field
|
8
|
+
end
|
9
|
+
def extract_field(record)
|
10
|
+
return record[@field.position...record.length] if @field.width.nil?
|
11
|
+
record[@field.position, @field.width]
|
12
|
+
end
|
13
|
+
def coerce_field(field)
|
14
|
+
return field.to_i if @field.type == Integer
|
15
|
+
return adjust_float(field.to_f) if @field.type == Float
|
16
|
+
field
|
17
|
+
end
|
18
|
+
def adjust_float(field)
|
19
|
+
field / 10 ** @field.precision
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class FieldPixxxitter
|
2
|
+
def initialize(field)
|
3
|
+
@field = field
|
4
|
+
end
|
5
|
+
def pixxxit(hash, record)
|
6
|
+
field = fetch_field hash
|
7
|
+
add_to_record record, field
|
8
|
+
end
|
9
|
+
def add_to_record(record, field)
|
10
|
+
record = widen_record record
|
11
|
+
inject_field record, field
|
12
|
+
end
|
13
|
+
def widen_record(record)
|
14
|
+
record.ljust @field.position
|
15
|
+
end
|
16
|
+
def inject_field(record, field)
|
17
|
+
record[@field.position, field.length] = field
|
18
|
+
record
|
19
|
+
end
|
20
|
+
def fetch_field(hash)
|
21
|
+
field = coerce_field hash[@field.name].to_s
|
22
|
+
field = pad_field field
|
23
|
+
shorten_field field
|
24
|
+
end
|
25
|
+
def coerce_field(field)
|
26
|
+
field = (field.to_f * 10 ** @field.precision).to_i if @field.type == Float
|
27
|
+
field.to_s
|
28
|
+
end
|
29
|
+
def shorten_field(field)
|
30
|
+
if @field.type == Integer || @field.type == Float
|
31
|
+
return field[field.length - @field.width, @field.width] unless @field.width.nil?
|
32
|
+
else
|
33
|
+
return field[0, @field.width] unless @field.width.nil?
|
34
|
+
end
|
35
|
+
field
|
36
|
+
end
|
37
|
+
def pad_field(field)
|
38
|
+
if @field.type == Integer || @field.type == Float
|
39
|
+
return field.rjust(@field.width, '0') unless @field.width.nil?
|
40
|
+
else
|
41
|
+
return field.ljust(@field.width, ' ') unless @field.width.nil?
|
42
|
+
end
|
43
|
+
field
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class FieldPixxxitter
|
2
|
+
def initialize(field)
|
3
|
+
@field = field
|
4
|
+
end
|
5
|
+
def pixxxit(hash, record)
|
6
|
+
field = fetch_field hash
|
7
|
+
add_to_record record, field
|
8
|
+
end
|
9
|
+
def add_to_record(record, field)
|
10
|
+
record = widen_record record
|
11
|
+
inject_field record, field
|
12
|
+
end
|
13
|
+
def widen_record(record)
|
14
|
+
record.ljust @field.position
|
15
|
+
end
|
16
|
+
def inject_field(record, field)
|
17
|
+
record[@field.position, field.length] = field
|
18
|
+
record
|
19
|
+
end
|
20
|
+
def fetch_field(hash)
|
21
|
+
field = coerce_field hash[@field.name].to_s
|
22
|
+
field = pad_field field
|
23
|
+
field = shorten_field field
|
24
|
+
field
|
25
|
+
end
|
26
|
+
def coerce_field(field)
|
27
|
+
field = (field.to_f * 10 ** @field.precision).to_i if @field.type == Float
|
28
|
+
field.to_s
|
29
|
+
end
|
30
|
+
def shorten_field(field)
|
31
|
+
if @field.type == Integer || @field.type == Float
|
32
|
+
return field[field.length - @field.width, @field.width] unless @field.width.nil?
|
33
|
+
else
|
34
|
+
return field[0, @field.width] unless @field.width.nil?
|
35
|
+
end
|
36
|
+
field
|
37
|
+
end
|
38
|
+
def pad_field(field)
|
39
|
+
if @field.type == Integer || @field.type == Float
|
40
|
+
return field.rjust(@field.width, '0') unless @field.width.nil?
|
41
|
+
else
|
42
|
+
return field.ljust(@field.width, ' ') unless @field.width.nil?
|
43
|
+
end
|
44
|
+
field
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'pixxxer/template'
|
2
|
+
|
3
|
+
$pixxxer_templates = {}
|
4
|
+
|
5
|
+
def define_pixxx_template(template_name)
|
6
|
+
$pixxxer_templates[template_name] = PixxxerTemplate.new
|
7
|
+
end
|
8
|
+
|
9
|
+
class String
|
10
|
+
def depixxxit(template_name)
|
11
|
+
$pixxxer_templates[template_name].depixxxit self
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Hash
|
16
|
+
def pixxxit(template_name)
|
17
|
+
$pixxxer_templates[template_name].pixxxit self
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'pixxxer/field'
|
2
|
+
|
3
|
+
class PixxxerTemplate
|
4
|
+
def initialize
|
5
|
+
@fields = {}
|
6
|
+
end
|
7
|
+
def add_field(field_name)
|
8
|
+
@fields[field_name] = PixxxerField.new(field_name, self)
|
9
|
+
@fields[field_name]
|
10
|
+
end
|
11
|
+
def depixxxit(string)
|
12
|
+
record = {}
|
13
|
+
@fields.each do |field_name, field|
|
14
|
+
record[field_name] = field.depixxxit string
|
15
|
+
end
|
16
|
+
record
|
17
|
+
end
|
18
|
+
def pixxxit(hash)
|
19
|
+
string = ''
|
20
|
+
@fields.each do |field_name, field|
|
21
|
+
string = field.pixxxit hash, string
|
22
|
+
end
|
23
|
+
string
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pixxxer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Guy Royse
|
9
|
+
- Alyssa Diaz
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-05-12 00:00:00.000000000 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
requirement: &10736808 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.5.0
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *10736808
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-core
|
29
|
+
requirement: &10736532 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.5.1
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *10736532
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec-expectations
|
40
|
+
requirement: &10736256 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.5.0
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *10736256
|
49
|
+
description: Simple interface for dealing with the fixed width records that are commong
|
50
|
+
required when working with legacy COBOL systems.
|
51
|
+
email:
|
52
|
+
- guy@guyroyse.com
|
53
|
+
executables: []
|
54
|
+
extensions: []
|
55
|
+
extra_rdoc_files: []
|
56
|
+
files:
|
57
|
+
- lib/pixxxer/field.rb
|
58
|
+
- lib/pixxxer/field_depixxxitter.rb
|
59
|
+
- lib/pixxxer/field_pixxxitter.rb
|
60
|
+
- lib/pixxxer/field_pixxxitter.rb~
|
61
|
+
- lib/pixxxer/pixxxer.rb
|
62
|
+
- lib/pixxxer/template.rb
|
63
|
+
- lib/pixxxer.rb
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/guyroyse/pixxxer
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.5.2
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.5.2
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: DSL for building and parsing fixed with records
|
92
|
+
test_files: []
|