module HL7::Message::SegmentFields::ClassMethods

Public Instance Methods

add_field( name, options={}, &blk ) click to toggle source

define a field alias

  • name is the alias itself (required)

  • options is a hash of parameters

    • :id is the field number to reference (optional, auto-increments from 1

      by default)
    • :blk is a validation proc (optional, overrides the second argument)

  • blk is an optional validation/convertion proc which MUST take a parameter and always return a value for the field (it will be

    used on read/write calls)
# File lib/segment_fields.rb, line 23
    def add_field( name, options={}, &blk )
      options = { :idx =>-1, :blk =>blk}.merge!( options )
      name ||= :id
      namesym = name.to_sym
      @field_cnt ||= 1
      if options[:idx] == -1
        options[:idx] = @field_cnt # provide default auto-incrementing
      end
      @field_cnt = options[:idx].to_i + 1

      singleton.module_eval do
        @fields ||= {}
        @fields[ namesym ] = options
      end

      self.class_eval <<-END
        def #{name}(val=nil)
          unless val
            read_field( :#{namesym} )
          else
            write_field( :#{namesym}, val )
            val # this matches existing n= method functionality
          end
        end

        def #{name}=(value)
          write_field( :#{namesym}, value )
        end
      END
    end
alias_field(new_field_name, old_field_name) click to toggle source
# File lib/segment_fields.rb, line 60
    def alias_field(new_field_name, old_field_name)
      self.class_eval <<-END
        def #{new_field_name}(val=nil)
          raise HL7::InvalidDataError.new unless self.class.fields[:#{old_field_name}]
          unless val
            read_field( :#{old_field_name} )
          else
            write_field( :#{old_field_name}, val )
            val # this matches existing n= method functionality
          end
        end

        def #{new_field_name}=(value)
          write_field( :#{old_field_name}, value )
        end
      END
    end