tabl 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/tabl/column.rb +21 -28
- data/lib/tabl/deref_column.rb +11 -1
- data/lib/tabl/table.rb +3 -3
- data/spec/integration_spec.rb +6 -1
- data/spec/lib/user_columns.rb +1 -0
- data/spec/tabl/deref_column_spec.rb +5 -0
- data/spec/tabl/table_spec.rb +7 -0
- data/tabl.gemspec +2 -2
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/tabl/column.rb
CHANGED
@@ -3,12 +3,15 @@ module Tabl
|
|
3
3
|
attr_reader :name
|
4
4
|
attr_accessor :label
|
5
5
|
attr_writer :value
|
6
|
+
attr_reader :formats
|
6
7
|
|
7
8
|
def initialize(name, args = {})
|
8
9
|
@name = name
|
9
10
|
@label = args[:label] || @name.to_s.titleize
|
10
11
|
@value = args[:value] || lambda { |record| record.send(@name) }
|
11
|
-
@formats =
|
12
|
+
@formats = {}
|
13
|
+
@format_dsl = FormatDsl.new(self)
|
14
|
+
|
12
15
|
yield self if block_given?
|
13
16
|
end
|
14
17
|
|
@@ -16,17 +19,23 @@ module Tabl
|
|
16
19
|
@value.call(record)
|
17
20
|
end
|
18
21
|
|
19
|
-
def format(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def format(key = nil, value = nil, record = nil)
|
23
|
+
if key.nil?
|
24
|
+
return @format_dsl
|
25
|
+
else
|
26
|
+
format = @formats[key]
|
27
|
+
return format unless value
|
28
|
+
if (format.arity == 1)
|
29
|
+
return format.call(value)
|
30
|
+
else
|
31
|
+
return format.call(value, record)
|
32
|
+
end
|
26
33
|
end
|
34
|
+
end
|
27
35
|
|
28
|
-
|
29
|
-
|
36
|
+
class FormatDsl
|
37
|
+
def initialize(column)
|
38
|
+
@column = column
|
30
39
|
end
|
31
40
|
|
32
41
|
def method_missing(name, *args)
|
@@ -34,26 +43,10 @@ module Tabl
|
|
34
43
|
assign = (name =~ /=$/)
|
35
44
|
key = name.gsub(/=$/, '').to_sym
|
36
45
|
|
37
|
-
# super unless Tabl.formats.include?(key)
|
38
|
-
|
39
46
|
if (assign)
|
40
|
-
|
47
|
+
@column.formats[key] = *args
|
41
48
|
else
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def write(key, proc)
|
47
|
-
@formats[key] = proc
|
48
|
-
end
|
49
|
-
|
50
|
-
def read(key, value = nil, record = nil)
|
51
|
-
format = @formats[key]
|
52
|
-
return format unless value
|
53
|
-
if (format.arity == 1)
|
54
|
-
return format.call(value)
|
55
|
-
else
|
56
|
-
return format.call(value, record)
|
49
|
+
@column.format(key, *args)
|
57
50
|
end
|
58
51
|
end
|
59
52
|
end
|
data/lib/tabl/deref_column.rb
CHANGED
@@ -6,10 +6,20 @@ module Tabl
|
|
6
6
|
@column = column
|
7
7
|
@callback = deref
|
8
8
|
@callback = lambda { |record| record.send(deref) } if Symbol === deref
|
9
|
+
@format_dsl = Column::FormatDsl.new(self)
|
9
10
|
end
|
10
11
|
|
11
12
|
def value(record)
|
12
|
-
|
13
|
+
@column.value(deref(record))
|
14
|
+
end
|
15
|
+
|
16
|
+
def format(format = nil, value = nil, record = nil)
|
17
|
+
if format.nil?
|
18
|
+
@format_dsl
|
19
|
+
else
|
20
|
+
record = deref(record) if record
|
21
|
+
@column.format(format, value, record)
|
22
|
+
end
|
13
23
|
end
|
14
24
|
|
15
25
|
def deref(record)
|
data/lib/tabl/table.rb
CHANGED
@@ -89,7 +89,7 @@ module Tabl
|
|
89
89
|
if value
|
90
90
|
format(key, value, record)
|
91
91
|
else
|
92
|
-
@base.default_value
|
92
|
+
@base.default_value
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
@@ -97,8 +97,8 @@ module Tabl
|
|
97
97
|
def format(key, value, record)
|
98
98
|
column = @table.column(key)
|
99
99
|
|
100
|
-
if column.
|
101
|
-
column.format
|
100
|
+
if column.formats[@name]
|
101
|
+
column.format(@name, value, record)
|
102
102
|
else
|
103
103
|
@base.format(value)
|
104
104
|
end
|
data/spec/integration_spec.rb
CHANGED
@@ -4,12 +4,17 @@ require 'lib/post_tables'
|
|
4
4
|
|
5
5
|
describe PostTables do
|
6
6
|
it 'should return csv' do
|
7
|
-
post = OpenStruct.new(:post => 'foo', :user => OpenStruct.new(:first_name => 'John', :last_name => 'Smith'))
|
7
|
+
post = OpenStruct.new(:post => 'foo', :user => OpenStruct.new(:id => 1, :first_name => 'John', :last_name => 'Smith'))
|
8
8
|
PostTables.posts.to_csv([post]).should == <<CSV
|
9
9
|
Post,User
|
10
10
|
foo,John Smith
|
11
11
|
CSV
|
12
12
|
end
|
13
|
+
|
14
|
+
it 'should format values for html' do
|
15
|
+
post = OpenStruct.new(:post => '<>', :user => OpenStruct.new(:key => 1, :first_name => 'John', :last_name => 'Smith'))
|
16
|
+
PostTables.posts.html.values(post).should == ['<>', "<a href='/user/1'>John Smith</a>"]
|
17
|
+
end
|
13
18
|
end
|
14
19
|
|
15
20
|
|
data/spec/lib/user_columns.rb
CHANGED
@@ -11,6 +11,11 @@ describe Tabl::DerefColumn do
|
|
11
11
|
@deref_column.value(@bar).should == 'foo'
|
12
12
|
end
|
13
13
|
|
14
|
+
it 'it should dereference the object when formatting' do
|
15
|
+
@column.format.html = lambda { |value, record| record.foo.upcase }
|
16
|
+
@deref_column.format.html('foo', @bar).should == 'FOO'
|
17
|
+
end
|
18
|
+
|
14
19
|
it 'should delegate the label' do
|
15
20
|
@column.label = 'FOO'
|
16
21
|
@deref_column.label.should == 'FOO'
|
data/spec/tabl/table_spec.rb
CHANGED
@@ -47,6 +47,13 @@ describe Tabl::Table do
|
|
47
47
|
foo = OpenStruct.new(:foo => OpenStruct.new(:bar => 'bar'))
|
48
48
|
table.values(foo).should == ['bar']
|
49
49
|
end
|
50
|
+
|
51
|
+
it 'should format dereferenced columns' do
|
52
|
+
foo_column = Tabl::DerefColumn.new(Tabl::Column.new(:foo), :bar)
|
53
|
+
table = Tabl::Table.new(:base_columns => [foo_column], :columns => [:foo])
|
54
|
+
bar = OpenStruct.new(:bar => OpenStruct.new(:foo => 'foo'))
|
55
|
+
table.html.values(bar).should == ['foo']
|
56
|
+
end
|
50
57
|
end
|
51
58
|
|
52
59
|
|
data/tabl.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{tabl}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
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-
|
12
|
+
s.date = %q{2011-09-17}
|
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 = [
|
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:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
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-
|
18
|
+
date: 2011-09-17 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
type: :runtime
|