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.



563
564
565
566
567
568
569
570
571
# File 'lib/apiql.rb', line 563

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



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

def authorize!(*args); end

#inject_delegators(target) ⇒ Object



575
576
577
578
579
580
581
582
# File 'lib/apiql.rb', line 575

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

#parse_params(list) ⇒ Object



584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/apiql.rb', line 584

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



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/apiql.rb', line 598

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
      raise
    end
  end
end