Sha256: 435daa579aa0f0b635ef97a3df8992980a6caaadfc67adfe6b4b2619a445b258

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 KB

Contents

module Eco::Data::Locations::NodeBase
  module Parsing
    include Eco::Data::Locations::Convert
    include Eco::Data::Locations::NodeBase::Treeify

    # @param csv [CSV::Table]
    # @return [Array<NodePlain>, Array<NodeLevel>] with integrity issues resolved.
    def nodes_from_csv(csv)
      raise ArgumentError, "Expecting CSV::Table. Given: #{csv.class}" unless csv.is_a?(::CSV::Table)
      return Eco::Data::Locations::NodePlain.nodes_from_csv(csv) if Eco::Data::Locations::NodePlain.csv_matches_format?(csv)
      return Eco::Data::Locations::NodeLevel.nodes_from_csv(csv) if Eco::Data::Locations::NodeLevel.csv_matches_format?(csv)
      raise ArgumentError, "The input csv does not have the required format to read a locations structure."
    end

    # @yield [Node] optional custom serializer
    # @yieldreturn [Hash] the serialized Node
    # @return [Array<Hash>] a hierarchical tree of nested Hashes via `nodes` key.
    def hash_tree_from_csv(csv, &block)
      raise ArgumentError, "Expecting CSV::Table. Given: #{csv.class}" unless csv.is_a?(::CSV::Table)
      treeify(nodes_from_csv(csv), &block)
    end

    # Shortcut to obtain a list of parsed nodes out of a file
    # @param filename [String] the csv file.
    # @return [Array<NodePlain>, Array<NodeLevel>] with integrity issues resolved.
    def csv_nodes_from(filename, encoding: 'utf-8')
      nodes_from_csv(csv_from(filename, encoding: 'utf-8'))
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
eco-helpers-2.6.4 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.6.3 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.6.2 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.6.1 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.6.0 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.10 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.9 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.8 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.7 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.6 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.5 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.4 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-2.5.3 lib/eco/data/locations/node_base/parsing.rb