hirb 0.2.10 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ module Hirb::Views::Rails #:nodoc:
2
+ def active_record__base_view(obj)
3
+ {:fields=>get_active_record_fields(obj)}
4
+ end
5
+
6
+ def get_active_record_fields(obj)
7
+ fields = obj.class.column_names.map {|e| e.to_sym }
8
+ # if query used select
9
+ if obj.attributes.keys.sort != obj.class.column_names.sort
10
+ selected_columns = obj.attributes.keys
11
+ sorted_columns = obj.class.column_names.dup.delete_if {|e| !selected_columns.include?(e) }
12
+ sorted_columns += (selected_columns - sorted_columns)
13
+ fields = sorted_columns.map {|e| e.to_sym}
14
+ end
15
+ fields
16
+ end
17
+ end
18
+
19
+ Hirb::DynamicView.add Hirb::Views::Rails, :helper=>:auto_table
@@ -0,0 +1,96 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ module Hirb
4
+ class DynamicViewTest < Test::Unit::TestCase
5
+ def output_expects(output, expects)
6
+ Helpers::ObjectTable.expects(:render).with(output, expects)
7
+ Helpers::AutoTable.render(output)
8
+ end
9
+ after(:all) { Formatter.dynamic_config = {} }
10
+
11
+ context "add" do
12
+ before(:all) { View.load_config }
13
+
14
+ test "raises error if no :helper option" do
15
+ assert_raises(ArgumentError) {
16
+ Hirb.add_dynamic_view 'Blah', {}
17
+ }.message.should =~ /:helper.*required/
18
+ end
19
+
20
+ test "raises error if :helper option not a dynamic_view module" do
21
+ assert_raises(ArgumentError) {
22
+ Hirb.add_dynamic_view('Blah', :helper=>:table) {|obj| }
23
+ }.message.should =~ /:helper.*must/
24
+ end
25
+
26
+ test "raises error if views module not a module" do
27
+ assert_raises(ArgumentError) {
28
+ Hirb.add_dynamic_view 'Blah', :helper=>:auto_table
29
+ }.message.should =~ /must be a module/
30
+ end
31
+
32
+ test "adds a view with block" do
33
+ Hirb.add_dynamic_view('Date', :helper=>:auto_table) do |obj|
34
+ {:fields=>obj.class::DAYNAMES}
35
+ end
36
+ output_expects [Date.new], :fields=>Date::DAYNAMES
37
+ end
38
+
39
+ test "when adding views with a block, second view for same class overrides first one" do
40
+ Hirb.add_dynamic_view('Date', :helper=>:auto_table) do |obj|
41
+ {:fields=>obj.class::DAYNAMES}
42
+ end
43
+ Hirb.add_dynamic_view('Date', :helper=>:auto_table) do |obj|
44
+ {:fields=>[:blah]}
45
+ end
46
+ output_expects [Date.new], :fields=>[:blah]
47
+ end
48
+ end
49
+
50
+ test "class_to_method and method_to_class convert to each other" do
51
+ ["DBI::Row", "Hirb::View"].each do |e|
52
+ Helpers::AutoTable.method_to_class(DynamicView.class_to_method(e).downcase).should == e
53
+ end
54
+ end
55
+
56
+ context "dynamic_view" do
57
+ def define_view(mod_name= :Blah, &block)
58
+ mod = Views.const_set(mod_name, Module.new)
59
+ mod_block = block_given? ? block : lambda {|obj| {:fields=>obj.class::DAYNAMES}}
60
+ mod.send(:define_method, :date_view, mod_block)
61
+ Hirb.add_dynamic_view mod, :helper=>:auto_table
62
+ end
63
+
64
+ before(:all) { View.load_config }
65
+ before(:each) { Formatter.dynamic_config = {} }
66
+ after(:each) { Views.send(:remove_const, :Blah) }
67
+ after(:all) { reset_config }
68
+
69
+ test "sets a view's options" do
70
+ define_view
71
+ output_expects [Date.new], :fields=>Date::DAYNAMES
72
+ end
73
+
74
+ test "does override existing formatter dynamic_config" do
75
+ Formatter.dynamic_config["Date"] = {:class=>Helpers::Table}
76
+ define_view
77
+ Formatter.dynamic_config["Date"].should == {:class=>Hirb::Helpers::AutoTable, :ancestor=>true}
78
+ end
79
+
80
+ test "raises a readable error when error occurs in a view" do
81
+ define_view {|obj| raise 'blah' }
82
+ assert_raises(RuntimeError) {
83
+ Helpers::AutoTable.render([Date.new])
84
+ }.message.should =~ /'Date'.*date_view.*\nblah/
85
+ end
86
+
87
+ test "another view can reuse an old view's options" do
88
+ define_view
89
+ define_view(:Blah2) do |obj|
90
+ {:fields=>obj.class::DAYNAMES + ['blah']}
91
+ end
92
+ output_expects [Date.new], :fields=>(Date::DAYNAMES + ['blah'])
93
+ end
94
+ end
95
+ end
96
+ end
@@ -2,14 +2,12 @@ require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  module Hirb
4
4
  class FormatterTest < Test::Unit::TestCase
