Skip to content

Commit

Permalink
Fixed so that the 'Public Static Methods' is displayed in the result …
Browse files Browse the repository at this point in the history
…before the method specifications, refactoring alot
  • Loading branch information
andreasronge committed Aug 20, 2010
1 parent 31ed115 commit ff7e148
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 66 deletions.
10 changes: 0 additions & 10 deletions lib/meta_helper.rb

This file was deleted.

71 changes: 16 additions & 55 deletions lib/rspec-apigen.rb
@@ -1,24 +1,10 @@
require 'meta_helper'
require 'rspec_apigen/meta_helper'
require 'rspec_apigen/static_methods'
require 'rspec_apigen/argument'
require 'rspec_apigen/fixture'

module RSpec::ApiGen

class Fixture
attr_reader :description
attr_reader :create_proc
attr_reader :destroy_proc

def initialize(description, create_proc, destroy_proc = nil)
@description = description
@create_proc = create_proc
@destroy_proc = destroy_proc
end

def create
@create_proc.call
end
end


def add_fixture(name, clazz, description=nil, &block)
def clazz.fixture(name)
@_fixtures[name.to_sym]
Expand All @@ -39,29 +25,6 @@ def clazz.fixture(name)
end


class Argument
attr_reader :name, :description
def initialize(name, description)
@name = name
@description = description
end

def to_s
"Arg #{name}"
end
end

def arg(name, description='')
Argument.new(name, description)
end



def singleton_of(obj)
class << obj; self; end
end


def create_given_obj(given_args)
given_obj = Object.new
given_args.each_pair do |key,value|
Expand Down Expand Up @@ -103,6 +66,7 @@ def execute_given_block(args, given_block)
end

def static_method(method, param, &block)
puts "DEFINE METHOD #{method} on #{self}"
args = param[:args]
context "##{method}", describe_args(args) do

Expand Down Expand Up @@ -151,25 +115,22 @@ def describe_args(args)
"(#{args.collect { |x| x.kind_of?(Argument)? x.name : "#{x}:#{x.class}" }.join(',')})"
end

def static_methods
def static_methods(&block)
clazz = describes
metaclass = class << self
self
end
# TODO It comes in the wrong order
describe "Public Static Methods" do
def_methods = clazz.methods - Object.methods + %w[new]
static_context = StaticMethods.new

# TODO - how do I find which methods was defined on the clazz and not inherited ?
def_methods = clazz.public_methods - Object.methods + %w[new]
current_context = self
puts "DEF METHODS #{def_methods}"
def_methods.each do |meth_name|
# MetaHelper.create_instance_method(self, meth_name) do |*args, &example_group|
metaclass.send(:define_method, meth_name) do |*args, &example_group|
if example_group # UGLY, since we have modified wrong new method
static_method(meth_name, :args => args, &example_group)
else
super
end
MetaHelper.create_instance_method(static_context, meth_name) do |*args, &example_group|
# metaclass.send(:define_method, meth_name) do |*args, &example_group|
current_context.static_method(meth_name, :args => args, &example_group)
end
end
yield
static_context.instance_eval(&block)
end
end

Expand Down
15 changes: 15 additions & 0 deletions lib/rspec_apigen/argument.rb
@@ -0,0 +1,15 @@
module RSpec::ApiGen
class Argument
attr_reader :name, :description

def initialize(name, description)
@name = name
@description = description
end

def to_s
"Arg #{name}"
end
end

end
18 changes: 18 additions & 0 deletions lib/rspec_apigen/fixture.rb
@@ -0,0 +1,18 @@
module RSpec::ApiGen
class Fixture
attr_reader :description
attr_reader :create_proc
attr_reader :destroy_proc

def initialize(description, create_proc, destroy_proc = nil)
@description = description
@create_proc = create_proc
@destroy_proc = destroy_proc
end

def create
@create_proc.call
end
end

end
14 changes: 14 additions & 0 deletions lib/rspec_apigen/meta_helper.rb
@@ -0,0 +1,14 @@
module RSpec::ApiGen
module MetaHelper
def self.singleton_of(obj)
class << obj;
self;
end
end

def self.create_instance_method(obj, name, &block)
singleton_of(obj).send(:define_method, name, &block)
end

end
end
9 changes: 9 additions & 0 deletions lib/rspec_apigen/static_methods.rb
@@ -0,0 +1,9 @@
module RSpec::ApiGen

class StaticMethods
def arg(name, description='')
Argument.new(name, description)
end
end

end
3 changes: 3 additions & 0 deletions spec/account.rb
Expand Up @@ -7,6 +7,9 @@ def initialize(balance=0, currency = 'USD')
# puts "Create account #{balance} #{currency}"
end

def self.sune
puts "SUNE CALLED"
end
def transfer(amount, currency)
# puts "called transfer with #{amount} #{currency}"
TransactionBuilder.new
Expand Down
2 changes: 1 addition & 1 deletion spec/account_spec.rb
Expand Up @@ -3,7 +3,7 @@


describe Account do

# jokka palm

static_methods do
new do
Expand Down

0 comments on commit ff7e148

Please sign in to comment.