Skip to content
Browse files

adding initialize docs + included support + nice warning when not ext…

…ending Concern
  • Loading branch information...
1 parent ee5956b commit 1f002ed6195913f0cf7d2dfb3fbb69cfbe53fdc7 @grosser grosser committed Sep 12, 2009
Showing with 87 additions and 2 deletions.
  1. +24 −1 README.markdown
  2. +1 −1 VERSION
  3. +8 −0 lib/concern.rb
  4. +8 −0 spec/concern_spec.rb
  5. +29 −0 spec/examples/adding.rb
  6. +1 −0 spec/examples/delegate.rb
  7. +1 −0 spec/examples/inline.rb
  8. +15 −0 spec/examples/parent_warning.rb
View
25 README.markdown
@@ -34,7 +34,6 @@ Normal usage:
Delegate usage:
- # a.rb
class A
class Message
def write_message ...
@@ -49,6 +48,30 @@ Delegate usage:
A.new.write_message
A.new.message.read_message
+Adding to concerned:
+ class A
+ class B < Concern
+ def initialize
+ super
+ @something = {}
+ end
+
+ def self.included(base)
+ base.class_eval do
+ test
+ end
+ end
+ end
+
+ def self.test
+ puts "it works"
+ end
+
+ concern 'a/b'
+ end
+
+More examples can be found in [spec/examples](http://github.com/grosser/concern/tree/master/spec/examples)
+
Author
======
[Michael Grosser](http://pragmatig.wordpress.com)
View
2 VERSION
@@ -1 +1 @@
-0.1.0
+0.2.0
View
8 lib/concern.rb
@@ -20,6 +20,10 @@ def self.concern(lib, options={})
klass = eval(klass)
end
+ unless klass.instance_methods.map{|x|x.to_s}.include?('concerned=')
+ raise "A concern must always extend Concern"
+ end
+
# make accessor
accessor = lib.split('/').last.to_sym
eval <<-EOF
@@ -31,6 +35,10 @@ def #{accessor}
end
EOF
+ # call included
+ base = eval(name) #self.class is always Class, but name is class that called concern
+ klass.included(base) if klass.respond_to? :included
+
# delegate methods
if options[:delegate]
methods = if options[:delegate].is_a?(Array)
View
8 spec/concern_spec.rb
@@ -14,4 +14,12 @@
it "can delegate some" do
`ruby spec/examples/picky_delegate.rb`.should == "hello"
end
+
+ it "add to parent" do
+ `ruby spec/examples/adding.rb`.should == "yepworld"
+ end
+
+ it "warns when parent is not Concern" do
+ `ruby spec/examples/parent_warning.rb`.should == "SUCCESS"
+ end
end
View
29 spec/examples/adding.rb
@@ -0,0 +1,29 @@
+$LOAD_PATH << 'lib' << 'spec/examples'
+require 'concern'
+
+class A
+ class B < Concern
+ def initialize
+ super
+ @hello = 'world'
+ end
+
+ def hello
+ @hello
+ end
+
+ def self.included(base)
+ base.class_eval do
+ test
+ end
+ end
+ end
+
+ def self.test
+ print "yep"
+ end
+
+ concern 'a/b'
+end
+
+print A.new.b.hello
View
1 spec/examples/delegate.rb
@@ -1,5 +1,6 @@
$LOAD_PATH << 'lib' << 'spec/examples'
require 'concern'
+
class A
def xxx
'xxx'
View
1 spec/examples/inline.rb
@@ -1,5 +1,6 @@
$LOAD_PATH << 'lib'
require 'concern'
+
class A
class B < Concern
def test
View
15 spec/examples/parent_warning.rb
@@ -0,0 +1,15 @@
+$LOAD_PATH << 'lib' << 'spec/examples'
+require 'concern'
+
+class A
+ class B # < Concern
+ end
+
+ begin
+ concern 'a/b'
+ print 'FAIL'
+ rescue
+ raise unless $!.to_s =~ /extend Concern/
+ print 'SUCCESS'
+ end
+end

0 comments on commit 1f002ed

Please sign in to comment.
Something went wrong with that request. Please try again.