Module: RubyXmlMapper::RubyXmlMapperClassMethods

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

Instance Method Summary collapse

Instance Method Details

#check_file(file_name) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/ruby-xml-mapper.rb', line 149

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



159
160
161
# File 'lib/ruby-xml-mapper.rb', line 159

def new_from_xml_file file_name
  new_from_xml_node(parse_xml_file(file_name))
end

#new_from_xml_node(node) ⇒ Object



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

def new_from_xml_node node
  allocated = allocate
  allocated.initialize_from_xml node
  allocated
end

#parse_xml_file(file_name) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ruby-xml-mapper.rb', line 134

def parse_xml_file(file_name)
  check_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



169
170
171
172
173
# File 'lib/ruby-xml-mapper.rb', line 169

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