Class: ActiveExport::Xml

Inherits:
Base
  • Object
show all
Defined in:
lib/active_export/xml.rb

Instance Attribute Summary

Attributes inherited from Base

#config, #eval_methods, #label_keys, #label_prefix, #namespace, #options, #source, #source_name

Instance Method Summary collapse

Methods inherited from Base

build_label_keys_and_eval_methods, #build_label_keys_and_eval_methods!, #convert, #default_scope, export, #export_data, export_file, #find_in_batches_options, #generate_header, #generate_value, #initialize, #key_name, translate, #translate

Constructor Details

This class inherits a constructor from ActiveExport::Base

Instance Method Details

#binding(values) ⇒ Object (protected)



29
30
31
32
33
34
35
# File 'lib/active_export/xml.rb', line 29

def binding(values)
  result_string = binding_source.dup
  values.each_pair do |k,v|
    result_string.gsub!("%%#{k}%%", v)
  end
  result_string
end

#binding_sourceObject (protected)



37
38
39
# File 'lib/active_export/xml.rb', line 37

def binding_source
  @xml_body ||= source[:xml_format][:body]
end

#export(data) ⇒ String

Returns XML style string.

Returns:

  • (String)

    XML style string



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_export/xml.rb', line 6

def export(data)
  str = ''
  str << source[:xml_format][:header]
  [].tap {|o| export_data(data, o)}.inject([]) do |result, f|
    result << {}.tap{|o|
      self.label_keys.each_with_index {|k, i| o[k] = f[i] }
    }
  end.each do |f|
    str << binding(f)
  end
  str << source[:xml_format][:footer]
  str
end

#export_file(data, filename) ⇒ Object



20
21
22
23
24
# File 'lib/active_export/xml.rb', line 20

def export_file(data, filename)
  File.atomic_write(filename.to_s) do |file|
    file.write export(data)
  end
end