# = behavng.rb
#
# CREDIT Nobuyoshi Nakada

class Behavior < Module
    def define(behavior, &body)
    if body
      define_method(behavior, &body)
    else
      behavior.each do |behavior, body|
        if body
          define_method(behavior, &body)
        elsif body.nil?
          remove_method(behavior)
        else
          undef_method(behavior)
        end
      end
    end
  end
end

module Kernel
  def behaving(behavior, &body)
    unless @_behaviors
      extend(@_behaviors = Behavior.new)
    end
    @_behaviors.define(behavior, &body)
  end
end


# OLD IMPLEMENTATION

=begin
class Behavior < Module
    def initialize(behavior, &body)
    if body
      define_method(behavior, &body)
    else
      behavior.each do |behavior, body|
        if body
          define_method(behavior, &body)
        elsif body.nil?
          remove_method(behavior)
        else
          undef_method(behavior)
        end
      end
    end
  end
end

module Kernel
  def behaving(behavior, &body)
    extend(Behavior.new(behavior, &body))
  end
end
=end