5
- context "formatter" do
6
- def set_formatter(hash={})
7
- @formatter = Formatter.new(hash)
8
- end
9
-
10
- before(:all) { eval "module ::Dooda; end" }
5
+ def set_formatter(hash={})
6
+ @formatter = Formatter.new(hash)
7
+ end
11
8
 
12
- test "klass_config recursively merges ancestor options" do
9
+ context "klass_config" do
10
+ test "recursively merges ancestor options" do
13
11
  set_formatter "String"=>{:args=>[1,2], :options=>{:fields=>[:to_s]}},
14
12
  "Object"=>{:method=>:object_output, :ancestor=>true, :options=>{:vertical=>true}},
15
13
  "Kernel"=>{:method=>:default_output}
@@ -17,33 +15,50 @@ class FormatterTest < Test::Unit::TestCase
17
15
  @formatter.klass_config(::String).should == expected_result
18
16
  end
19
17
 
20
- test "klass_config doesn't merge ancestor options" do
18
+ test "doesn't merge ancestor options" do
21
19
  set_formatter "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output}, "Kernel"=>{:method=>:default_output}
22
- expected_result = {:args=>[1, 2]}
23
- @formatter.klass_config(::String).should == expected_result
20
+ @formatter.klass_config(::String).should == {:args=>[1, 2]}
24
21
  end
25
22
 
26
- test "klass_config returns hash when nothing found" do
23
+ test "returns hash when nothing found" do
27
24
  set_formatter.klass_config(::String).should == {}
28
25
  end
29
26
 
30
- test "reload detects new Hirb::Views" do
31
- set_formatter
32
- @formatter.config.keys.include?('Zzz').should be(false)
33
- eval "module ::Hirb::Views::Zzz; def self.render; end; end"
34
- @formatter.reload
35
- @formatter.config.keys.include?('Zzz').should be(true)
27
+ context "with dynamic_config" do
28
+ after(:each) { Formatter.dynamic_config = {}}
29
+
30
+ test "merges ancestor options and sets local config" do
31
+ Formatter.dynamic_config = {"Object"=>{:method=>:blah}, "Kernel"=>{:args=>[1,2], :ancestor=>true}}
32
+ set_formatter.klass_config(::String).should == {:args=>[1,2], :ancestor=>true}
33
+ @formatter.config['Kernel'].should == {:args=>[1,2], :ancestor=>true}
34
+ end
35
+
36
+ test "uses local config over dynamic_config" do
37
+ Formatter.dynamic_config = {"String"=>{:method=>:blah}}
38
+ set_formatter "String"=>{:args=>[1,2]}
39
+ @formatter.klass_config(::String).should == {:args=>[1,2]}
40
+ end
41
+
42
+ test "uses dynamic_config and sets local config" do
43
+ Formatter.dynamic_config = {"String"=>{:method=>:blah}}
44
+ set_formatter.klass_config(::String).should == {:method=>:blah}
45
+ @formatter.config['String'].should == {:method=>:blah}
46
+ end
36
47
  end
48
+ end
49
+
50
+ context "formatter methods:" do
51
+ before(:all) { eval "module ::Dooda; end" }
37
52
 
38
- test "format_class sets formatter config" do
53
+ test "add_view sets formatter config" do
39
54
  set_formatter
40
- @formatter.format_class ::Dooda, :class=>"DoodaView"
55
+ @formatter.add_view ::Dooda, :class=>"DoodaView"
41
56
  @formatter.klass_config(::Dooda).should == {:class=>"DoodaView"}
42
57
  end
43
58
 
44
- test "format_class overwrites existing formatter config" do
59
+ test "add_view overwrites existing formatter config" do
45
60
  set_formatter "Dooda"=>{:class=>"DoodaView"}
46
- @formatter.format_class ::Dooda, :class=>"DoodaView2"
61
+ @formatter.add_view ::Dooda, :class=>"DoodaView2"
47
62
  @formatter.klass_config(::Dooda).should == {:class=>"DoodaView2"}
48
63
  end
49
64
 
@@ -55,33 +70,6 @@ class FormatterTest < Test::Unit::TestCase
55
70
  end
56
71
  end
57
72
 
