Class: RBEDI::Segment
- Inherits:
-
Object
- Object
- RBEDI::Segment
- Defined in:
- lib/rbedi/segment.rb
Instance Attribute Summary collapse
-
#segment_elements ⇒ Object
Returns the value of attribute segment_elements.
-
#separator ⇒ Object
Returns the value of attribute separator.
Class Method Summary collapse
Instance Method Summary collapse
- #[](element) ⇒ Object
- #[]=(element, value) ⇒ Object
- #get_raw_element(n) ⇒ Object
-
#initialize(segment_name, segment_elements = {}) ⇒ Segment
constructor
A new instance of Segment.
- #raw_segment_name ⇒ Object
- #segment_type ⇒ Object
- #set_raw_element(n, v) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(segment_name, segment_elements = {}) ⇒ Segment
Returns a new instance of Segment.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rbedi/segment.rb', line 3 def initialize(segment_name, segment_elements={}) @segment_name = get_raw_segment_name(segment_name) @separator = segment_elements.delete(:_separator) begin @codes_class = Codes.const_get(@segment_name) rescue NameError @codes_class = nil end @segment_elements = [] segment_elements.each do |k, v| self[k] = v end end |
Instance Attribute Details
#segment_elements ⇒ Object
Returns the value of attribute segment_elements.
100 101 102 |
# File 'lib/rbedi/segment.rb', line 100 def segment_elements @segment_elements end |
#separator ⇒ Object
Returns the value of attribute separator.
100 101 102 |
# File 'lib/rbedi/segment.rb', line 100 def separator @separator end |
Class Method Details
.parse(segment, separator: nil) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/rbedi/segment.rb', line 90 def self.parse(segment, separator:nil) elements = segment.delete_suffix("~").split(Codes::ELEMENT_SEPARATOR) segment = new(elements[0]) elements[1..-1].each_with_index do |e, i| segment.set_raw_element(i+1, e) end segment.separator = separator return segment end |
Instance Method Details
#[](element) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rbedi/segment.rb', line 23 def [](element) return get_raw_element(element) unless @codes_class element_pos = get_element_pos(element) raise NonExistentElementError, "Element `#{element}' does not exist for segment `#{raw_segment_name}'" unless element_pos code = @codes_class.code(element_pos, get_raw_element(element_pos)) raw_element = get_raw_element(element_pos) return nil unless raw_element if @separator raw_element_arr = raw_element.split(@separator) raw_element_arr.map! do |i| c = @codes_class.code(element_pos, i) c.nil? ? i : c end return raw_element_arr unless raw_element_arr.length == 1 end code.nil? ? raw_element : code end |
#[]=(element, value) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rbedi/segment.rb', line 49 def []=(element, value) return set_raw_element(element, (value.is_a?(Array) && @separator) ? value.join(@separator) : value.to_s) unless @codes_class element_pos = get_element_pos(element) raise NonExistentElementError, "Element `#{element}' does not exist for segment `#{raw_segment_name}'" unless element_pos if @separator && value.is_a?(Array) value.map! do |i| if i.is_a?(String) i else c = @codes_class.code(element_pos, i) c.nil? ? i : c end end set_raw_element(element_pos, value.join(@separator)) else return set_raw_element(element_pos, value) if value.is_a?(String) code = @codes_class.code(element_pos, value) set_raw_element(element_pos, code.nil? ? value : code) end end |
#get_raw_element(n) ⇒ Object
82 83 84 |
# File 'lib/rbedi/segment.rb', line 82 def get_raw_element(n) @segment_elements[n - 1] end |
#raw_segment_name ⇒ Object
78 79 80 |
# File 'lib/rbedi/segment.rb', line 78 def raw_segment_name @segment_name end |
#segment_type ⇒ Object
74 75 76 |
# File 'lib/rbedi/segment.rb', line 74 def segment_type Codes::SegmentNames.segment_name(raw_segment_name) || raw_segment_name end |
#set_raw_element(n, v) ⇒ Object
86 87 88 |
# File 'lib/rbedi/segment.rb', line 86 def set_raw_element(n, v) @segment_elements[n - 1] = v end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/rbedi/segment.rb', line 19 def to_s ([@segment_name] + @segment_elements).join(Codes::ELEMENT_SEPARATOR) + Codes::SEGMENT_TERMINATOR end |