Sha256: 27f1297c1faeb88da9b96c72a6635c3bf12e2eb3adc0082dcdbaf1776b7ff3ba

Contents?: true

Size: 1.27 KB

Versions: 40

Compression:

Stored size: 1.27 KB

Contents

class Eco::Data::Locations::NodePlain
  module Parsing
    attr_writer :node_class

    def node_class
      @node_class ||= Eco::Data::Locations::NodePlain
    end

    # Subset of property names expected to be among the headers
    def basic_headers
      @basic_headers ||= %w[id name parent_id]
    end

    # Checks if the `basic_headers` to process as `NodePlain` are present.
    # @param [CSV::Table]
    # @return [Boolean] whether or not it's worthy trying to parse with `NodePlain`.
    def csv_matches_format?(csv)
      return false unless csv.is_a?(::CSV::Table)
      (basic_headers & csv.headers) == basic_headers
    end

    # It builds each NodePlain from the input csv.
    # @param `csv` [CSV::Table] with specific headers
    # @return [Array<NodePlain>]
    def nodes_from_csv(csv)
      raise ArgumentError, "Expecting CSV::Table. Given: #{csv.class}" unless csv.is_a?(::CSV::Table)
      # Convert to Eco::CSV::Table for a fresh start
      csv     = Eco::CSV.parse(csv.to_csv).nil_blank_cells.add_index_column(:row_num)
      headers = node_class::ALL_ATTRS.map(&:to_s)
      csv.each_with_object([]) do |row, out|
        row_num, *values = row.values_at(*headers)
        out << node_class.new(row_num, *values)
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.20 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.19 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.18 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.17 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.16 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.15 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.14 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.13 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.12 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.11 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.10 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.9 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.8 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.7 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.6 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.5 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.4 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.3 lib/eco/data/locations/node_plain/parsing.rb
eco-helpers-3.0.2 lib/eco/data/locations/node_plain/parsing.rb