lib/plist4r/backend/haml.rb in plist4r-0.2.2 vs lib/plist4r/backend/haml.rb in plist4r-1.0.0

- old
+ new

@@ -1,13 +1,18 @@ require 'plist4r/backend_base' +require 'plist4r/mixin/ruby_stdlib' +require 'haml' +require 'base64' +require 'date' -# Requires Haml. Implements writing and saving for the :xml file format only. +# This backend uses haml to generate xml plists +# @author Dreamcat4 (dreamcat4@gmail.com) module Plist4r::Backend::Haml - class << self - def to_xml_haml - @to_xml_haml ||= <<-'EOC' + class << self + def to_xml_haml + @to_xml_haml ||= <<-'EOC' !!! XML UTF-8 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" > %plist{ :version => '1.0' } %dict @@ -18,58 +23,64 @@ - when TrueClass, FalseClass %key #{k} <#{v}/> - when String %key #{k} - %string #{v} - - when Fixnum + - if v.blob? + - data = Base64::encode64(v) + %data #{data} + - else + %string #{v} + - when Integer %key #{k} %integer #{v} + - when Float + %key #{k} + %real #{v} + - when Time + %key #{k} + %date #{v.utc.strftime('%Y-%m-%dT%H:%M:%SZ')} + - when Date + %key #{k} + %date #{v.strftime('%Y-%m-%dT%H:%M:%SZ')} - when Array %key #{k} %array - v.compact.each do |e| - case e - when TrueClass, FalseClass - <#{v}/> + <#{e}/> - when String - %string #{e} - - when Fixnum - %integer #{v} + - if e.blob? + - data = Base64::encode64(e) + %data #{data} + - else + %string #{e} + - when Integer + %integer #{e} + - when Float + %real #{e} + - when Time + %date #{e.utc.strftime('%Y-%m-%dT%H:%M:%SZ')} + - when Date + %date #{e.strftime('%Y-%m-%dT%H:%M:%SZ')} - when Hash %dict - tab_up ; block.call(e, block) ; tab_down - - else - - raise "Invalid input. Array: #{v.inspect} can only contain elements of type String (<string>) or Hash (<dict>). Found element: #{e.inspect} of type: \"#{e.class}\"" - when Hash %key #{k} %dict - tab_up ; block.call(v, block) ; tab_down - - else - - raise "Invalid input. Hash: #{data.inspect} can only contain values of type true, false, String (<string>) Fixnum (<integer>) Array (<array>) or Hash (<dict>). Found value: #{v.inspect} of type: \"#{v.class}\"" - - p.call( @hash, p) + + - p.call( @plist.to_hash, p) EOC - end + end - def to_xml plist - require 'haml' - # engine = Haml::Engine.new File.read("launchd_plist.haml") - engine = Haml::Engine.new to_xml_haml - rendered_xml_output = engine.render self - File.open(@filename,'w') do |o| - o << rendered_xml_output - end - end + def to_xml plist + @plist = plist + engine = Haml::Engine.new to_xml_haml + rendered_xml_output = engine.render self + end - def save plist - file_format = plist.file_format || Config[:default_format] - raise "#{self} - cant save file format #{file_format}" unless file_format == :xml - - hash = plist.to_hash - filename = plist.filename_path - File.open(filename,'w') do |out| - out << to_xml(plist) - end - end - end + end end