Module: Tins::Implement
- Included in:
- Module
- Defined in:
- lib/tins/implement.rb
Overview
Provides methods for defining abstract method implementations that raise NotImplementedError. Useful for creating interface-like modules or base classes where certain methods must be implemented by subclasses.
Constant Summary collapse
- MESSAGES =
Predefined error message templates for different implementation contexts.
These templates provide context-specific error messages that help developers understand where and how methods should be implemented.
{ default: 'method %{method_name} not implemented in module %{module}', subclass: 'method %{method_name} has to be implemented in '\ 'subclasses of %{module}', submodule: 'method %{method_name} has to be implemented in '\ 'submodules of %{module}', }
Instance Method Summary collapse
-
#implement(method_name, msg = :default) ⇒ Object
Defines an implementation for a method that raises NotImplementedError.
-
#implement_in_submodule(method_name) ⇒ Object
Defines an implementation for a method that raises NotImplementedError specifically for submodule implementations.
Instance Method Details
#implement(method_name, msg = :default) ⇒ Object
Defines an implementation for a method that raises NotImplementedError.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tins/implement.rb', line 53 def implement(method_name, msg = :default) method_name.nil? and return case msg when ::Symbol msg = MESSAGES.fetch(msg) when ::Hash return implement method_name, msg.fetch(:in) end display_method_name = method_name if m = instance_method(method_name) rescue nil m.extend Tins::MethodDescription display_method_name = m.description(style: :name) end begin msg = msg % { method_name: display_method_name, module: self } rescue KeyError end define_method(method_name) do |*| raise ::NotImplementedError, msg end end |
#implement_in_submodule(method_name) ⇒ Object
Defines an implementation for a method that raises NotImplementedError specifically for submodule implementations.
83 84 85 86 87 |
# File 'lib/tins/implement.rb', line 83 def implement_in_submodule(method_name) implement method_name, 'method %{method_name} has to be implemented in submodules of'\ ' %{module}' end |