Sha256: 6bc18a50be94128dd17bd5e1c3600e1c3bbd48ebbf7d1e988b3ddfa1d51f6b56

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module GraphqlRails
  module Model
    # Contains methods which are shared between various configurations.
    #
    # Expects `default_name` to be defined.
    # Expects `build_attribute(attr_name)` method to be defined.
    module Configurable
      require 'active_support/concern'
      require 'graphql_rails/concerns/chainable_options'

      extend ActiveSupport::Concern

      included do
        include GraphqlRails::ChainableOptions

        chainable_option :description
      end

      def initialize_copy(other)
        super
        @type_name = nil
        @attributes = other.attributes.transform_values(&:dup)
      end

      def attributes
        @attributes ||= {}
      end

      def name(*args)
        get_or_set_chainable_option(:name, *args) || default_name
      end

      def type_name
        @type_name ||= "#{name.camelize}Type#{SecureRandom.hex}"
      end

      def attribute(attribute_name, **attribute_options)
        key = attribute_name.to_s

        attributes[key] ||= build_attribute(attribute_name).tap do |new_attribute|
          new_attribute.with(**attribute_options) unless attribute_options.empty?
          yield(new_attribute) if block_given?
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphql_rails-3.0.0 lib/graphql_rails/model/configurable.rb
graphql_rails-2.4.0 lib/graphql_rails/model/configurable.rb
graphql_rails-2.3.0 lib/graphql_rails/model/configurable.rb
graphql_rails-2.2.0 lib/graphql_rails/model/configurable.rb
graphql_rails-2.1.0 lib/graphql_rails/model/configurable.rb
graphql_rails-2.0.0 lib/graphql_rails/model/configurable.rb