Class: APIQL::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/apiql.rb

Direct Known Subclasses

HashEntity

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, context) ⇒ Entity

Returns a new instance of Entity.



342
343
344
345
346
347
# File 'lib/apiql.rb', line 342

def initialize(object, context)
  @object = object
  @context = context
  authorize! :read, object
  @context.inject_delegators(self)
end

Class Attribute Details

.apiql_attributesObject (readonly)

Returns the value of attribute apiql_attributes.



320
321
322
# File 'lib/apiql.rb', line 320

def apiql_attributes
  @apiql_attributes
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



340
341
342
# File 'lib/apiql.rb', line 340

def object
  @object
end

Class Method Details

.attributes(*attrs) ⇒ Object



334
335
336
337
# File 'lib/apiql.rb', line 334

def attributes(*attrs)
  @apiql_attributes ||= []
  @apiql_attributes += attrs.map(&:to_sym)
end

.inherited(child) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/apiql.rb', line 322

def inherited(child)
  super

  return if self.class == ::APIQL::Entity

  attributes = apiql_attributes&.try(:deep_dup) || []

  child.instance_eval do
    @apiql_attributes = attributes
  end
end

Instance Method Details

#render(schema = nil) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/apiql.rb', line 349

def render(schema = nil)
  return unless @object.present?

  respond = {}

  schema.each do |field|
    if field.is_a? Hash
      field.each do |field, sub_schema|
        reg = field.match(/\A((?<alias>[\w\.]+):\s*)?(?<name>[\w\.]+)(\((?<params>.*?)\))?\z/)
        raise Error, field unless reg.present?

        name = reg[:alias] || reg[:name]

        if respond[name].is_a? ::Hash
          respond = respond.deep_merge({
            name => render_attribute(reg[:name], reg[:params].presence, sub_schema)
          })
        else
          respond[name] = render_attribute(reg[:name], reg[:params].presence, sub_schema)
        end
      end
    else
      reg = field.match(/\A((?<alias>[\w\.]+):\s*)?(?<name>[\w\.]+)(\((?<params>.*?)\))?\z/)
      raise Error, field unless reg.present?

      name = reg[:alias] || reg[:name]

      if respond[name].is_a? ::Hash
        respond = respond.deep_merge({
          name => render_attribute(reg[:name], reg[:params].presence)
        })
      else
        respond[name] = render_attribute(reg[:name], reg[:params].presence)
      end
    end
  end

  respond
end