Class: Rocknab::Builder
- Inherits:
-
Object
- Object
- Rocknab::Builder
- Defined in:
- lib/rocknab/builder.rb
Overview
This is the DSL that is used to declare CNAB registers
Declaration:
class MyRegister < Rocknab::Builder
text :txt_field, length: 4
number :num_field, length: 2
end
To use the class:
register = MyRegister.new(txt_field: "text", num_field: 10)
register.build
=> "TEXT10"
Direct Known Subclasses
Layouts::CNAB240::BatchHeader, Layouts::CNAB240::BatchTrailer, Layouts::CNAB240::Header, Layouts::CNAB240::Segment, Layouts::CNAB240::Trailer
Class Method Summary collapse
-
.date(name, length: 14, default: 0, padding: 0) ⇒ Object
Declares that the generated content has a date field.
- .inherited(child) ⇒ Object
-
.length ⇒ Object
Returns the total length of the fields.
-
.number(name, length: 1, default: 0, padding: 0) ⇒ Object
Declares that the generated content has a numeric field.
-
.padding(length: 1, char: " ") ⇒ Object
Declares that the generated content has a padding.
-
.text(name, length: 1, default: " ", padding: " ") ⇒ Object
Declares that the generated content has a text field.
Instance Method Summary collapse
-
#build ⇒ Object
Builds the class according to the definition.
-
#initialize(**args) ⇒ Builder
constructor
Subclasses can be initialized with the arguments passed as a hash, just like a Struct.
Constructor Details
#initialize(**args) ⇒ Builder
Subclasses can be initialized with the arguments passed as a hash, just like a Struct.
Usage:
MyRegister.new(txt_field: "text", num_field: 10)
28 29 30 31 32 |
# File 'lib/rocknab/builder.rb', line 28 def initialize(**args) args.each do |key, value| instance_variable_set("@#{key}", value) end end |
Class Method Details
.date(name, length: 14, default: 0, padding: 0) ⇒ Object
Declares that the generated content has a date field. The format is DDMMYYYYhhmmss.
Parameters:
- name: Name of the field accessors
- length: Fixed length of the field. Default is 14.
- default: Default value. Zeroes if not specified.
- padding: Padding character. Zeros, if not specified.
Tip: Use 10 as the length, if you only need the date.
Usage:
class MyRegister < Rocknab::Builder
date :created_at, length: 14, default: "hello", padding: " "
end
99 100 101 102 |
# File 'lib/rocknab/builder.rb', line 99 def self.date(name, length: 14, default: 0, padding: 0) << Builders::Date.new(name, length, default, padding) attr_accessor(name) end |
.inherited(child) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/rocknab/builder.rb', line 129 def self.inherited(child) child.instance_eval do class << self attr_accessor :metadata end end child. = [] end |
.length ⇒ Object
Returns the total length of the fields. Useful for validation purposes and testing.
Usage:
MyRegister.length
=> 240
125 126 127 |
# File 'lib/rocknab/builder.rb', line 125 def self.length .map(&:length).reduce(&:+) end |
.number(name, length: 1, default: 0, padding: 0) ⇒ Object
Declares that the generated content has a numeric field. This field should only be used for integers.
Parameters:
- name: Name of the field accessors
- length: Fixed length of the field
- default: Default value. Zero if not specified.
- padding: Padding character. Zeros, if not specified.
Usage:
class MyRegister < Rocknab::Builder
number :my_num, length: 20, default: 1234, padding: 0
end
78 79 80 81 |
# File 'lib/rocknab/builder.rb', line 78 def self.number(name, length: 1, default: 0, padding: 0) << Builders::Number.new(name, length, default, padding) attr_accessor(name) end |
.padding(length: 1, char: " ") ⇒ Object
Declares that the generated content has a padding
Parameters:
- length: Fixed length of the field
- char: Character that will be used to pad the content.
The default is whitespace.
Usage:
padding length: 10, char: "0"
114 115 116 |
# File 'lib/rocknab/builder.rb', line 114 def self.padding(length: 1, char: " ") << Builders::Padding.new(length, char) end |
.text(name, length: 1, default: " ", padding: " ") ⇒ Object
Declares that the generated content has a text field
Parameters:
- name: Name of the field accessors
- length: Fixed length of the field
- default: Default value. Empty if not specified.
- padding: Padding character. Whitespace, if not specified.
Usage:
class MyRegister < Rocknab::Builder
text :my_field, length: 20, default: "hello", padding: " "
end
59 60 61 62 |
# File 'lib/rocknab/builder.rb', line 59 def self.text(name, length: 1, default: " ", padding: " ") << Builders::Text.new(name, length, default, padding) attr_accessor(name) end |
Instance Method Details
#build ⇒ Object
Builds the class according to the definition
Usage:
register.build
=> "TEXT10"
40 41 42 43 44 |
# File 'lib/rocknab/builder.rb', line 40 def build self.class..map do |field| field.build(self) end.join end |