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.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_not_implemented_info(klass) ⇒ Object

Shortcut to NotImplementedInfoFinder.new.find(klass)



71
72
73
# File 'lib/abstractable.rb', line 71

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)



76
77
78
# File 'lib/abstractable.rb', line 76

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
48
49
# File 'lib/abstractable.rb', line 45

def abstract_methods(all = true)
  return individual_abstract_methods unless all
  collect = lambda { |klass, array| array.push(*klass.abstract_methods(false)) if klass.is_a? Abstractable }
  individual_abstract_methods + (ancestors - [self]).each_with_object([], &collect)
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)


66
67
68
# File 'lib/abstractable.rb', line 66

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.



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

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