58
- context "enable" do
59
- before(:each) { View.formatter = nil; reset_config }
60
- after(:each) { Hirb.disable }
61
-
62
- def formatter_config
63
- View.formatter.config
64
- end
65
-
66
- test "sets default formatter config" do
67
- eval "module ::Hirb::Views::Something_Base; def self.render; end; end"
68
- Hirb.enable
69
- formatter_config["Something::Base"].should == {:class=>"Hirb::Views::Something_Base"}
70
- end
71
-
72
- test "sets default formatter config with default_options" do
73
- eval "module ::Hirb::Views::Blah; def self.render; end; def self.default_options; {:ancestor=>true}; end; end"
74
- Hirb.enable
75
- formatter_config["Blah"].should == {:class=>"Hirb::Views::Blah", :ancestor=>true}
76
- end
77
-
78
- test "sets formatter config" do
79
- class_hash = {"Something::Base"=>{:class=>"BlahBlah"}}
80
- Hirb.enable :output=>class_hash
81
- formatter_config['Something::Base'].should == class_hash['Something::Base']
82
- end
83
- end
84
-
85
73
  context "format_output" do
86
74
  def view_output(*args, &block); View.view_output(*args, &block); end
87
75
  def render_method(*args); View.render_method(*args); end
@@ -27,7 +27,17 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
27
27
  +---+---+
28
28
  1 row in set
29
29
  TABLE
30
- table([{:a=>1, :b=>2}], :headers=>nil).should == expected_table
30
+ table([{:a=>1, :b=>2}], :headers=>false).should == expected_table
31
+ end
32
+
33
+ test "with no headers and nil fields renders" do
34
+ expected_table = <<-TABLE.unindent
35
+ +---+---+
36
+ | 1 | |
37
+ +---+---+
38
+ 1 row in set
39
+ TABLE
40
+ table([{:a=>1, :b=>nil}], :headers=>false).should == expected_table
31
41
  end
32
42
 
33
43
  test "with string keys renders" do
@@ -66,6 +76,10 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
66
76
  table([]).should == "0 rows in set"
67
77
  end
68
78
 
79
+ test "with invalid rows raises an argumenterror" do
80
+ assert_raises(ArgumentError) { table(:a=>1) }.message.should =~ /Table must/
81
+ end
82
+
69
83
  test "renders utf8" do
70
84
  expected_table = <<-TABLE.unindent
71
85
  +--------------------+
@@ -151,7 +165,7 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
151
165
 
152
166
  test "fields and number options copies fields option and does not modify it" do
153
167
  options = {:fields=>[:f1], :number=>true}
154
- table({:f1=>1, :f2=>2}, options)
168
+ table([{:f1=>1, :f2=>2}], options)
155
169
  options[:fields].should == [:f1]
156
170
  end
157
171
 
@@ -439,8 +453,31 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
439
453
  table([[1,2],[2,3]], :change_fields=>['name', 'value']).should == expected_table
440
454
  end
441
455
 
442
- test "return_rows option returns rows" do
443
- table([[1,2],[2,3]], :return_rows=>true).should == [{0=>"1", 1=>"2"}, {0=>"2", 1=>"3"}]
456
+ test "change_fields and fields option renders" do
457
+ expected_table = <<-TABLE.unindent
458
+ +------+
459
+ | name |
460
+ +------+
461
+ | 1 |
462
+ | 2 |
463
+ +------+
464
+ 2 rows in set
465
+ TABLE
466
+ table([[1,2],[2,3]], :change_fields=>['name', 'value'], :fields=>['name']).should == expected_table
467
+ end
468
+
469
+ test "invalid fields in change_fields options are ignored" do
470
+ expected_table = <<-TABLE.unindent
471
+ +------+-------+
472
+ | name | value |
473
+ +------+-------+
474
+ | 1 | 2 |
475
+ | 2 | 3 |
476
+ +------+-------+
477
+ 2 rows in set
478
+ TABLE
479
+ table([{:a=>1,:b=>2}, {:a=>2,:b=>3}], :change_fields=>{:a=>'name', :b=>'value', :c=>'time'}).should == expected_table
480
+ table([[1,2],[2,3]], :change_fields=>['name', 'value','time']).should == expected_table
444
481
  end
445
482
 
446
483
  test "filter_any option filters any value" do
@@ -107,6 +107,23 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
107
107
  TREE
108
108
  tree([[0,'0'],[1,'1'],[2,'2'],[2,'3'],[1,'4']], :type=>:number)
109
109
  end
110
+
111
+ test "with multi-line nodes option renders" do
112
+ expected_tree = <<-TREE.unindent
113
+ parent
114
+ +-------+
115
+ | value |
116
+ +-------+
117
+ | 1 |
118
+ | 2 |
119
+ | 3 |
120
+ +-------+
121
+ indented
122
+ stuff
123
+ TREE
124
+ node1 = "+-------+\n| value |\n+-------+\n| 1 |\n| 2 |\n| 3 |\n+-------+"
125
+ tree([ [0, 'parent'],[1, node1],[2, "indented\nstuff"]], :multi_line_nodes=>true)
126
+ end
110
127
  end
