Module: GraphQL::Define::InstanceDefinable::ClassMethods

Defined in:
lib/graphql/define/instance_definable.rb

Instance Method Summary collapse

Instance Method Details

#accepts_definitions(*accepts) ⇒ Object

Attach definitions to this class. Each symbol in ‘accepts` will be assigned with `key=`. The last entry in accepts may be a hash of name-proc pairs for custom definitions.



93
94
95
# File 'lib/graphql/define/instance_definable.rb', line 93

def accepts_definitions(*accepts)
  @own_dictionary = own_dictionary.merge(AssignmentDictionary.create(*accepts))
end

#define(**kwargs, &block) ⇒ Object

Prepare the defintion for an instance of this class using its definitions. Note that the block is not called right away – instead, it’s deferred until one of the defined fields is needed.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/graphql/define/instance_definable.rb', line 74

def define(**kwargs, &block)
  instance = self.new

  instance.definition_proc = -> (obj) {
    kwargs.each do |keyword, value|
      public_send(keyword, value)
    end

    if block
      instance_eval(&block)
    end
  }

  instance
end

#dictionaryHash

Returns combined definitions for self and ancestors.

Returns:

  • (Hash)

    combined definitions for self and ancestors



115
116
117
118
119
120
121
# File 'lib/graphql/define/instance_definable.rb', line 115

def dictionary
  if superclass.respond_to?(:dictionary)
    own_dictionary.merge(superclass.dictionary)
  else
    own_dictionary
  end
end

#lazy_defined_attr_accessor(*attr_names) ⇒ Object

Define a reader and writer for each of ‘attr_names` which ensures that the definition block was called before accessing it.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/graphql/define/instance_definable.rb', line 99

def lazy_defined_attr_accessor(*attr_names)
  attr_names.each do |attr_name|
    ivar_name = :"@#{attr_name}"
    define_method(attr_name) do
      ensure_defined
      instance_variable_get(ivar_name)
    end

    define_method("#{attr_name}=") do |new_value|
      ensure_defined
      instance_variable_set(ivar_name, new_value)
    end
  end
end

#own_dictionaryHash

Returns definitions for this class only.

Returns:

  • (Hash)

    definitions for this class only



124
125
126
# File 'lib/graphql/define/instance_definable.rb', line 124

def own_dictionary
  @own_dictionary ||= {}
end