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.



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/graphql/define/instance_definable.rb', line 159

def accepts_definitions(*accepts)
  new_assignments = if accepts.last.is_a?(Hash)
    accepts.pop.dup
  else
    {}
  end

  accepts.each do |key|
    new_assignments[key] = AssignAttribute.new(key)
  end

  @own_dictionary = own_dictionary.merge(new_assignments)
end

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

Create a new instance and prepare a definition using its definitions.

Parameters:

  • kwargs (Hash)

    Key-value pairs corresponding to defininitions from accepts_definitions

  • block (Proc)

    Block which calls helper methods from accepts_definitions



150
151
152
153
154
# File 'lib/graphql/define/instance_definable.rb', line 150

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

#dictionaryHash

Returns combined definitions for self and ancestors.

Returns:

  • (Hash)

    combined definitions for self and ancestors



188
189
190
191
192
193
194
# File 'lib/graphql/define/instance_definable.rb', line 188

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

#ensure_defined(*method_names) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/graphql/define/instance_definable.rb', line 173

def ensure_defined(*method_names)
  ensure_defined_module = Module.new
  ensure_defined_module.module_eval {
    method_names.each do |method_name|
      define_method(method_name) { |*args, &block|
        ensure_defined
        super(*args, &block)
      }
    end
  }
  self.prepend(ensure_defined_module)
  nil
end

#own_dictionaryHash

Returns definitions for this class only.

Returns:

  • (Hash)

    definitions for this class only



197
198
199
# File 'lib/graphql/define/instance_definable.rb', line 197

def own_dictionary
  @own_dictionary ||= {}
end