Class: APIQL::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(binder, *fields) ⇒ Context

Returns a new instance of Context.



493
494
495
496
497
498
499
500
501
# File 'lib/apiql.rb', line 493

def initialize(binder, *fields)
  @fields = fields
  fields.each do |field|
    instance_variable_set("@#{field}", binder.send(field))
    class_eval do
      attr_accessor field
    end
  end
end

Instance Method Details

#authorize!(*args) ⇒ Object



503
# File 'lib/apiql.rb', line 503

def authorize!(*args); end

#inject_delegators(target) ⇒ Object



505
506
507
508
509
510
511
# File 'lib/apiql.rb', line 505

def inject_delegators(target)
  @fields.each do |field|
    target.class_eval do
      delegate field, to: :@context
    end
  end
end

#parse_params(list) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/apiql.rb', line 513

def parse_params(list)
  list&.split(',')&.map(&:strip)&.map do |name|
    if reg = name.match(/\A[a-zA-Z_]\w*\z/)
      params[name]
    else
      begin
        Float(name)
      rescue
        name
      end
    end
  end
end

#render_value(value, schema) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/apiql.rb', line 527

def render_value(value, schema)
  if value.is_a? Hash
    HashEntity.new(value, self).render(schema)
  elsif value.respond_to?(:each) && value.respond_to?(:map)
    value.map do |object|
      render_value(object, schema)
    end
  elsif APIQL::simple_class?(value)
    value
  else
    begin
      "#{value.class.name}::Entity".constantize.new(value, self).render(schema)
    rescue StandardError
      if ::Rails.env.development?
        raise
      else
        nil
      end
    end
  end
end