Class: Uniword::Resource::Importer
- Inherits:
-
Object
- Object
- Uniword::Resource::Importer
- 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
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#word ⇒ Object
readonly
Returns the value of attribute word.
Instance Method Summary collapse
-
#import_all(force: false) ⇒ Hash
Import all resources from Word.
-
#import_all_of_type(type, force: false) ⇒ Integer
Import all resources of a type.
-
#import_one(type, name) ⇒ Object
Import a single resource by name.
-
#initialize(word, cache) ⇒ Importer
constructor
Initialize importer with dependencies.
Constructor Details
#initialize(word, cache) ⇒ Importer
Initialize importer with dependencies
16 17 18 19 |
# File 'lib/uniword/resource/importer.rb', line 16 def initialize(word, cache) @word = word @cache = cache end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
10 11 12 |
# File 'lib/uniword/resource/importer.rb', line 10 def cache @cache end |
#word ⇒ Object (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
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
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.}" end end count end |
#import_one(type, name) ⇒ Object
Import a single resource by name
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 |