Module: RubyXmlMapper::RubyXmlMapperClassMethods

Defined in:
lib/ruby-xml-mapper.rb

Instance Method Summary collapse

Instance Method Details

#check_file(file_name) ⇒ Object

TODO: URL must pass the check



152
153
154
155
156
157
158
159
160
# File 'lib/ruby-xml-mapper.rb', line 152

def check_file file_name
  unless File.file?(file_name)
    raise "Unable to find file #{file_name.inspect}"
  end

  unless File.readable?(file_name)
    raise "File is not readable #{file_name.inspect}"
  end
end

#new_from_xml_file(file_name) ⇒ Object



162
163
164
165
166
# File 'lib/ruby-xml-mapper.rb', line 162

def new_from_xml_file file_name
  RubyXmlMapper.file_cache.get(file_name) do
    new_from_xml_node(parse_xml_file(file_name))
  end
end

#new_from_xml_node(node) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby-xml-mapper.rb', line 168

def new_from_xml_node node
  if node.attributes["source"]
    unless source_dir_path
      raise "source_dir_path not defined in #{self.class.inspect}"
    end
    new_from_xml_file(File.expand_path_restricted(node.attributes["source"], source_dir_path))
  else        
    allocated = allocate
    allocated.initialize_from_xml node
    allocated.after_initialize if allocated.respond_to?(:after_initialize) 
    allocated
  end
end

#parse_xml_file(file_name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ruby-xml-mapper.rb', line 138

def parse_xml_file(file_name)

  # NOTE: Don't know how to use XML::Document.file correctly.
  # As in libxml-ruby-1.1.3 it does not close file descriptor, so you end up with "Error: Too many open files."
  
  doc = LibXML::XML::Document.string(File.read(file_name))

  # NOTE: If document does not created from file, it leads to "Segmentation fault"
  # doc.xinclude

  doc.root
end

#xml(args) ⇒ Object



182
183
184
185
186
# File 'lib/ruby-xml-mapper.rb', line 182

def xml args
  register_method = args.keys.detect {|key| self.respond_to?("register_#{key}_mapping", true)} || "self"
  
  self.__send__("register_#{register_method}_mapping", args)
end