Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 26 additions & 10 deletions lib/superators/macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -115,4 +131,4 @@ def superator_valid?(operator)

end

module SuperatorFlag;end
module SuperatorFlag;end
2 changes: 1 addition & 1 deletion spec/superator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'lib/superators'
require './lib/superators'

describe "The 'superator' macro" do

Expand Down