class MacAppSync::Defaults::PlistConverter

Constants

TYPE_MAPPINGS

Public Class Methods

to_plist(value_data) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 21
def to_plist(value_data)
  build_plist_value(value_data.fetch("type"), value_data.fetch("value"))
end
to_ruby(plist_data) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 17
def to_ruby(plist_data)
  convert_plist_dict(plist_data)
end

Private Class Methods

build_plist_value(type, value) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 40
def build_plist_value(type, value)
  plist_class = TYPE_MAPPINGS.invert.fetch(type.to_sym)
  converted_value = convert_ruby_value(type, value)

  plist_class.new(converted_value)
end
convert_plist_array(container) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 60
def convert_plist_array(container)
  container.value.map { |element| convert_plist_value(element) }
end
convert_plist_dict(container) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 64
def convert_plist_dict(container)
  ruby_values = container.value.map { |key, element| [key, convert_plist_value(element)] }
  ruby_values.sort_by { |key, _| key }.to_h
end
convert_plist_simple(value, type) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 69
def convert_plist_simple(value, type)
  {
    "value" => value,
    "type" => type
  }
end
convert_plist_value(container) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 27
def convert_plist_value(container)
  case container
  when CFPropertyList::CFArray
    convert_plist_simple(convert_plist_array(container), :array)
  when CFPropertyList::CFDictionary
    convert_plist_simple(convert_plist_dict(container), :dict)
  when CFPropertyList::CFData
    convert_plist_simple(container.encoded_value, :data)
  else
    convert_plist_simple(container.value, TYPE_MAPPINGS.fetch(container.class))
  end
end
convert_ruby_value(type, value) click to toggle source
# File lib/mac_app_sync/defaults/plist_converter.rb, line 47
def convert_ruby_value(type, value)
  case type
  when "array"
    value.map { |value_data| to_plist(value_data) }
  when "dict"
    value.map { |key, value_data| [key, to_plist(value_data)] }.to_h
  when "date"
    Time.parse(value)
  else
    value
  end
end