Sha256: 682e48b8873db981c649a5b68bd9ad1b131772fc2fa1103da0a9a010196733fb

Contents?: true

Size: 1.12 KB

Versions: 9

Compression:

Stored size: 1.12 KB

Contents

# this file gona be gem!
# 
# TODO:
# use Modules.call as a default method, instead of using used do


class MetaModule < Module
  # use .new so I don't bother user to use super in #initialize
  def self.new(*)
    super.tap { |x| x.send :include, self::Methods }
  end

  def self.used &block
    define_method :included, &block
    define_method :extended, &block
  end
end



class MetaModule2 #< Class
  def self.new *params
    _params = params.join ?,
    a_params = params.map{|x|"@#{x}"}.join ?,

    Class.new(MetaModule) do
      eval "def initialize(#{_params}); #{a_params} = #{_params} end"
      private; attr_reader *params
    end
  end
end


class My2 < MetaModule2.new :x, :y
  used do |target|
    target.check x
    target.check y
    target.check [x,y]
  end
  
  module Methods
    def check given
      p given
    end
  end
end

# module Context
#   extend My2.new 1, 2
# end

# class My < MetaModule
#   def initialize name
#     @name = name
#   end

#   used do |at|
#     at.abc @name
#   end

#   module Methods
#     def abc given; p given end
#   end
# end

# module A
#   extend My.new 123
# end
# include My.new 234

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
real_zip-1.1.8 spec/support/meta_module.rb
real_zip-1.1.7 spec/support/meta_module.rb
real_zip-1.1.6 spec/support/meta_module.rb
real_zip-1.0.1 spec/support/meta_module.rb
real_zip-1.0.0 spec/support/meta_module.rb
real_zip-0.2.0 spec/support/meta_module.rb
real_zip-0.0.8 spec/support/meta_module.rb
fake_zip-0.0.2 spec/support/meta_module.rb
fake_zip-0.0.1 spec/support/meta_module.rb