Module: HL7::Message::SegmentFields::ClassMethods
- Defined in:
- lib/segment_fields.rb
Instance Method Summary collapse
-
#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).
- #alias_field(new_field_name, old_field_name) ⇒ Object
-
#fields ⇒ Object
:nodoc:.
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)
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 48 49 50 51 52 |
# File 'lib/segment_fields.rb', line 23 def add_field( name, ={}, &blk ) = { :idx =>-1, :blk =>blk}.merge!( ) name ||= :id namesym = name.to_sym @field_cnt ||= 1 if [:idx] == -1 [:idx] = @field_cnt # provide default auto-incrementing end @field_cnt = [:idx].to_i + 1 singleton.module_eval do @fields ||= {} @fields[ namesym ] = 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) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# 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 |
#fields ⇒ Object
:nodoc:
54 55 56 57 58 |
# File 'lib/segment_fields.rb', line 54 def fields #:nodoc: singleton.module_eval do (@fields ||= []) end end |