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.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/graphql/define/instance_definable.rb', line 131

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`



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

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



163
164
165
166
167
168
169
# File 'lib/graphql/define/instance_definable.rb', line 163

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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/graphql/define/instance_definable.rb', line 147

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



172
173
174
# File 'lib/graphql/define/instance_definable.rb', line 172

def own_dictionary
  @own_dictionary ||= {}
end