Class: Aperitiiif::Index
- Inherits:
-
Object
- Object
- Aperitiiif::Index
- Defined in:
- lib/aperitiiif/index.rb
Overview
TO DO COMMENT
Constant Summary collapse
- VALID_TYPES =
%i[json html].freeze
- TEMPLATE_FILE =
"#{__dir__}/template/index.html".freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
-
#initialize(batch) ⇒ Index
constructor
A new instance of Index.
- #path(type) ⇒ Object
-
#to_html(items = self.items, config = self.config) ⇒ Object
has smell :reek:DuplicateMethodCall.
-
#to_json(items = self.items) ⇒ Object
rubocop: enable Metrics/AbcSize.
-
#write(**options) ⇒ Object
rubocop: disable Metrics/AbcSize has smell :reek:TooManyStatements.
Constructor Details
#initialize(batch) ⇒ Index
Returns a new instance of Index.
15 16 17 18 19 |
# File 'lib/aperitiiif/index.rb', line 15 def initialize(batch) @batch = batch @config = batch.config @items = batch.items end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/aperitiiif/index.rb', line 13 def config @config end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
13 14 15 |
# File 'lib/aperitiiif/index.rb', line 13 def items @items end |
Instance Method Details
#path(type) ⇒ Object
21 22 23 |
# File 'lib/aperitiiif/index.rb', line 21 def path(type) "#{@config.html_build_dir}/index.#{type}" end |
#to_html(items = self.items, config = self.config) ⇒ Object
has smell :reek:DuplicateMethodCall
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/aperitiiif/index.rb', line 47 def to_html(items = self.items, config = self.config) vars = { 'items' => items.map(&:to_hash), 'config' => config.hash, 'iiif_collection_url' => @batch.iiif_collection_url, 'formatted_date' => Aperitiiif::Utils.formatted_datetime } template = Liquid::Template.parse File.open(TEMPLATE_FILE).read template.render vars end |
#to_json(items = self.items) ⇒ Object
rubocop: enable Metrics/AbcSize
42 43 44 |
# File 'lib/aperitiiif/index.rb', line 42 def to_json(items = self.items) JSON.pretty_generate items.map(&:to_hash) end |
#write(**options) ⇒ Object
rubocop: disable Metrics/AbcSize has smell :reek:TooManyStatements
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/aperitiiif/index.rb', line 27 def write(**) raise Aperitiiif::Error, "Index#write is missing required 'type:' option" unless .key? :type raise Aperitiiif::Error, "Index#write 'type: #{[:type]}' does not match available types #{VALID_TYPES}" unless VALID_TYPES.include? [:type] print "Writing #{[:type]} index...".colorize(:cyan) index = [:type] == :html ? to_html : to_json path = &.[](:path) || path([:type]) Aperitiiif::Utils.mkfile_p path, index puts "\r#{"Writing #{[:type]} index:".colorize(:cyan)} #{'Done ✓'.colorize(:green)} " end |