Permalink
Browse files

Implement behaves_like accepts blocks; Add specs for that

  • Loading branch information...
1 parent 245a20d commit f6de33a00f217ed3c4c0b4976e7df1ba8727689d @svenyurgensson committed Oct 7, 2012
Showing with 45 additions and 19 deletions.
  1. +29 −6 README.md
  2. +6 −4 lib/penetrator.rb
  3. +1 −1 lib/penetrator/version.rb
  4. +9 −8 spec/concern_spec.rb
View
@@ -70,6 +70,7 @@ File: *app/controllers/traits/crudable_trait.rb*
@_resources ||= resource_class.order(default_order).all
end
end
+
```
File: *app/controllers/accomodations_controller.rb*
@@ -136,13 +137,13 @@ File: *app/models/my_model.rb*
```ruby
class Victim
- behaves_like :CanHaveArgs, 'arg1', 'arg2'
+ behaves_like :CanHaveArgs, 'first', 'second'
end
obj = Victim.new
- obj.arg1 # => 'arg1-chunked!'
- obj.arg2 # => 'arg2-chunked!'
+ obj.first # => first-chunked!
+ obj.second # => second-chunked!
```
@@ -154,7 +155,7 @@ Also you can freely utilize `ClassMethods` internal module as you usually do wit
extend Penetrator::Concern
module ClassMethods
def class_method
- ... add what you want ...
+ # ... add what you want ...
end
end
@@ -172,14 +173,36 @@ You can even extend arbitrary instance of any class with your trait:
extend Penetrator::Concern
def cleanup
- ....
+ # ...
end
end
string_of_dirty_html = "Something <span>dirty</span> and even <marquee>fearing ugly</marquee>"
string_of_dirty_html.behave_like 'html_sanitizer'
- ...
+```
+
+`behave_like` also accepts block which can be used in `included` section.
+I have no idea whom need that, but decided to make it possible.
+
+```ruby
+
+ module HtmlSanitizerTrait
+ included do |*args, block|
+ args.each do |method_name|
+ define_method(method_name) do
+ method_name.to_s
+ end
+ end
+ block.call if block
+ end # included
+
+ class VictimWithBlock
+ behaves_like :HtmlSanitizerTrait, 'cleanup_processor' do
+ class_variable_set(:@@foo, "I'm set")
+ # ... something useful
+ end
+ end
```
View
@@ -18,20 +18,22 @@ def append_features(base)
@_dependencies.each { |dep| base.send(:include, dep) }
super
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
+ return unless instance_variable_defined?("@_included_block")
if @_trait_block
- base.class_exec(*@_trait_args, @_trait_block, &@_included_block) if instance_variable_defined?("@_included_block")
+ base.class_exec(*@_trait_args, @_trait_block, &@_included_block)
else
- base.class_exec(*@_trait_args, &@_included_block) if instance_variable_defined?("@_included_block")
+ base.class_exec(*@_trait_args, &@_included_block)
end
end
end
def extend_object(obj)
super
+ return unless instance_variable_defined?("@_included_block")
if @_trait_block
- (class << obj; self; end).instance_exec(*@_trait_args, @_trait_block, &@_included_block) if instance_variable_defined?("@_included_block")
+ (class << obj; self; end).instance_exec(*@_trait_args, @_trait_block, &@_included_block)
else
- (class << obj; self; end).instance_exec(*@_trait_args, &@_included_block) if instance_variable_defined?("@_included_block")
+ (class << obj; self; end).instance_exec(*@_trait_args, &@_included_block)
end
end
@@ -5,7 +5,7 @@ module Penetrator
module Version
MAJOR = 0
MINOR = 1
- PATCH = 6
+ PATCH = 8
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
#
View
@@ -93,29 +93,30 @@ def instance_method arg
-describe 'behavior when trait utilize arguments and block for i' do
+describe 'behavior when trait utilize arguments and block' do
module WithArgsAndBlockTrait
extend Penetrator::Concern
- included do |*args, &block|
+ included do |*args, block|
args.each do |method_name|
define_method(method_name) do
method_name.to_s
end
end
- yield if block_given?
+ block.call if block
end # included
+
end # WithArgsAndBlockTrait
it 'receive trait block' do
- class Victim
- behaves_like :CanHaveArgs, 'arg1' do
- @victim = "I'm set"
+ class VictimWithBlock
+ behaves_like :WithArgsAndBlock, 'arg1' do
+ class_variable_set(:@@foo, "I'm set")
end
end
- obj = Victim.new
+ obj = VictimWithBlock.new
obj.must_respond_to :arg1
obj.arg1.must_be :==, 'arg1'
- obj.instance_variable_get(:@vivtim).must_be :==, "I'm set"
+ VictimWithBlock.class_variable_get(:@@foo).must_be :==, "I'm set"
end
end

0 comments on commit f6de33a

Please sign in to comment.