pixxer 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 +0 -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
File without changes
|
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: pixxer
|
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: &10727916 !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: *10727916
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-core
|
29
|
+
requirement: &10727640 !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: *10727640
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec-expectations
|
40
|
+
requirement: &10727364 !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: *10727364
|
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: []
|