Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Using instance_exec to pass object with instance_eval

  • Loading branch information...
commit 77ca4b2b52360709f6cdcb37a046fec887d3ea7a 1 parent 5d0805c
Joe Fiorini authored
View
8 lib/core_ext/object.rb
@@ -0,0 +1,8 @@
+class Object
+ unless defined? instance_exec # 1.9
+ def instance_exec(*arguments, &block)
+ block.bind(self)[*arguments]
+ end
+ end
+end
+
View
13 lib/core_ext/proc.rb
@@ -0,0 +1,13 @@
+class Proc #:nodoc:
+ def bind(object)
+ block, time = self, Time.now
+ (class << object; self end).class_eval do
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
+ define_method(method_name, &block)
+ method = instance_method(method_name)
+ remove_method(method_name)
+ method
+ end.bind(object)
+ end
+end
+
View
5 lib/totally_tabular/column.rb
@@ -1,11 +1,12 @@
module TotallyTabular
class Column
- attr_accessor :name, :template, :header_attributes, :row_attributes
+ attr_accessor :name, :template, :header_attributes, :row_attributes, :definition
- def initialize(name)
+ def initialize(name, &block)
@name = name
@header_attributes = {}
@row_attributes = {}
+ @definition = block
end
def template!(&block)
View
3  lib/totally_tabular/table.rb
@@ -7,8 +7,7 @@ def initialize
end
def define_column(name, header_attributes={}, &block)
- column = Column.new(name)
- column.instance_eval(&block)
+ column = Column.new(name, &block)
@columns << column
end
View
3  lib/totally_tabular/table_view.rb
@@ -16,12 +16,12 @@ def initialize(collection, options={}, &block)
def render
table = Table.new
table.instance_eval(&@column_definition)
+ rows = build_rows(@collection, table.columns)
headers = if @collection.empty?
""
else
build_headers(table.columns)
end
- rows = build_rows(@collection, table.columns)
tbody = @helper.content_tag(:tbody, rows.join)
thead = @helper.content_tag(:thead, headers)
tfoot = @helper.content_tag(:tfoot)
@@ -39,6 +39,7 @@ def build_rows(collection, columns)
row = Row.new
row_attributes = nil
row.render(columns.map do |column|
+ column.instance_exec(item, &column.definition)
row.attributes!(column.row_attributes)
@helper.content_tag(:td, column.template.call(item, row))
end.join)
View
16 spec/lib/core_ext/instance_exec_spec.rb
@@ -0,0 +1,16 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
+
+describe "instance_exec" do
+
+ it "should have instance_exec method on object" do
+ Object.new.should respond_to(:instance_exec)
+ end
+
+ it "should provide arguments in a block" do
+ the_object = Object.new
+ final_object = nil
+ b = Proc.new { |o| final_object = o }
+ Object.new.instance_exec(the_object, &b)
+ final_object.should == the_object
+ end
+end
View
13 spec/lib/totally_tabular/table_view_spec.rb
@@ -116,6 +116,19 @@ def selector(string, selector)
selector(t.render, "table tr.diddy").length.should == 1
end
+ it "allows defining a class from an object attribute" do
+ obj = OpenStruct.new(:status => 'unread')
+ t = TableView.new([obj]) do
+ define_column("Blah") do |o|
+ row_attributes!(:class => o.status)
+ template! do
+ end
+ end
+ end
+
+ selector(t.render, "table tr.unread").length.should == 1
+ end
+
it "should take a block and give a table class" do
table = TableView::Table.new
TableView::Table.stub!(:new).and_return(table)

0 comments on commit 77ca4b2

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