class Magnetize::Convert
Constants
- DEFAULTS
Public Instance Methods
to_magento(options = {})
click to toggle source
# File lib/magnetize/convert.rb, line 29 def to_magento(options = {}) options = DEFAULTS.deep_merge(options) result = {} options[:types].each do |type, config| input = File.expand_path(config[:toml]) output = File.expand_path(config[:magento]) if config[:content] hash = TOML.load(config[:content]) else hash = read_toml(input) end xml = make_xml(hash[type.to_s]) if options[:write] write(output, xml, options[:force]) else result[config[:magento]] = xml end end return result unless options[:write] end
to_toml(options = {})
click to toggle source
# File lib/magnetize/convert.rb, line 50 def to_toml(options = {}) options = DEFAULTS.deep_merge(options) result = {} options[:types].each do |type, config| input = File.expand_path(config[:magento]) output = File.expand_path(config[:toml]) if config[:toml] if config[:content] xml = config[:content] else xml = IO.read(input) end hash = { type.to_s => read_xml(xml) } config = read_toml(output) config.merge!(hash) toml = make_toml(config) if options[:write] write(output, toml, options[:force]) else result[type] = toml end end return result unless options[:write] end
Private Instance Methods
make_toml(hash)
click to toggle source
# File lib/magnetize/convert.rb, line 86 def make_toml(hash) TOML::Generator.new(hash).body end
make_xml(hash)
click to toggle source
# File lib/magnetize/convert.rb, line 94 def make_xml(hash) Gyoku.xml(hash, { :key_converter => :none, :builder => { :indent => 2 } }) end
read_toml(path)
click to toggle source
# File lib/magnetize/convert.rb, line 78 def read_toml(path) if !path.nil? && File.exists?(path) TOML.load_file(path) else {} end end
read_xml(xml)
click to toggle source
# File lib/magnetize/convert.rb, line 90 def read_xml(xml) replace_nil(Hash.from_xml(xml)) end
replace_nil(h)
click to toggle source
# File lib/magnetize/convert.rb, line 110 def replace_nil(h) h.each_pair do |k,v| if v.is_a?(Hash) replace_nil(v) else h[k] = v.to_s if v.nil? end end end
write(path, content, force=false)
click to toggle source
# File lib/magnetize/convert.rb, line 98 def write(path, content, force=false) FileUtils.mkdir_p(File.dirname(path)) unless force if File.exists?(path) say("#{path} already exists. ", :red) abort("----> Skipped #{File.basename(path)}") if no?("Overwrite? (y/N):", :yellow) end end File.open(path, 'w') {|f| f.write(content)} puts "----> Wrote #{File.basename(path)}" end