Class: SOCMaker::YPP
- Inherits:
-
Object
- Object
- SOCMaker::YPP
- Defined in:
- lib/soc_maker/ypp.rb
Overview
YAML Pre-Processor: We use it only in a static way, no objects are created (private ctor). There are two methods:
-
SOCMaker::YPP::to_yaml( a_string )
-
SOCMaker::YPP::from_yaml( a_string )
The static method to_yaml replaces our custom tags (like SOCM_CORE) with YAML object identifiers (like ‘— ruby/object::SOCMaker::CoreDef’)
The static method from_yaml does the inverse to to_yaml: it replaces the object identifiers with our custom tags.
See also SOCMaker::Conf YPP_LUT YPP_INV_LUT and YPP_INV_REGEX, these are the lookup tables and regular expressions used for this processing
Goal: This is used to have a nicer YAML file and without the need of writing loong object identifier lines.
Class Method Summary collapse
-
.from_yaml(string) ⇒ Object
This method does the inverse to to_yaml.
-
.split_with_match(string, regex) ⇒ Object
This method is used to split concatenated yaml object strings.
-
.to_yaml(string) ⇒ Object
- This method does the pre-processing and replaces our custom tags with yaml-tags, like — !ruby/object:SOCMaker
-
.…
Class Method Details
.from_yaml(string) ⇒ Object
This method does the inverse to to_yaml.
94 95 96 97 98 99 100 101 102 |
# File 'lib/soc_maker/ypp.rb', line 94 def from_yaml( string ) string.gsub SOCMaker::conf[ :YPP_INV_REGEX ] do |words| # if there is a white-space at the beginning ($1 != nil), we keep # the white-space, if there is no white-space we won't keep it ws = $1.nil? ? "" : " " SOCMaker::conf[ :YPP_INV_LUT ].has_key?( $2 ) ? ws + SOCMaker::conf[ :YPP_INV_LUT ][ $2 ] : words end end |
.split_with_match(string, regex) ⇒ Object
This method is used to split concatenated yaml object strings.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/soc_maker/ypp.rb', line 108 def split_with_match(string, regex) indices = [] strings = [] return [] if string == nil or string.size == 0 string.scan( regex ) { |c| indices << $~.offset(0)[0] } return [] if indices.size == 0 indices = [ indices, indices[ 1..-1].map{ |x| x-1 } << string.size-1 ] indices[0].zip( indices[1] ).each{ |x| strings<< string[ x[0]..x[1] ] } return strings end |
.to_yaml(string) ⇒ Object
This method does the pre-processing and replaces our custom tags with yaml-tags, like
--- !ruby/object:SOCMaker:: ....
In addition, if a block is given, the string is separated into substrings and each sub-string is passed to the block. This allows to process YAML strings, which contain multiple objects. Each object is passed (as yaml string) to the block.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/soc_maker/ypp.rb', line 78 def to_yaml( string ) SOCMaker::conf[ :YPP_LUT ].each do |r, repl| string = string.gsub r, repl end if block_given? strings = split_with_match( string, SOCMaker::conf[ :YPP_SPLIT_REGEX ] ) if string != nil strings.each{ |x| yield( x ) } end return string end |