<commit_before>require "plist"
require "serverkit/resources/base"
require "shellwords"
require "tempfile"

module Serverkit
  module Resources
    # A resource class for Mac OS X user defaults system.
    class Defaults < Base
      DEFAULT_DOMAIN = "NSGlobalDomain"

      # @example "com.apple.TextEdit"
      attribute :domain, default: DEFAULT_DOMAIN, type: String

      # @example "com.apple.keyboard.fnState"
      attribute :key, required: true, type: String

      # @example 1
      attribute :value, required: true, type: [Array, Fixnum, Float, Hash, String]

      def apply
        run_command("defaults write #{escaped_domain} #{escaped_key} #{type_option} '#{value_in_plist}'")
      end

      # @note Override
      # @return [true, false]
      def check
        value == read_value
      end

      private

      # @note Override
      def default_id
        "#{domain} #{key}"
      end

      # @return [String]
      # @example "com.apple.TextEdit"
      def escaped_domain
        ::Shellwords.shellescape(domain)
      end

      # @return [String]<commit_msg>Fix double-escape bug on writing string value<commit_after>require "plist"
require "serverkit/resources/base"
require "shellwords"
require "tempfile"

module Serverkit
  module Resources
    # A resource class for Mac OS X user defaults system.
    class Defaults < Base
      DEFAULT_DOMAIN = "NSGlobalDomain"

      # @example "com.apple.TextEdit"
      attribute :domain, default: DEFAULT_DOMAIN, type: String

      # @example "com.apple.keyboard.fnState"
      attribute :key, required: true, type: String

      # @example 1
      attribute :value, required: true, type: [Array, Fixnum, Float, Hash, String]

      def apply
        run_command("defaults write #{escaped_domain} #{escaped_key} #{type_option} #{value_in_plist}")
      end

      # @note Override
      # @return [true, false]
      def check
        value == read_value
      end

      private

      # @note Override
      def default_id
        "#{domain} #{key}"
      end

      # @return [String]
      # @example "com.apple.TextEdit"
      def escaped_domain
        ::Shellwords.shellescape(domain)
      end

      # @return [String]
{'fraction_non_alphanumeric': 0.10045454545454545, 'fraction_numerical': 0.0009090909090909091, 'mean_word_length': 2.632013201320132, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4022852', 'n_tokens_mistral': 660, 'n_tokens_neox': 634, 'n_words': 189}