Sha256: afee96bc1ba7d301ce326163ddb5d71da36f456536f295efc42309c49809e16f

Contents?: true

Size: 1.2 KB

Versions: 36

Compression:

Stored size: 1.2 KB

Contents

module Tins::Annotate
  def annotate(name)
    singleton_class.class_eval do
      define_method(name) do |annotation|
        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

36 entries across 34 versions & 4 rubygems

Version Path
tdiary-5.0.11 vendor/bundle/gems/tins-1.16.3/lib/tins/annotate.rb
tins-1.18.0 lib/tins/annotate.rb
tins-1.17.0 lib/tins/annotate.rb
tdiary-5.0.9 vendor/bundle/gems/tins-1.16.3/lib/tins/annotate.rb
tdiary-5.0.8 vendor/bundle/gems/tdiary-5.0.7/vendor/bundle/gems/tins-1.16.3/lib/tins/annotate.rb
tdiary-5.0.7 vendor/bundle/gems/tins-1.16.3/lib/tins/annotate.rb
tins-1.16.3 lib/tins/annotate.rb
tins-1.16.2 lib/tins/annotate.rb
tins-1.16.1 lib/tins/annotate.rb
tins-1.16.0 lib/tins/annotate.rb
tins-1.15.1 lib/tins/annotate.rb
tdiary-5.0.6 vendor/bundle/gems/tins-1.15.0/lib/tins/annotate.rb
tins-1.15.0 lib/tins/annotate.rb
tdiary-5.0.5 vendor/bundle/gems/tins-1.13.2/lib/tins/annotate.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/tins-1.13.2/lib/tins/annotate.rb
tdiary-5.0.5 vendor/bundle/gems/tins-1.14.0/lib/tins/annotate.rb
tins-1.14.0 lib/tins/annotate.rb
tins-1.13.3 lib/tins/annotate.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/tins-1.13.2/lib/tins/annotate.rb
tdiary-5.0.4 vendor/bundle/gems/tins-1.13.2/lib/tins/annotate.rb