Module: Ergane::Concerns::Inheritance::ClassMethods

Defined in:
lib/ergane/concerns/inheritance.rb

Instance Method Summary collapse

Instance Method Details

#abstract_class=(value) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/ergane/concerns/inheritance.rb', line 11

def abstract_class=(value)
  @abstract_class = value
  # Unregister from parent's subcommands when marked abstract
  if value && respond_to?(:command_name) && self < Ergane::Command
    parent = superclass
    parent.subcommands.delete(command_name) if parent.respond_to?(:subcommands)
  end
end

#abstract_class?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ergane/concerns/inheritance.rb', line 20

def abstract_class?
  @abstract_class == true
end

#base_classObject

Returns the class descending directly from Ergane::Command, or an abstract class, if any, in the inheritance hierarchy.

If A extends Command, A.base_class returns A. If B < A through some hierarchy, B.base_class returns A. If A is abstract, both B.base_class and C.base_class return B.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ergane/concerns/inheritance.rb', line 30

def base_class
  unless self < Ergane::Command
    raise Ergane::Error, "#{name} doesn't belong in a hierarchy descending from Ergane::Command"
  end

  if superclass == Ergane::Command || superclass.abstract_class?
    self
  else
    superclass.base_class
  end
end