Class: Reading::Parsing::Attributes::Variants
- Defined in:
- lib/reading/parsing/attributes/variants.rb
Overview
Transformer for the :variant item attribute.
Instance Method Summary collapse
-
#series(hash) ⇒ Array<Hash>
The :series sub-attribute for the given parsed hash.
-
#sources(hash) ⇒ Array<Hash>
The :sources sub-attribute for the given parsed hash.
-
#template ⇒ Hash
A shortcut to the variant template.
-
#transform_from_parsed(parsed_row, head_index) ⇒ Array<Hash>
An array of variants; see Config#default_config[:template].
-
#url_name(url) ⇒ String?
The name for the given URL string, according to Config.hash, or nil.
Instance Method Details
#series(hash) ⇒ Array<Hash>
The :series sub-attribute for the given parsed hash.
41 42 43 44 45 46 47 |
# File 'lib/reading/parsing/attributes/variants.rb', line 41 def series(hash) (hash[:series_names] || []) .zip(hash[:series_volumes] || []) .map { |name, volume| { name:, volume: Integer(volume, exception: false) } } end |
#sources(hash) ⇒ Array<Hash>
The :sources sub-attribute for the given parsed hash.
52 53 54 55 56 57 58 59 60 |
# File 'lib/reading/parsing/attributes/variants.rb', line 52 def sources(hash) hash[:sources]&.map { |source| if source.match?(/\Ahttps?:\/\//) { name: url_name(source), url: source } else { name: source, url: nil } end } end |
#template ⇒ Hash
A shortcut to the variant template.
34 35 36 |
# File 'lib/reading/parsing/attributes/variants.rb', line 34 def template Config.hash.deep_fetch(:item, :template, :variants).first end |
#transform_from_parsed(parsed_row, head_index) ⇒ Array<Hash>
Returns an array of variants; see Config#default_config[:template].
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/reading/parsing/attributes/variants.rb', line 13 def transform_from_parsed(parsed_row, head_index) head = parsed_row[:head][head_index] # || [{}] in case there is no Sources column. (parsed_row[:sources].presence || [{}])&.map { |variant| format = variant[:format] || head[:format] { format:, series: (series(head) + series(variant)).presence, sources: sources(variant) || sources(head), isbn: variant[:isbn] || variant[:asin], length: Attributes::Shared.length(variant, format:) || Attributes::Shared.length(parsed_row[:length], format:), extra_info: Array(head[:extra_info]) + Array(variant[:extra_info]), }.map { |k, v| [k, v || template.fetch(k)] }.to_h }&.compact&.presence end |
#url_name(url) ⇒ String?
The name for the given URL string, according to Config.hash, or nil.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/reading/parsing/attributes/variants.rb', line 66 def url_name(url) Config.hash .fetch(:source_names_from_urls) .each do |url_part, name| if url.include?(url_part) return name end end nil end |