Sha256: a5dbd14868cefc41278ee199a33cb773808b82a547d6ddade57c6a5af42ff17f

Contents?: true

Size: 1.22 KB

Versions: 59

Compression:

Stored size: 1.22 KB

Contents

module Tins::Annotate
  def annotate(name)
    singleton_class.class_eval do
      define_method(name) do |annotation = :annotated|
        instance_variable_set "@__annotation_#{name}__", annotation
      end

      define_method("#{name}_of") do |method_name|
        __send__("#{name}_annotations")[method_name]
      end

      define_method("#{name}_annotations") do
        if instance_variable_defined?("@__annotation_#{name}_annotations__")
          instance_variable_get "@__annotation_#{name}_annotations__"
        else
          instance_variable_set "@__annotation_#{name}_annotations__", {}
        end
      end

      old_method_added = instance_method(:method_added)
      define_method(:method_added) do |method_name|
        old_method_added.bind(self).call method_name
        if annotation = instance_variable_get("@__annotation_#{name}__")
          __send__("#{name}_annotations")[method_name] = annotation
        end
        instance_variable_set "@__annotation_#{name}__", nil
      end
    end

    define_method("#{name}_annotations") do
      self.class.__send__("#{name}_annotations")
    end

    define_method("#{name}_of") do |method_name|
      self.class.__send__("#{name}_of", method_name)
    end
  end
end

Version data entries

59 entries across 49 versions & 2 rubygems

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