Class: JCR::JcrParts

Inherits:
Object
  • Object
show all
Defined in:
lib/jcr/parts.rb

Overview

This class extracts parts of a JCR file into multiple files based on comments in the file. It can also create a new file without the comments. This is useful for JCR going into specification documents where it is nice to break the JCR up for illustrative purposes in the specification but to also have one JCR file for programmatic testing purposes.

The file parts are extracted using the comments

; start_part FILENAME

and

; end_part

The comments must also be the only thing present on the line though leading whitespace is allowed if desired.

To get a new file with all parts be these comments, use this

; all_parts FILENAME

Instance Method Summary collapse

Instance Method Details

#get_all(line) ⇒ Object

Determines if the the line is an all_parts comment. Return the file name otherwise nil



49
50
51
52
53
54
55
56
# File 'lib/jcr/parts.rb', line 49

def get_all( line )
  retval = nil
  m = /^\s*;\s*all_parts\s*(.+)[^\s]*/.match( line )
  if m && m[1]
    retval = m[1]
  end
  return retval
end

#get_end(line) ⇒ Object

Determines if the the line is an end_parts comment. Return true otherwise nil



60
61
62
63
64
65
66
67
# File 'lib/jcr/parts.rb', line 60

def get_end( line )
  retval = nil
  m = /^\s*;\s*end_part/.match( line )
  if m
    retval = true
  end
  return retval
end

#get_start(line) ⇒ Object

Determines if the the line is a start_part comment. Return the file name otherwise nil



38
39
40
41
42
43
44
45
# File 'lib/jcr/parts.rb', line 38

def get_start( line )
  retval = nil
  m = /^\s*;\s*start_part\s*(.+)[^\s]*/.match( line )
  if m && m[1]
    retval = m[1]
  end
  return retval
end

#process_ruleset(ruleset, dirname = nil) ⇒ Object

processes the lines ruleset is to be a string read in using File.read



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jcr/parts.rb', line 71

def process_ruleset( ruleset, dirname = nil )
  all_file_names = []
  all_parts = []
  all_parts_name = nil
  current_part = nil
  current_part_name = nil
  ruleset.lines do |line|
    if !all_parts_name && ( all_parts_name = get_all( line ) )
      all_parts_name = File.join( dirname, all_parts_name ) if dirname
      all_file_names << all_parts_name
    elsif ( current_part_name = get_start( line ) )
      current_part_name = File.join( dirname, current_part_name ) if dirname
      if current_part
        current_part.close
      end
      current_part = File.open( current_part_name, "w" )
      all_file_names << current_part_name
    elsif get_end( line ) && current_part
      current_part.close
      current_part = nil
    elsif current_part
      current_part.puts line
      all_parts << line
    else
      all_parts << line
    end
  end
  if current_part
    current_part.close
  end
  if all_parts_name
    f = File.open( all_parts_name, "w" )
    all_parts.each do |line|
      f.puts( line )
    end
    f.close
  end
  if all_file_names.length
    xml_fn = File.basename( all_file_names[0],".*" ) + "_xml_entity_refs"
    xml_fn = File.join( File.dirname( all_file_names[0] ), xml_fn )
    xml = File.open( xml_fn, "w" )
    all_file_names.each do |fn|
      bn = File.basename( fn, ".*" )
      xml.puts( "<!ENTITY #{bn} PUBLIC '' '#{fn}'>")
    end
    xml.close
  end
end