Class: Uniword::Resource::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/resource/importer.rb

Overview

MODEL for importing resources from Word or files

Has state (word/cache references) and behavior (import_one, import_all) Does NOT extend Lutaml::Model::Serializable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word, cache) ⇒ Importer

Initialize importer with dependencies

Parameters:



16
17
18
19
# File 'lib/uniword/resource/importer.rb', line 16

def initialize(word, cache)
  @word = word
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/uniword/resource/importer.rb', line 10

def cache
  @cache
end

#wordObject (readonly)

Returns the value of attribute word.



10
11
12
# File 'lib/uniword/resource/importer.rb', line 10

def word
  @word
end

Instance Method Details

#import_all(force: false) ⇒ Hash

Import all resources from Word

Parameters:

  • force (Boolean) (defaults to: false)

    Overwrite existing cache entries

Returns:

  • (Hash)

    Count of imported resources by type



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/uniword/resource/importer.rb', line 70

def import_all(force: false)
  result = {}

  %i[theme styleset].each do |type|
    result[type] = import_all_of_type(type, force: force)
  end

  %i[color_scheme font_scheme].each do |type|
    result[type] = import_bundled(type, force: force)
  end

  result
end

#import_all_of_type(type, force: false) ⇒ Integer

Import all resources of a type

Parameters:

  • type (Symbol)

    Resource type

  • force (Boolean) (defaults to: false)

    Overwrite existing cache entries

Returns:

  • (Integer)

    Number of resources imported



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/uniword/resource/importer.rb', line 43

def import_all_of_type(type, force: false)
  unless word.installed?
    raise ArgumentError,
          "Microsoft Word is not installed"
  end

  available = available_resources(type)
  count = 0

  available.each do |name|
    next if !force && cache.exists?(type, name)

    begin
      import_one(type, name)
      count += 1
    rescue StandardError => e
      warn "Failed to import #{type} '#{name}': #{e.message}"
    end
  end

  count
end

#import_one(type, name) ⇒ Object

Import a single resource by name

Parameters:

  • type (Symbol)

    Resource type (:theme, :styleset, :color_scheme, :font_scheme)

  • name (String)

    Resource name

Returns:

  • (Object)

    The imported resource

Raises:

  • (ArgumentError)

    if Word not installed or resource not found



27
28
29
30
31
32
33
34
35
36
# File 'lib/uniword/resource/importer.rb', line 27

def import_one(type, name)
  unless word.installed?
    raise ArgumentError,
          "Microsoft Word is not installed"
  end

  resource = load_from_word(type, name)
  cache.write(type, name, resource.to_yaml)
  resource
end