tabl 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -19,16 +19,24 @@ module Tabl
19
19
  @value.call(record)
20
20
  end
21
21
 
22
- def format(key = nil, value = nil, record = nil)
22
+ def format(key = nil, value = nil, record = nil, context = nil)
23
23
  if key.nil?
24
24
  return @format_dsl
25
25
  else
26
26
  format = @formats[key]
27
27
  return format unless value
28
- if (format.arity == 1)
29
- return format.call(value)
28
+ if (context)
29
+ if (format.arity == 1)
30
+ context.instance_exec(value, &format)
31
+ else
32
+ context.instance_exec(value, record, &format)
33
+ end
30
34
  else
31
- return format.call(value, record)
35
+ if (format.arity == 1)
36
+ return format.call(value)
37
+ else
38
+ return format.call(value, record)
39
+ end
32
40
  end
33
41
  end
34
42
  end
@@ -13,12 +13,12 @@ module Tabl
13
13
  @column.value(deref(record))
14
14
  end
15
15
 
16
- def format(format = nil, value = nil, record = nil)
16
+ def format(format = nil, value = nil, record = nil, context = nil)
17
17
  if format.nil?
18
18
  @format_dsl
19
19
  else
20
20
  record = deref(record) if record
21
- @column.format(format, value, record)
21
+ @column.format(format, value, record, context)
22
22
  end
23
23
  end
24
24
 
@@ -83,22 +83,22 @@ module Tabl
83
83
  @base = base
84
84
  end
85
85
 
86
- def values(record)
86
+ def values(record, context = nil)
87
87
  @table.keys.map do |key|
88
88
  value = @table.value(key, record)
89
89
  if value
90
- format(key, value, record)
90
+ format(key, value, record, context)
91
91
  else
92
92
  @base.default_value
93
93
  end
94
94
  end
95
95
  end
96
96
 
97
- def format(key, value, record)
97
+ def format(key, value, record, context)
98
98
  column = @table.column(key)
99
99
 
100
100
  if column.formats[@name]
101
- column.format(@name, value, record)
101
+ column.format(@name, value, record, context)
102
102
  else
103
103
  @base.format(value)
104
104
  end
@@ -3,6 +3,10 @@ require 'lib/user_columns'
3
3
  require 'lib/post_tables'
4
4
 
5
5
  describe PostTables do
6
+ before do
7
+ @view = LinkHelper.new
8
+ end
9
+
6
10
  it 'should return csv' do
7
11
  post = OpenStruct.new(:post => 'foo', :user => OpenStruct.new(:id => 1, :first_name => 'John', :last_name => 'Smith'))
8
12
  PostTables.posts.to_csv([post]).should == <<CSV
@@ -13,7 +17,7 @@ CSV
13
17
 
14
18
  it 'should format values for html' do
15
19
  post = OpenStruct.new(:post => '<>', :user => OpenStruct.new(:key => 1, :first_name => 'John', :last_name => 'Smith'))
16
- PostTables.posts.html.values(post).should == ['&lt;&gt;', "<a href='/user/1'>John Smith</a>"]
20
+ PostTables.posts.html.values(post, @view).should == ['&lt;&gt;', "<a href='/user/1'>John Smith</a>"]
17
21
  end
18
22
  end
19
23
 
@@ -0,0 +1,10 @@
1
+ class LinkHelper
2
+ def self.link_to(text, href)
3
+ "<a href='#{href}'>#{ERB::Util.h(text)}</a>"
4
+ end
5
+
6
+ def link_to(text, href)
7
+ LinkHelper.link_to(text, href)
8
+ end
9
+ end
10
+
@@ -3,7 +3,7 @@ class UserColumns
3
3
 
4
4
  column :user do |column|
5
5
  column.value = lambda { |user| [ user.first_name, user.last_name ].join(' ') }
6
- column.format.html = lambda { |value, user| "<a href='/user/#{user.key}'>#{ERB::Util.h(value)}</a>" }
6
+ column.format.html = lambda { |value, user| link_to value, "/user/#{user.key}" }
7
7
  end
8
8
  end
9
9
 
@@ -1,6 +1,7 @@
1
1
  require 'rspec'
2
2
  require 'tabl'
3
3
  require 'ostruct'
4
+ require 'lib/link_helper'
4
5
 
5
6
  $: << File.join(File.dirname(File.expand_path(__FILE__)), 'lib')
6
7
 
@@ -45,5 +45,13 @@ describe Tabl::Column do
45
45
  column.value(foo).should == 'bar'
46
46
  column.format.html('bar').should == 'BAR'
47
47
  end
48
+
49
+ it 'should evaluate format with a context' do
50
+ foo = OpenStruct.new(:foo => 'foo')
51
+ column = Tabl::Column.new(:foo)
52
+ context = LinkHelper.new
53
+ column.format.html = lambda { |v, foo| link_to 'foo', v }
54
+ column.format.html('foo', foo, context).should == LinkHelper.link_to('foo', 'foo')
55
+ end
48
56
  end
49
57
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tabl}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Liehann Loots}]
12
- s.date = %q{2011-09-17}
12
+ s.date = %q{2011-09-19}
13
13
  s.description = %q{Gem for creating tables in Rails or any other system.}
14
14
  s.email = %q{liehannl@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/tabl/formats/html.rb",
36
36
  "lib/tabl/table.rb",
37
37
  "spec/integration_spec.rb",
38
+ "spec/lib/link_helper.rb",
38
39
  "spec/lib/post_tables.rb",
39
40
  "spec/lib/user_columns.rb",
40
41
  "spec/spec_helper.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Liehann Loots
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-17 00:00:00 Z
18
+ date: 2011-09-19 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime
@@ -149,6 +149,7 @@ files:
149
149
  - lib/tabl/formats/html.rb
150
150
  - lib/tabl/table.rb
151
151
  - spec/integration_spec.rb
152
+ - spec/lib/link_helper.rb
152
153
  - spec/lib/post_tables.rb
153
154
  - spec/lib/user_columns.rb
154
155
  - spec/spec_helper.rb