Class: APIQL::Entity

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

Direct Known Subclasses

HashEntity

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, context) ⇒ Entity

Returns a new instance of Entity.



401
402
403
404
405
406
# File 'lib/apiql.rb', line 401

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

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



399
400
401
# File 'lib/apiql.rb', line 399

def object
  @object
end

Class Method Details

.allObject



316
317
318
319
320
# File 'lib/apiql.rb', line 316

def all
  authorize! :read, @apiql_entity_class

  @apiql_entity_class.eager_load(eager_load).all
end

.create(params) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/apiql.rb', line 330

def create(params)
  authorize! :create, @apiql_entity_class

  if respond_to?(:create_params, params)
    params = create_params(params)
  elsif respond_to?(:params, params)
    params = self.params(params)
  end

  @apiql_entity_class.create!(params)
end

.destroy(id) ⇒ Object



356
357
358
359
360
361
362
# File 'lib/apiql.rb', line 356

def destroy(id)
  item = @apiql_entity_class.find(id)

  authorize! :destroy, item

  item.destroy!
end

.find(id) ⇒ Object



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

def find(id)
  item = @apiql_entity_class.eager_load(eager_load).find(id)

  authorize! :read, item

  item
end

.update(id, params) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/apiql.rb', line 342

def update(id, params)
  item = @apiql_entity_class.find(id)

  authorize! :update, item

  if respond_to?(:update_params, params)
    params = update_params(params)
  elsif respond_to?(:params, params)
    params = self.params(params)
  end

  item.update!(params)
end

Instance Method Details

#render(schema = nil) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/apiql.rb', line 408

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