Sha256: 7e20c8207d39f7d81147c7463483b8f50b020a12760eee8e759f95c59d753a52

Contents?: true

Size: 1.57 KB

Versions: 40

Compression:

Stored size: 1.57 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, json] optional custom serializer
    # @yieldparam node [Node] the node that is being serialized
    # @yieldparam json [Hash] the default serialization
    # @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: encoding))
    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_base/parsing.rb
eco-helpers-3.0.20 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.19 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.18 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.17 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.16 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.15 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.14 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.13 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.12 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.11 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.10 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.9 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.8 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.7 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.6 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.5 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.4 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.3 lib/eco/data/locations/node_base/parsing.rb
eco-helpers-3.0.2 lib/eco/data/locations/node_base/parsing.rb