Class: Mappum::WorkdirLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/mappum/xml_transform.rb

Overview

Class supporting loading working directory of the layout:

schema/ - Directory containing xsd files

map/ - directory containing Mappum maps

Instance Method Summary collapse

Constructor Details

#initialize(schema_path = "schema", map_dir = "maps", basedir = nil) ⇒ WorkdirLoader

Returns a new instance of WorkdirLoader.



179
180
181
182
183
184
185
# File 'lib/mappum/xml_transform.rb', line 179

def initialize(schema_path = "schema", map_dir="maps", basedir=nil)
  @schema_path = schema_path
  @basedir = basedir
  @basedir ||= Dir.mktmpdir
  @map_dir = map_dir
  @mapper_scripts = []
end

Instance Method Details

#generate_and_requireObject



186
187
188
189
# File 'lib/mappum/xml_transform.rb', line 186

def generate_and_require
  generate_classes
  require_all
end

#generate_classes(schema_path = nil, module_path = nil) ⇒ Object

Generate classes from xsd files in shema_path (defaults to schema). Files containt in subdirectories are generated to modules where module name is optaind from folder name. Generated classes are saved to basedir (defaults to tmp)



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/mappum/xml_transform.rb', line 198

def generate_classes(schema_path=nil, module_path=nil)
  class_dir = @basedir
  modname = module_path
  unless module_path.nil?
    class_dir = File.join(class_dir, module_path) 
    modname = modname.gsub(File::SEPARATOR, "::")
    modname = modname.gsub(/^[a-z]|\s+[a-z]|\:\:+[a-z]/) { |a| a.upcase }
  end
  
  Dir.mkdir(class_dir) unless File.exist? class_dir
  

  
  schema_path ||= @schema_path 
  
  Dir.foreach(schema_path) do |file_name|
    full_name = File.join(schema_path,file_name)
    #when file is a directory
    #generate classes in module (recursive)
    if File.directory?(full_name) and not file_name == "." and not file_name == ".."
      #make directory for future class files
      module_pth = file_name
      module_pth = File.join(module_path, module_pth) unless module_path.nil?
       
      generate_classes(full_name, module_pth)
    # for XSD files generate classes using XSD2Ruby
    elsif /.*.xsd/ =~ file_name
      run_xsd2ruby(full_name, file_name, module_path, modname)
      @mapper_scripts <<  class_dir + File::SEPARATOR + file_name[0..-5] + "_mapper.rb"
    end
  end
end

#require_allObject



190
191
192
193
# File 'lib/mappum/xml_transform.rb', line 190

def require_all
  require_schemas
  require_maps
end

#require_mapsObject



236
237
238
239
240
241
242
243
# File 'lib/mappum/xml_transform.rb', line 236

def require_maps
  $:.unshift @map_dir
  Dir.foreach(@map_dir) do |file_name|
    if /.*.rb/ =~ file_name
      require File.join(@map_dir, file_name)
    end
  end
end

#require_schemasObject



230
231
232
233
234
235
# File 'lib/mappum/xml_transform.rb', line 230

def require_schemas
  $:.unshift @basedir
  @mapper_scripts.each do |script|
    require script
  end
end