Module: ApipieDSL::Module

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

Instance Method Summary collapse

Instance Method Details

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



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/apipie_dsl/dsl.rb', line 465

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

  delegatee.with(options).eval_dsl_for(context)
  Delegatee.instance_reset
end

#apipie_class(name, desc_or_options = nil, options = {}, &block) ⇒ Object



447
448
449
450
451
452
453
# File 'lib/apipie_dsl/dsl.rb', line 447

def apipie_class(name, desc_or_options = nil, options = {}, &block)
  delegatee = prepare_delegatee(self, desc_or_options, options, &block)
  delegatee.name(name)
  delegatee.with(options).eval_dsl_for(:class)

  Delegatee.instance_reset
end

#apipie_method(name, desc_or_options = nil, options = {}, &block) ⇒ Object



455
456
457
458
459
460
461
462
463
# File 'lib/apipie_dsl/dsl.rb', line 455

def apipie_method(name, desc_or_options = nil, options = {}, &block)
  delegatee = prepare_delegatee(self, desc_or_options, options, &block)
  dsl_data = delegatee.eval_dsl_for(:method)
  class_scope = delegatee.class_scope
  ApipieDSL.remove_method_description(class_scope, dsl_data[:dsl_versions], name)
  ApipieDSL.define_method_description(class_scope, name, dsl_data)

  Delegatee.instance_reset
end

#method_added(method_name) ⇒ Object



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/apipie_dsl/dsl.rb', line 485

def method_added(method_name)
  super
  if Delegatee.instance.nil?
    # Don't autoload methods if validations are enabled but no apipie block
    # was called
    return if ApipieDSL.configuration.validate?
    # If no apipie block was called but validations are disabled then
    # it's possible to autoload methods
    return unless ApipieDSL.configuration.autoload_methods?

    apipie
  end

  instance = Delegatee.instance
  class_scope = instance.class_scope
  # Mainly for Rails in case of constant loading within apipie block.
  # Prevents methods, that are being defined in other class than the class
  # where apipie block was called, to be documented with current apipie block
  return unless class_scope == self

  dsl_data = instance.eval_dsl_for(:method)

  ApipieDSL.remove_method_description(class_scope, dsl_data[:dsl_versions], method_name)
  method_desc = ApipieDSL.define_method_description(class_scope, method_name, dsl_data)

  Delegatee.instance_reset
  Delegatee.define_validators(class_scope, method_desc)
ensure
  # Reset if we finished method describing in the right class
  Delegatee.instance_reset if class_scope == self
end