Sha256: e8a808a6abe3e6d587e0aaee19e01d726a61e5fb9151c7d81d8ea88fb0815696

Contents?: true

Size: 1.24 KB

Versions: 15

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Contracts
  module MethodDecorators
    def self.extended(klass)
      Engine.apply(klass)
    end

    def inherited(subclass)
      Engine.fetch_from(subclass).set_eigenclass_owner
      super
    end

    def method_added(name)
      MethodHandler.new(name, false, self).handle
      super
    end

    def singleton_method_added(name)
      MethodHandler.new(name, true, self).handle
      super
    end
  end

  class Decorator
    # an attr_accessor for a class variable:
    class << self; attr_accessor :decorators; end

    def self.inherited(klass)
      super
      name = klass.name.gsub(/^./) { |m| m.downcase }

      return if name =~ /^[^A-Za-z_]/ || name =~ /[^0-9A-Za-z_]/

      # the file and line parameters set the text for error messages
      # make a new method that is the name of your decorator.
      # that method accepts random args and a block.
      # inside, `decorate` is called with those params.
      MethodDecorators.module_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
        def #{klass}(*args, &blk)
          ::Contracts::Engine.fetch_from(self).decorate(#{klass}, *args, &blk)
        end
      RUBY_EVAL
    end

    def initialize(klass, method)
      @method = method
    end
  end
end

Version data entries

15 entries across 15 versions & 5 rubygems

Version Path
contracts-0.17.2 lib/contracts/decorators.rb
contracts-0.17.1 lib/contracts/decorators.rb
entitlements-app-1.1.0 lib/contracts-ruby3/lib/contracts/decorators.rb
entitlements-app-1.0.0 lib/contracts-ruby3/lib/contracts/decorators.rb
entitlements-app-0.3.4 lib/contracts-ruby3/lib/contracts/decorators.rb
entitlements-app-0.3.1 lib/contracts-ruby3/lib/contracts/decorators.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/lib/contracts/decorators.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/lib/contracts/decorators.rb
entitlements-app-0.3.0 lib/contracts-ruby3/lib/contracts/decorators.rb
entitlements-0.2.1 lib/contracts-ruby3/lib/contracts/decorators.rb
entitlements-app-0.2.1 lib/contracts-ruby3/lib/contracts/decorators.rb
entitlements-0.2.0 lib/contracts-ruby3/lib/contracts/decorators.rb
entitlements-app-0.2.0 lib/contracts-ruby3/lib/contracts/decorators.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/contracts-0.17/lib/contracts/decorators.rb
contracts-0.17 lib/contracts/decorators.rb