111
128
 
112
129
  def mock_node(value, value_method)
@@ -49,6 +49,12 @@ module Hirb
49
49
  { :output=>{klass=>{:class=>:auto_table}} }
50
50
  end
51
51
 
52
+ test "sets formatter config" do
53
+ class_hash = {"Something::Base"=>{:class=>"BlahBlah"}}
54
+ Hirb.enable :output=>class_hash
55
+ View.formatter_config['Something::Base'].should == class_hash['Something::Base']
56
+ end
57
+
52
58
  test "when called multiple times merges configs" do
53
59
  Hirb.config = nil
54
60
  # default config + config_file
@@ -0,0 +1,15 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class Hirb::ViewsTest < Test::Unit::TestCase
4
+ context "activerecord table" do
5
+ test "with no select gets default options" do
6
+ pet = stub(:name=>'rufus', :age=>7, :attributes=>{"name"=>'rufus', 'age'=>7}, :class=>stub(:column_names=>%w{age name}))
7
+ Hirb::Helpers::AutoTable.active_record__base_view(pet).should == {:fields=>[:age, :name]}
8
+ end
9
+
10
+ test "with select gets default options" do
11
+ pet = stub(:name=>'rufus', :age=>7, :attributes=>{'name'=>'rufus'}, :class=>stub(:column_names=>%w{age name}))
12
+ Hirb::Helpers::AutoTable.active_record__base_view(pet).should == {:fields=>[:name]}
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hirb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Gabriel Horner
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-16 00:00:00 -05:00
17
+ date: 2010-03-11 00:00:00 -05:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -27,13 +32,11 @@ files:
27
32
  - LICENSE.txt
28
33
  - README.rdoc
29
34
  - Rakefile
30
- - VERSION.yml
31
35
  - lib/hirb.rb
32
36
  - lib/hirb/console.rb
37
+ - lib/hirb/dynamic_view.rb
33
38
  - lib/hirb/formatter.rb
34
- - lib/hirb/hash_struct.rb
35
39
  - lib/hirb/helpers.rb
36
- - lib/hirb/helpers/active_record_table.rb
37
40
  - lib/hirb/helpers/auto_table.rb
38
41
  - lib/hirb/helpers/object_table.rb
39
42
  - lib/hirb/helpers/parent_child_tree.rb
@@ -47,11 +50,17 @@ files:
47
50
  - lib/hirb/pager.rb
48
51
  - lib/hirb/string.rb
49
52
  - lib/hirb/util.rb
53
+ - lib/hirb/version.rb
50
54
  - lib/hirb/view.rb
51
- - lib/hirb/views/activerecord_base.rb
52
- - test/active_record_table_test.rb
55
+ - lib/hirb/views.rb
56
+ - lib/hirb/views/couch_db.rb
57
+ - lib/hirb/views/misc_db.rb
58
+ - lib/hirb/views/mongo_db.rb
59
+ - lib/hirb/views/orm.rb
60
+ - lib/hirb/views/rails.rb
53
61
  - test/auto_table_test.rb
54
62
  - test/console_test.rb
63
+ - test/dynamic_view_test.rb
55
64
  - test/formatter_test.rb
56
65
  - test/hirb_test.rb
57
66
  - test/import_test.rb
@@ -64,6 +73,7 @@ files:
64
73
  - test/tree_test.rb
65
74
  - test/util_test.rb
66
75
  - test/view_test.rb
76
+ - test/views_test.rb
67
77
  has_rdoc: true
68
78
  homepage: http://tagaholic.me/hirb/
69
79
  licenses: []
@@ -77,25 +87,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
87
  requirements:
78
88
  - - ">="
79
89
  - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
80
92
  version: "0"
81
- version:
82
93
  required_rubygems_version: !ruby/object:Gem::Requirement
83
94
  requirements:
84
95
  - - ">="
85
96
  - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
86
99
  version: "0"
87
- version:
88
100
  requirements: []
89
101
 
90
102
  rubyforge_project: tagaholic
91
- rubygems_version: 1.3.5
103
+ rubygems_version: 1.3.6
92
104
  signing_key:
93
105
  specification_version: 3
94
106
  summary: A mini view framework for console/irb that's easy to use, even while under its influence.
95
107
  test_files:
96
- - test/active_record_table_test.rb
97
108
  - test/auto_table_test.rb
98
109
  - test/console_test.rb
110
+ - test/dynamic_view_test.rb
99
111
  - test/formatter_test.rb
100
112
  - test/hirb_test.rb
101
113
  - test/import_test.rb
@@ -108,3 +120,4 @@ test_files:
108
120
  - test/tree_test.rb
109
121
  - test/util_test.rb
110
122
  - test/view_test.rb
123
+ - test/views_test.rb