Class: Reading::Row::VariantsAttribute

Inherits:
Attribute show all
Defined in:
lib/reading/attribute/variants/variants_attribute.rb

Instance Method Summary collapse

Methods inherited from Attribute

#initialize

Constructor Details

This class inherits a constructor from Reading::Row::Attribute

Instance Method Details

#parseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/reading/attribute/variants/variants_attribute.rb', line 11

def parse
  sources_str = columns[:sources]&.presence || " "

  format_as_separator = config.deep_fetch(:csv, :regex, :formats_split)

  sources_str.split(format_as_separator).map { |variant_with_extras|
    # without extra info or series
    bare_variant = variant_with_extras
      .split(config.deep_fetch(:csv, :long_separator))
      .first

    series_attr = SeriesSubattribute.new(item_head:, variant_with_extras:, config:)
    sources_attr = SourcesSubattribute.new(bare_variant:, config:)
    # Length, despite not being very complex, is still split out into a
    # subattribute because it needs to be accessible to
    # ExperiencesAttribute (more specifically SpansSubattribute) which
    # uses length as a default value for amount.
    length_attr = LengthSubattribute.new(bare_variant:, columns:, config:)
    extra_info_attr = ExtraInfoSubattribute.new(item_head:, variant_with_extras:, config:)

    variant =
      {
        format: format(bare_variant) || format(item_head) || template.fetch(:format),
        series: series_attr.parse                         || template.fetch(:series),
        sources: sources_attr.parse                       || template.fetch(:sources),
        isbn: isbn(bare_variant)                          || template.fetch(:isbn),
        length: length_attr.parse                         || template.fetch(:length),
        extra_info: extra_info_attr.parse                 || template.fetch(:extra_info)
      }

    if variant != template
      variant
    else
      nil
    end
  }.compact.presence
end