diff --git a/Rakefile b/Rakefile index c8f6993..267840d 100644 --- a/Rakefile +++ b/Rakefile @@ -3,25 +3,26 @@ require 'rubygems' require 'hoe' require './lib/superators.rb' -require 'spec/rake/spectask' +require 'rspec/core/rake_task' -Spec::Rake::SpecTask.new do |opts| - opts.spec_opts = %w'-c' +RSpec::Core::RakeTask.new do |opts| + opts.rspec_opts = %w'-c' end desc "Generate a HTML report of the RSpec specs" -Spec::Rake::SpecTask.new "report" do |opts| - opts.spec_opts = %w'--format html:report.html' +RSpec::Core::RakeTask.new "report" do |opts| + opts.rspec_opts = %w'--format html:report.html' end -Hoe.new('superators', Superators::VERSION) do |p| - p.rubyforge_name = 'superators' - p.author = 'Jay Phillips' - p.email = 'jay -at- codemecca.com' - p.summary = 'Superators add new sexy operators to your Ruby objects.' - p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n") - p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1] - p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n") +Hoe.spec 'superators' do + self.version = Superators::VERSION + self.rubyforge_name = 'superators' + self.author = 'Jay Phillips' + self.email = 'jay -at- codemecca.com' + self.summary = 'Superators add new sexy operators to your Ruby objects.' + self.description = paragraphs_of('README.txt', 2..4).join("\n\n") + self.url = paragraphs_of('README.txt', 0).first.split(/\n/)[-1].strip + self.changes = paragraphs_of('History.txt', 0..1).join("\n\n") end # vim: syntax=Ruby diff --git a/lib/superators/macro.rb b/lib/superators/macro.rb index f178433..d81f9b6 100644 --- a/lib/superators/macro.rb +++ b/lib/superators/macro.rb @@ -10,10 +10,23 @@ module SuperatorMixin VALID_SUPERATOR = /^(#{BINARY_OPERATOR_PATTERN})(#{UNARY_OPERATOR_PATTERN_WITHOUT_AT_SIGN})+$/ def superator_send(sup, operand) - if respond_to_superator? sup - __send__ superator_definition_name_for(sup), operand - else - raise NoMethodError, "Superator #{sup} has not been defined on #{self.class}" + meth = method(superator_definition_name_for(sup)) + begin + # If the user supplied a block that doesn't take any arguments, Ruby 1.9 + # objects if we try to pass it an argument + if meth.arity.zero? + meth.call + else + meth.call(operand) + end + rescue NoMethodError + # Checking for respond_to_superator? is relatively slow, so only do this + # if calling the superator didn't work out as expected + if not respond_to_superator? sup + raise NoMethodError, "Superator #{sup} has not been defined on #{self.class}" + else + raise + end end end @@ -22,7 +35,7 @@ def respond_to_superator?(sup) end def defined_superators - methods.grep(/^superator_definition_/).map { |m| superator_decode(m) } + methods.grep(/^superator_definition_/).map { |m| superator_decode(m.to_s) } end protected @@ -36,8 +49,8 @@ def superator(operator, &block) class_eval do # Step in front of the old operator's dispatching. alias_for_real_method = superator_alias_for real_operator - - if instance_methods.include?(real_operator) && !respond_to_superator?(operator) + + if instance_methods.any? {|m| m.to_s == real_operator} && !respond_to_superator?(operator) alias_method alias_for_real_method, real_operator end @@ -86,14 +99,17 @@ def undef_superator(sup) def superator_encode(str) tokenizer = /#{BINARY_OPERATOR_PATTERN}|#{UNARY_OPERATOR_PATTERN_WITHOUT_AT_SIGN}/ - str.scan(tokenizer).map { |op| op.split('').map { |s| s[0] }.join "_" }.join "__" + r = str.scan(tokenizer).map do |op| + op.enum_for(:each_byte).to_a.join "_" + end + r.join "__" end def superator_decode(str) tokens = str.match /^(superator_(definition|alias_for))?((_?\d{2,3})+)((__\d{2,3})+)$/ #puts *tokens if tokens - (tokens[3].split("_" ) + tokens[5].split('__')).reject { |x| x.empty? }.map { |s| s.to_i.chr }.join + (tokens[3].split("_") + tokens[5].split("__")).reject { |x| x.empty? }.map { |s| s.to_i.chr }.join end end @@ -115,4 +131,4 @@ def superator_valid?(operator) end -module SuperatorFlag;end \ No newline at end of file +module SuperatorFlag;end diff --git a/spec/superator_spec.rb b/spec/superator_spec.rb index 1ba7322..b8da2cf 100644 --- a/spec/superator_spec.rb +++ b/spec/superator_spec.rb @@ -1,4 +1,4 @@ -require 'lib/superators' +require './lib/superators' describe "The 'superator' macro" do