Sha256: f0aadc2ba0d593352b274554a76599a88717f668abf48e48f56129f93915adf7

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module GraphqlRails
  module Attributes
    # Parses attribute name and can generates graphql scalar type,
    # grapqhl name and etc. based on that
    class AttributeNameParser
      attr_reader :name

      def initialize(original_name, options: {})
        name = original_name.to_s
        @required = !name['!'].nil?
        @name = name.tr('!', '')
        @options = options
      end

      def field_name
        @field_name ||= \
          if original_format?
            preprocesed_name
          else
            preprocesed_name.camelize(:lower)
          end
      end

      def graphql_type
        @graphql_type ||= \
          case name
          when 'id', /_id\Z/
            GraphQL::Types::ID
          when /\?\Z/
            GraphQL::Types::Boolean
          else
            GraphQL::Types::String
          end
      end

      def required?
        @required
      end

      private

      attr_reader :options

      def original_format?
        options[:input_format] == :original || options[:attribute_name_format] == :original
      end

      def preprocesed_name
        if name.end_with?('?')
          "is_#{name.remove(/\?\Z/)}"
        else
          name
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphql_rails-1.2.6 lib/graphql_rails/attributes/attribute_name_parser.rb
graphql_rails-1.2.4 lib/graphql_rails/attributes/attribute_name_parser.rb
graphql_rails-1.2.3 lib/graphql_rails/attributes/attribute_name_parser.rb
graphql_rails-1.2.2 lib/graphql_rails/attributes/attribute_name_parser.rb
graphql_rails-1.2.1 lib/graphql_rails/attributes/attribute_name_parser.rb