Storing Plugin Data Revisited (Wall Presets)

I use plain text json in my plugin. It is still pretty easy to read in a text editor, but much easier to parse.

Here you can see how I can handle some or all of my settings being missing.

@settings_path = ENV['GEM_PATH'].split('SketchUp')[0] + "#{extension_name}/"
  def read_settings
    settings_file = @settings_path + 'settings.json'
    if File.exist?(settings_file)
      begin
        json = File.open(settings_file, 'r', &:read)
        @settings = JSON.parse(json)
        restore_default_settings unless @settings
        restore_colors unless @settings.key?('colors')
        restore_default_colors unless @settings.key?('default_colors')
        restore_default_building_specs unless @settings.key?('default_building')
        restore_default_preferences unless @settings.key?('preferences')
        restore_default_trim_widths unless @settings.key?('trim_widths')
        restore_default_sb_preferences unless @settings.key?('sb_preferences')
        change_trim_widths(@settings['trim_widths'].to_json, false)
      rescue StandardError => _e
        restore_default_settings
      end
    else
      restore_default_settings
    end
    read_openings
  end

With json it is very easy handle situations where ‘old’ settings files are missing properties.

data['key'] ||= default_value
3 Likes