Module: Abstractable

Defined in:
lib/abstractable.rb,
lib/abstractable/version.rb,
lib/abstractable/pedigree_stream.rb,
lib/abstractable/not_implemented_info_finder.rb

Overview

abstract method support module.

Defined Under Namespace

Classes: NotImplementedInfoFinder, PedigreeStream, SingletonPedigreeStream, WrongOperationError

Constant Summary collapse

VERSION =
"1.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_not_implemented_info(klass) ⇒ Object

Shortcut to NotImplementedInfoFinder.new.find(klass)



79
80
81
# File 'lib/abstractable.rb', line 79

def self.find_not_implemented_info(klass)
  NotImplementedInfoFinder.new.find(klass)
end

.find_not_implemented_info_from_singleton(klass) ⇒ Object

Shortcut to NotImplementedInfoFinder.new.find_from_singleton(klass)



84
85
86
# File 'lib/abstractable.rb', line 84

def self.find_not_implemented_info_from_singleton(klass)
  NotImplementedInfoFinder.new.find_from_singleton(klass)
end

Instance Method Details

#abstract(*names, &block) ⇒ Object

Define abstract methods.

Example:

abstract :execute           # one symbol
abstract :method1, :method2 # multiple symbol
# in block
abstract do
  def method3; end
  def method4; end
end


34
35
36
37
# File 'lib/abstractable.rb', line 34

def abstract(*names, &block)
  add_abstract_methods(*names.compact)
  add_abstract_methods_by_block(&block) if block_given?
end

#abstract_methods(all = true) ⇒ Object

abstract_methods(all=true) -> array

Returns an array containing the names of abstract methods in the receiver. if set true to args include ancestors abstract methods. (default true)



45
46
47
# File 'lib/abstractable.rb', line 45

def abstract_methods(all = true)
  do_abstract_methods(all, self, (ancestors - [self]))
end

#abstract_singleton_methods(all = true) ⇒ Object

abstract_singleton_methods(all=true) -> array

Returns an array containing the names of abstract methods in the receivers singleton class. if set true to args include ancestor singleton classes abstract methods. (default true)



55
56
57
# File 'lib/abstractable.rb', line 55

def abstract_singleton_methods(all = true)
  do_abstract_methods(all, singleton_class, (ancestors - [self]).map(&:singleton_class))
end

#allocate(*args, &block) ⇒ Object

allocate after unimplemented abstract methods validation.



19
20
21
22
# File 'lib/abstractable.rb', line 19

def allocate(*args, &block)
  validate_on_create(:allocate)
  super(*args, &block)
end

#included(base) ⇒ Object



8
9
10
# File 'lib/abstractable.rb', line 8

def included(base)
  base.extend(Abstractable)
end

#new(*args, &block) ⇒ Object

new after unimplemented abstract methods validation.



13
14
15
16
# File 'lib/abstractable.rb', line 13

def new(*args, &block)
  validate_on_create(:new)
  super(*args, &block)
end

#required_validate?Boolean

required_validate? -> true or false

Returns true if required unimplemented abstract methods validation.

if validated or if defined environment variable ABSTRACTABLE_IGNORE_VALIDATE then always return true.

Returns:

  • (Boolean)


74
75
76
# File 'lib/abstractable.rb', line 74

def required_validate?
  !ENV["ABSTRACTABLE_IGNORE_VALIDATE"] && @implemented_abstract_methods != abstract_methods
end

#validate_not_implemented_abstract_methodsObject

Unimplemented abstract methods validation.

if found unimplemented methods then throw NotImplementedError.



62
63
64
65
66
# File 'lib/abstractable.rb', line 62

def validate_not_implemented_abstract_methods
  not_impl_info = Abstractable.find_not_implemented_info(self)
  fail NotImplementedError, build_error_message(not_impl_info) unless not_impl_info.empty?
  @implemented_abstract_methods = abstract_methods
end