Class: HealthSeven::Message::SegmentDef

Inherits:
Object
  • Object
show all
Defined in:
lib/health_seven/message.rb

Overview

GRAMMAR PART

Direct Known Subclasses

MessageDef

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ SegmentDef

Returns a new instance of SegmentDef.



12
13
14
15
16
# File 'lib/health_seven/message.rb', line 12

def initialize(name, &block)
  @name = name
  @segments = []
  self.instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/health_seven/message.rb', line 39

def method_missing(name, &block)
  unless @dsl_off
    @segments<< SegmentDef.new(name, &block)
  else
    super
  end
end

Instance Method Details

#_children_enumObject



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

def _children_enum
  children = @segments.map do |s|
    s.dsl_off!
    res = s.rule_name
    if s.optional?
      if s.multiple?
        res<< "*"
      else
        res<< "?"
      end
    else
      res<< "+" if s.multiple?
    end
    res
  end
end

#dsl_off!Object



18
19
20
# File 'lib/health_seven/message.rb', line 18

def dsl_off!
  @dsl_off = true
end

#multiple?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/health_seven/message.rb', line 35

def multiple?
  @name[3] == 's'
end

#optional?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/health_seven/message.rb', line 31

def optional?
  @name[-1] == '?'
end

#rule_nameObject



27
28
29
# File 'lib/health_seven/message.rb', line 27

def rule_name
  @name[0..2]
end

#seg_nameObject



22
23
24
25
# File 'lib/health_seven/message.rb', line 22

def seg_name
  return '' if @name == :message
  "'#{@name.to_s[0..2].upcase}'"
end

#to_gramar(gramar) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/health_seven/message.rb', line 47

def to_gramar(gramar)
  gramar.push <<-RULE

 rule #{self.rule_name}
   #{seg_name} payload delim #{_children_enum.join(" ")} <HealthSeven::SegmentLiteral>
 end
 RULE

 @segments.each do |s|
   s.to_gramar(gramar)
 end
 gramar
end