module Eco::Data::Locations::NodeBase module CsvConvert include Eco::Data::Locations::NodeBase::Parsing def tree_class Eco::API::Organization::TagTree end # @yield [Node] optional custom serializer # @yieldreturn [Hash] the serialized Node # @param value [CSV::Table, Eco::API::Organization::TagTree] # @return [Array] a plain list of hash nodes def hash_list(value, &block) return hash_list(org_tree(value)) if value.is_a?(::CSV::Table) return value.as_nodes_json if value.is_a?(tree_class) raise ArgumentError, "Expecting Eco::API::Organization::TagTree or CSV::Table. Given: #{value.class}" end # @yield [Node] optional custom serializer # @yieldreturn [Hash] the serialized Node # @param value [CSV::Table, Eco::API::Organization::TagTree] # @return [Array] a hierarchical tree of hash nodes, # ready to be parsed as an organization tagtree def hash_tree(value, &block) return hash_tree_from_csv(value, &block) if value.is_a?(::CSV::Table) return value.as_json if value.is_a?(tree_class) raise ArgumentError, "Expecting Eco::API::Organization::TagTree or CSV::Table. Given: #{value.class}" end # @yield [Node] optional custom serializer # @yieldreturn [Hash] the serialized Node # @param value [CSV::Table, Eco::API::Organization::TagTree] # @return [Eco::API::Organization::TagTree] def org_tree(value, &block) return tree_class.new(hash_tree(value), &block) if value.is_a?(::CSV::Table) return tree_class.new(value.as_json) if value.is_a?(tree_class) raise ArgumentError, "Expecting Eco::API::Organization::TagTree or CSV::Table. Given: #{value.class}" end # @yield [Node] optional custom serializer # @yieldreturn [Hash] the serialized Node # @return [CSV::Table] a table with L1 to Ln columns ready for dump to csv def csv_tree(value, encoding: 'utf-8', &block) Eco::CSV::Table.new(hash_tree_to_tree_csv(hash_tree(value, &block))) end # @note it just converts to an organizational tagtree and uses a helper method. # @yield [Node] optional custom serializer # @yieldreturn [Hash] the serialized Node # @param value [CSV::Table, Eco::API::Organization::TagTree] # @return [CSV::Table] a table with a list of nodes and their parents def csv_list(value, &block) value = org_tree(value, &block) unless value.is_a?(tree_class) Eco::CSV.Table.new(hash_list(value)) end end end