lib/shared.rb in shared-0.4.4 vs lib/shared.rb in shared-1.0.0

- old
+ new

@@ -1,63 +1,74 @@ -module Shared - Shared::VERSION = '0.4.4' unless defined?(Shared::VERSION) - def version() Shared::VERSION end +unless defined?(Shared) + module Shared + Shared::VERSION = '1.0.0' unless defined?(Shared::VERSION) + def version() Shared::VERSION end - Code = {} + Code = {} - def shared name, &block - key = key_for name + def shared name, &block + key = key_for name - return Code[key] if block.nil? + return Code[key] if block.nil? - m = (Code[key] || Module.new) + m = (Code[key] || Module.new) - singleton_class(m) do - unless defined?(@@blocks) - @@blocks = [] + singleton_class(m) do + unless m.respond_to?(:blocks) + blocks = [] + define_method(:blocks){ blocks } - define_method(:blocks){ @@blocks } + define_method(:included) do |other| + blocks.each{|b| other.send(:module_eval, &b)} + end - define_method(:included) do |other| - blocks.each{|b| other.send(:module_eval, &b)} - end - - define_method(:extend_object) do |other| - Shared.singleton_class(other) do - m.blocks.each{|b| module_eval &b} + define_method(:extend_object) do |other| + Shared.singleton_class(other) do + m.blocks.each{|b| module_eval &b} + end end end end + + m.blocks << block + + Code[key] ||= m end + + alias_method 'share', 'shared' + alias_method 'for', 'shared' + alias_method '[]', 'shared' - m.blocks << block + def key_for name + name.to_s.strip.downcase + end - Code[key] ||= m - end - - alias_method 'share', 'shared' + def singleton_class object, &block + singleton_class = + class << object + self + end + block ? singleton_class.module_eval(&block) : singleton_class + end - def key_for name - name.to_s.strip.downcase + extend self end - def singleton_class object, &block - singleton_class = - class << object - self - end - block ? singleton_class.module_eval(&block) : singleton_class - end + module Kernel + private + def share(*a, &b) + Shared.share(*a, &b) + end - extend self -end + def Share(*a, &b) + Shared.share(*a, &b) + end -module Kernel -private - def share *a, &b - Shared.share *a, &b - end + def shared(*a, &b) + Shared.shared(*a, &b) + end - def shared *a, &b - Shared.shared *a, &b + def Shared(*a, &b) + Shared.shared(*a, &b) + end end end