Sha256: 2a217e538f1b687905e0a75ee46104b3cdede91b31303ac852f640645a87dc84

Contents?: true

Size: 945 Bytes

Versions: 1

Compression:

Stored size: 945 Bytes

Contents

module Plister
  class Exporter
    attr_reader :path

    def initialize(path = nil)
      @path = path || "/Users/#{Plister.user}/.osx.yml"
    end

    def export
      File.write path, to_s
    end

    def to_s
      Psych.dump(preferences)
    end

    private

    def types
      @types ||= {
        system: '/Library/Preferences',
        user:   "/Users/#{Plister.user}/Library/Preferences",
        host:   "/Users/#{Plister.user}/Library/preferences/ByHost"
      }
    end

    def paths
      @paths ||= types.map { |type, path| [type, Dir["#{path}/*.plist"]] }.to_h
    end

    def preferences
      @preferences ||= begin
        output = {}
        paths.each do |type, plist_paths|
          plists = plist_paths.map { |domain| Plist.new domain, type: type }
          plists.select!(&:readable?)
          output[type.to_s] = plists.map { |p| [p.domain, p.to_h] }.to_h
        end
        output
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
plister-0.2.0 lib/plister/exporter.rb