Module: HL7::Message::SegmentFields::ClassMethods

Defined in:
lib/segment_fields.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, options = {}, &blk) ⇒ Object

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)
    


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/segment_fields.rb', line 25

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

  class_eval <<-END, __FILE__, __LINE__ + 1
    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) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/segment_fields.rb', line 62

def alias_field(new_field_name, old_field_name)
  class_eval <<-END, __FILE__, __LINE__ + 1
    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

#fieldsObject

:nodoc:



56
57
58
59
60
# File 'lib/segment_fields.rb', line 56

def fields # :nodoc:
  singleton.module_eval do
    (@fields ||= [])
  end
end