Module: LIS::Message::ClassMethods

Included in:
Base
Defined in:
lib/lis/messages.rb

Overview

declarative definition of message structure

Constant Summary

CONVERSION_WRITER =
{
  :string => lambda { |s| s },
  :int => lambda { |s| s.to_i },
  :timestamp => lambda { |s| DateTime.strptime(s, "%Y%m%d%H%M%S") rescue nil }
}

Instance Method Summary collapse

Instance Method Details

#default_fieldsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lis/messages.rb', line 21

def default_fields
  arr = Array.new(@field_count)
  (0 .. @field_count).inject(arr) do |a,i|
    default = (get_field_attributes(i) || {})[:default]
    if default
      default = default.call if default.respond_to?(:call)
      a[i-1] = default
    end
    arr
  end
end

#field_count(val = nil) ⇒ Object



16
17
18
19
# File 'lib/lis/messages.rb', line 16

def field_count(val = nil)
  @field_count = val if val
  @field_count
end

#get_field_attributes(index) ⇒ Object



79
80
81
82
83
84
# File 'lib/lis/messages.rb', line 79

def get_field_attributes(index)
  @field_names ||= {}
  val = (@field_names || {})[index]
  val ||= superclass.get_field_attributes(index) if superclass.respond_to?(:get_field_attributes)
  val
end

#get_index_for(field_name) ⇒ Object



73
74
75
76
77
# File 'lib/lis/messages.rb', line 73

def get_index_for(field_name)
  val = @field_index[field_name]
  val ||= superclass.get_index_for(field_name) if superclass.respond_to?(:get_index_for)
  val
end

#has_field(idx, name = nil, opts = {}) ⇒ Object

define a field of a message at a specific index and an optional type



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lis/messages.rb', line 35

def has_field(idx, name = nil, opts={})
  set_index_for(name, idx) if name
  set_field_attributes(idx, { :name => name,
                              :type => opts[:type] || :string,
                              :default => opts[:default]})

  @field_count ||= 0
  @field_count = [@field_count, idx].max

  return unless name

  define_method :#{name}=" do |val|
    @fields ||= {}
    @fields[idx] = val
  end

  define_method :#{name}" do
    field_attrs = self.class.get_field_attributes(idx)
    val = (@fields || {})[idx]
    converter = CONVERSION_WRITER[field_attrs[:type]] if field_attrs
    val = converter.call(val) if converter
    val
  end
end

#set_field_attributes(index, hash) ⇒ Object



86
87
88
89
# File 'lib/lis/messages.rb', line 86

def set_field_attributes(index, hash)
  @field_names ||= {}
  @field_names[index] = hash
end

#set_index_for(field_name, idx) ⇒ Object



68
69
70
71
# File 'lib/lis/messages.rb', line 68

def set_index_for(field_name, idx)
  @field_indices ||= {}
  @field_indices[field_name] = idx
end

#type_id(char = nil) ⇒ Object

registers a message with a specific type prefix to make Base.from_string work



62
63
64
65
66
# File 'lib/lis/messages.rb', line 62

def type_id(char = nil)
  return @@messages_by_type.find {|c,klass| klass == self }.first unless char
  @@messages_by_type ||= {}
  @@messages_by_type[char] = self
end