Class: Doing::JSONImport

Inherits:
Object show all
Includes:
Util
Defined in:
lib/doing/plugins/import/json_import.rb

Overview

JSON importer

Class Method Summary collapse

Methods included from Util

#args_for_editor, #deep_merge_hashes, #deep_merge_hashes!, #default_editor, #duplicable?, #duplicate_frozen_values, #editor_with_args, #exec_available, #find_default_editor, #first_available_exec, #mergable?, #merge_default_proc, #merge_values, #safe_load_file, #user_home, #write_to_file

Class Method Details

.import(wwid, path, options: {}) ⇒ Object

Imports a Timing report

Parameters:

  • wwid (WWID)

    The wwid object

  • path (String)

    Path to JSON report file

  • options (Hash) (defaults to: {})

    Additional Options



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/doing/plugins/import/json_import.rb', line 26

def self.import(wwid, path, options: {})
  exit_now! 'Path to JSON export required' if path.nil?
  options[:no_overlap] ||= false
  options[:autotag] ||= Doing.auto_tag

  exit_now! 'File not found' unless File.exist?(File.expand_path(path))

  updated = 0
  added = 0
  skipped = 0

  data = JSON.parse(IO.read(File.expand_path(path)))
  new_items = []
  new_section = options[:section] || Doing.setting('current_section')

  data['items'].each do |entry|
    title = entry['title']
    date = Time.parse(entry['date'])
    date ||= entry['date'].chronify
    note = Doing::Note.new(entry['note'])
    section = if entry['section'].empty?
                new_section
              else
                entry['section']
              end
    id = entry.key?('id') ? entry['id'] : nil

    new_item = Doing::Item.new(date, title, section, note, id)

    is_match = true

    if options[:search]
      is_match = new_item.search(options[:search], case_type: options[:case], negate: options[:not])
    end

    if is_match && options[:date_filter]
      is_match = start_time > options[:date_filter][0] && start_time < options[:date_filter][1]
      is_match = options[:not] ? !is_match : is_match
    end

    unless is_match
      skipped += 1
      next

    end

    if wwid.content.find_id(new_item.id)
      old_index = wwid.content.index_for_id(entry['id'])
      old_item = wwid.content[old_index].clone
      wwid.content[old_index] = new_item
      Hooks.trigger :post_entry_updated, self, new_item, old_item
      updated += 1
    else
      Hooks.trigger :pre_entry_add, self, item
      wwid.content << new_entry
      Hooks.trigger :post_entry_added, self, item
      added += 1
    end
  end
  total = new_items.count
  skipped = data.count - total
  Doing.logger.debug('Skipped:', %(#{skipped} items)) if skipped.positive?
  Doing.logger.info('Updated:', %(#{updated} items))
  Doing.logger.info('Imported:', %(#{added} new items to #{new_section}))
end

.settingsObject



12
13
14
15
16
# File 'lib/doing/plugins/import/json_import.rb', line 12

def self.settings
  {
    trigger: 'json'
  }
end