Sha256: 9d879b4465a96a97133c9389426180041f5bfd243d15f7cce0ea24fb1943a6a9

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

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.compact.map {|val| to_classification(val)}.compact
      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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
eco-helpers-3.0.11 lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb
eco-helpers-3.0.10 lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb
eco-helpers-3.0.9 lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb
eco-helpers-3.0.8 lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb
eco-helpers-3.0.7 lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb
eco-helpers-3.0.6 lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb
eco-helpers-3.0.5 lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb