Sha256: ffe1a681a2b8e86be300487d670f6bbb6a8a20c73c216901b2e54b87317ea2a9

Contents?: true

Size: 1.14 KB

Versions: 116

Compression:

Stored size: 1.14 KB

Contents

module Tins
  module Implement
    MESSAGES = {
      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}',
    }

    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

    def implement_in_submodule(method_name)
      implement method_name,
        'method %{method_name} has to be implemented in submodules of'\
        ' %{module}'
    end
  end
end

Version data entries

116 entries across 102 versions & 8 rubygems

Version Path
tins-1.38.0 lib/tins/implement.rb
tins-1.37.1 lib/tins/implement.rb
tins-1.37.0 lib/tins/implement.rb
tins-1.36.1 lib/tins/implement.rb
tins-1.36.0 lib/tins/implement.rb
tins-1.35.0 lib/tins/implement.rb
tins-1.34.0 lib/tins/implement.rb
tins-1.33.0 lib/tins/implement.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/implement.rb
tins-1.32.1 lib/tins/implement.rb
tins-1.32.0 lib/tins/implement.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/implement.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/implement.rb
tins-1.31.1 lib/tins/implement.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/tins-1.31.0/lib/tins/implement.rb
tins-1.31.0 lib/tins/implement.rb
tins-1.30.0 lib/tins/implement.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/implement.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/implement.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/implement.rb