Module: ApipieDSL::Extension

Includes:
Delegatable
Defined in:
lib/apipie_dsl/dsl.rb

Instance Method Summary collapse

Instance Method Details

#apipie(context = :method, desc_or_options = nil, options = {}, &block) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/apipie_dsl/dsl.rb', line 541

def apipie(context = :method, desc_or_options = nil, options = {}, &block)
  case desc_or_options
  when Hash
    options = options.merge(desc_or_options)
  when String
    options[:desc] = desc_or_options
  end
  options[:name] ||= context.to_s

  block = proc {} unless block_given?

  delegatee = Delegatee.instance_for(self).with(&block)
  delegatee.short(options[:desc])
  # Don't eval the block, since it will be evaluated after method is defined
  return if context == :method

  # Currently method extensions are supported only
  Delegatee.instance_reset
end

#apipie_update(context = :method, &block) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/apipie_dsl/dsl.rb', line 561

def apipie_update(context = :method, &block)
  block = proc {} unless block_given?

  delegatee = Delegatee.instance_for(self).with(&block)
  delegatee.dsl_data[:update_only] = true

  return if context == :method

  # Save instance to reuse when actual scope is set
  Delegatee.extension_data[:class] = delegatee
  Delegatee.instance_reset
end

#method_added(method_name) ⇒ Object



602
603
604
605
606
607
608
609
610
611
# File 'lib/apipie_dsl/dsl.rb', line 602

def method_added(method_name)
  super
  # Methods autoload is not supported for extension modules
  return if Delegatee.instance.nil?

  dsl_data = Delegatee.instance.eval_dsl_for(:method)
  Delegatee.extension_data[:methods] << [method_name, dsl_data]
ensure
  Delegatee.instance_reset
end

#prepended(klass) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/apipie_dsl/dsl.rb', line 574

def prepended(klass)
  super
  Delegatee.extension_data[:class]&.class_scope = klass
  Delegatee.extension_data[:class]&.eval_dsl_for(:class)
  Delegatee.extension_data[:methods].each do |method_name, dsl_data|
    class_scope = klass
    if dsl_data[:update_only]
      class_name = ApipieDSL.get_class_name(class_scope)
      # Update the old method description
      method_desc = ApipieDSL.get_method_description(class_name, method_name)
      unless method_desc
        raise StandardError, "Could not find method description for #{class_name}##{method_name}. Was the method defined?"
      end

      Delegatee.update_method_desc(method_desc, dsl_data)
      # Define validators for the new method
      class_scope = self
    else
      ApipieDSL.remove_method_description(class_scope, dsl_data[:dsl_versions], method_name)
      method_desc = ApipieDSL.define_method_description(class_scope, method_name, dsl_data)
    end
    Delegatee.instance_reset
    Delegatee.define_validators(class_scope, method_desc)
  end
ensure
  Delegatee.instance_reset
end