module Eco::API::UseCases::GraphQL::Helpers::Location module Base module ClassificationsParser include Eco::API::UseCases::GraphQL::Helpers::Base::CaseEnv private # @note # 1. It returns `nil` unless there are classifications defined # 2. If the value is wrong, it warns and returns `nil` def to_classification(value) return nil unless node_classifications? raise ArgumentError, "Expecting a single element. Given: #{value.class}" if value.is_a?(Enumerator) return nil unless value.is_a?(String) node_classifications.to_id(value).tap do |type| unknown_clasification!(value) unless type end end def to_classification_ids(values) values = [values].flatten unless values.is_a?(Array) values = values.compact.map {|val| val.split('|')}.flatten values.compact.map {|val| to_classification(val)}.compact.uniq end # @note # 1. It returns `nil` unless there are classifications defined # 2. If the value is wrong, it warns and returns `nil` def to_classification_name(value) return nil unless node_classifications? raise ArgumentError, "Expecting a single element. Given: #{value.class}" if value.is_a?(Enumerator) return nil unless value.is_a?(String) node_classifications.to_name(value) do |name| unknown_clasification!(value) unless name end end def node_classifications? node_classifications.any? end def node_classifications @node_classifications ||= session.node_classifications end def unknown_clasification!(value) return if unknown_classifications.include?(value) unknown_classifications << value log(:warn) { "Unknown location node classification '#{value}'" } end def unknown_classifications @unknown_classifications ||= [] end end end end