cldwalker-hirb 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -0
- data/README.rdoc +8 -7
- data/VERSION.yml +1 -1
- data/lib/hirb/console.rb +1 -1
- data/lib/hirb/helpers/parent_child_tree.rb +22 -0
- data/lib/hirb/helpers/tree.rb +177 -0
- data/lib/hirb/helpers.rb +1 -1
- data/lib/hirb/util.rb +10 -0
- data/lib/hirb/view.rb +36 -15
- data/test/test_helper.rb +3 -2
- data/test/tree_test.rb +167 -0
- data/test/util_test.rb +6 -0
- data/test/view_test.rb +18 -0
- metadata +5 -2
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -3,15 +3,15 @@
|
|
3
3
|
Hirb currently provides a mini view framework for console applications, designed with irb in mind.
|
4
4
|
Given the output of a console application, it renders a view if there is one configured, based on
|
5
5
|
the output's class. The framework encourages reusing views by letting you package them in classes
|
6
|
-
and associate them with any number of output classes. Hirb comes with
|
7
|
-
Hirb::Helpers::
|
8
|
-
classes.
|
6
|
+
and associate them with any number of output classes. Hirb comes with tree views (see
|
7
|
+
Hirb::Helpers::Tree) and table views (see Hirb::Helpers::Table). By default Hirb displays Rails'
|
8
|
+
model classes as tables.
|
9
9
|
|
10
10
|
== Install
|
11
11
|
|
12
12
|
Install the gem with:
|
13
13
|
|
14
|
-
sudo gem install cldwalker-hirb
|
14
|
+
sudo gem install cldwalker-hirb --source http://gems.github.com
|
15
15
|
|
16
16
|
== Rails Example
|
17
17
|
|
@@ -214,14 +214,15 @@ and colors to work nicely, please fork. To colorize your Hirb output:
|
|
214
214
|
|
215
215
|
== Motivation
|
216
216
|
Table code from http://gist.github.com/72234 and {my console
|
217
|
-
app}[http://github.com/cldwalker/tag-tree].
|
217
|
+
app's needs}[http://github.com/cldwalker/tag-tree].
|
218
218
|
|
219
219
|
== Links
|
220
220
|
* http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html
|
221
|
+
* http://tagaholic.me/2009/03/18/ruby-class-trees-rails-plugin-trees-with-hirb.html
|
221
222
|
|
222
223
|
== Todo
|
223
|
-
* Create tree views.
|
224
|
-
* Possibly add non-view irb goodies ie command manager.
|
225
224
|
* Configurable max height, which if exceeded activates a pager.
|
225
|
+
* Possibly add non-view irb goodies ie command manager.
|
226
|
+
* Consider applying multiple views/filters to output.
|
226
227
|
* Provides helper methods to all view classes.
|
227
228
|
* Consider adding a template system as needed.
|
data/VERSION.yml
CHANGED
data/lib/hirb/console.rb
CHANGED
@@ -9,7 +9,7 @@ module Hirb
|
|
9
9
|
def table(output, options={})
|
10
10
|
Hirb::View.console_render_output(output, options.merge(:class=>"Hirb::Helpers::AutoTable"))
|
11
11
|
end
|
12
|
-
# Renders any specified view for the given object. Takes same options as Hirb::View.
|
12
|
+
# Renders any specified view for the given object. Takes same options as Hirb::View.console_render_output.
|
13
13
|
def view(*args)
|
14
14
|
Hirb::View.console_render_output(*args)
|
15
15
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Hirb::Helpers::ParentChildTree < Hirb::Helpers::Tree
|
2
|
+
class <<self
|
3
|
+
# Starting with the given node, this builds a tree by recursively calling a children method.
|
4
|
+
# Takes same options as Hirb::Helper::Table.render with some additional ones below.
|
5
|
+
# ==== Options:
|
6
|
+
# [:value_method] Method to call to display as a node's value. If not given, uses :name if node
|
7
|
+
# responds to :name or defaults to :object_id.
|
8
|
+
# [:children_method] Method to call to obtain a node's children. Default is :children.
|
9
|
+
def render(root_node, options={})
|
10
|
+
@value_method = options[:value_method] || (root_node.respond_to?(:name) ? :name : :object_id)
|
11
|
+
@children_method = options[:children_method] || :children
|
12
|
+
@nodes = []
|
13
|
+
build_node(root_node, 0)
|
14
|
+
super(@nodes, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def build_node(node, level) #:nodoc:
|
18
|
+
@nodes << {:value=>node.send(@value_method), :level=>level}
|
19
|
+
node.send(@children_method).each {|e| build_node(e, level + 1)}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
# Base tree class which given an array of nodes produces different types of trees.
|
2
|
+
# The types of trees currently are:
|
3
|
+
# * basic:
|
4
|
+
# 0
|
5
|
+
# 1
|
6
|
+
# 2
|
7
|
+
# 3
|
8
|
+
# 4
|
9
|
+
#
|
10
|
+
# * directory:
|
11
|
+
# 0
|
12
|
+
# |-- 1
|
13
|
+
# | |-- 2
|
14
|
+
# | `-- 3
|
15
|
+
# `-- 4
|
16
|
+
#
|
17
|
+
# * number:
|
18
|
+
# 1. 0
|
19
|
+
# 1. 1
|
20
|
+
# 1. 2
|
21
|
+
# 2. 3
|
22
|
+
# 2. 4
|
23
|
+
#
|
24
|
+
# Tree nodes can be given as an array of arrays or an array of hashes.
|
25
|
+
# To render the above basic tree with an array of hashes:
|
26
|
+
# Hirb::Helpers::Tree.render([{:value=>0, :level=>0}, {:value=>1, :level=>1}, {:value=>2, :level=>2},
|
27
|
+
# {:value=>3, :level=>2}, {:value=>4, :level=>1}])
|
28
|
+
# Note from the hash keys that :level refers to the depth of the tree while :value refers to the text displayed
|
29
|
+
# for a node.
|
30
|
+
#
|
31
|
+
# To render the above basic tree with an array of arrays:
|
32
|
+
# Hirb::Helpers::Tree.render([[0,0], [1,1], [2,2], [2,3], [1,4]])
|
33
|
+
# Note that the each array pair consists of the level and the value for the node.
|
34
|
+
class Hirb::Helpers::Tree
|
35
|
+
class ParentlessNodeError < StandardError; end
|
36
|
+
|
37
|
+
class <<self
|
38
|
+
# Main method which renders a tree.
|
39
|
+
# ==== Options:
|
40
|
+
# [:type] Type of tree. Either :basic, :directory or :number. Default is :basic.
|
41
|
+
# [:validate] Boolean to validate tree. Checks to see if all nodes have parents. Raises ParentlessNodeError if
|
42
|
+
# an invalid node is found. Default is false.
|
43
|
+
# [:indent] Number of spaces to indent between levels for basic + number trees. Default is 4.
|
44
|
+
# [:limit] Limits the level or depth of a tree that is displayed. Root node is level 0.
|
45
|
+
# [:description] Displays brief description about tree ie how many nodes it has.
|
46
|
+
# Examples:
|
47
|
+
# Hirb::Helpers::Tree.render([[0, 'root'], [1, 'child']], :type=>:directory)
|
48
|
+
def render(nodes, options={})
|
49
|
+
new(nodes, options).render
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# :stopdoc:
|
54
|
+
attr_accessor :nodes
|
55
|
+
|
56
|
+
def initialize(input_nodes, options={})
|
57
|
+
@options = options
|
58
|
+
@type = options[:type] || :basic
|
59
|
+
if input_nodes[0].is_a?(Array)
|
60
|
+
@nodes = input_nodes.map {|e| Node.new(:level=>e[0], :value=>e[1]) }
|
61
|
+
else
|
62
|
+
@nodes = input_nodes.map {|e| Node.new(e)}
|
63
|
+
end
|
64
|
+
@nodes.each_with_index {|e,i| e.merge!(:tree=>self, :index=>i)}
|
65
|
+
@nodes.each {|e| e[:value] = e[:value].to_s }
|
66
|
+
validate_nodes if options[:validate]
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
def render
|
71
|
+
body = render_tree
|
72
|
+
body += render_description if @options[:description]
|
73
|
+
body
|
74
|
+
end
|
75
|
+
|
76
|
+
def render_description
|
77
|
+
"\n\n#{@nodes.length} #{@nodes.length == 1 ? 'node' : 'nodes'} in tree"
|
78
|
+
end
|
79
|
+
|
80
|
+
def render_tree
|
81
|
+
@indent = ' ' * (@options[:indent] || 4 )
|
82
|
+
@nodes = @nodes.select {|e| e[:level] <= @options[:limit] } if @options[:limit]
|
83
|
+
case @type.to_s
|
84
|
+
when 'directory'
|
85
|
+
render_directory
|
86
|
+
when 'number'
|
87
|
+
render_number
|
88
|
+
else
|
89
|
+
render_basic
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def render_directory
|
94
|
+
mark_last_nodes_per_level
|
95
|
+
new_nodes = []
|
96
|
+
@nodes.each_with_index {|e, i|
|
97
|
+
value = ''
|
98
|
+
unless e.root?
|
99
|
+
value << e.render_parent_characters
|
100
|
+
value << (e[:last_node] ? "`-- " : "|-- ")
|
101
|
+
end
|
102
|
+
value << e[:value]
|
103
|
+
new_nodes << value
|
104
|
+
}
|
105
|
+
new_nodes.join("\n")
|
106
|
+
end
|
107
|
+
|
108
|
+
def render_number
|
109
|
+
counter = {}
|
110
|
+
@nodes.each {|e|
|
111
|
+
parent_level_key = "#{(e.parent ||{})[:index]}.#{e[:level]}"
|
112
|
+
counter[parent_level_key] ||= 0
|
113
|
+
counter[parent_level_key] += 1
|
114
|
+
e[:pre_value] = "#{counter[parent_level_key]}. "
|
115
|
+
}
|
116
|
+
@nodes.map {|e| @indent * e[:level] + e[:pre_value] + e[:value]}.join("\n")
|
117
|
+
end
|
118
|
+
|
119
|
+
def render_basic
|
120
|
+
@nodes.map {|e| @indent * e[:level] + e[:value]}.join("\n")
|
121
|
+
end
|
122
|
+
|
123
|
+
def validate_nodes
|
124
|
+
@nodes.each do |e|
|
125
|
+
raise ParentlessNodeError if (e[:level] > e.previous[:level]) && (e[:level] - e.previous[:level]) > 1
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# walks tree accumulating last nodes per unique parent+level
|
130
|
+
def mark_last_nodes_per_level
|
131
|
+
@nodes.each {|e| e.delete(:last_node)}
|
132
|
+
last_node_hash = @nodes.inject({}) {|h,e|
|
133
|
+
h["#{(e.parent ||{})[:index]}.#{e[:level]}"] = e; h
|
134
|
+
}
|
135
|
+
last_node_hash.values.uniq.each {|e| e[:last_node] = true}
|
136
|
+
end
|
137
|
+
#:startdoc:
|
138
|
+
class Node < ::Hash #:nodoc:
|
139
|
+
class MissingLevelError < StandardError; end
|
140
|
+
class MissingValueError < StandardError; end
|
141
|
+
|
142
|
+
def initialize(hash)
|
143
|
+
super
|
144
|
+
raise MissingLevelError unless hash.has_key?(:level)
|
145
|
+
raise MissingValueError unless hash.has_key?(:value)
|
146
|
+
replace(hash)
|
147
|
+
end
|
148
|
+
|
149
|
+
def parent
|
150
|
+
self[:tree].nodes.slice(0 .. self[:index]).reverse.detect {|e| e[:level] < self[:level]}
|
151
|
+
end
|
152
|
+
|
153
|
+
def next
|
154
|
+
self[:tree].nodes[self[:index] + 1]
|
155
|
+
end
|
156
|
+
|
157
|
+
def previous
|
158
|
+
self[:tree].nodes[self[:index] - 1]
|
159
|
+
end
|
160
|
+
|
161
|
+
def root?; self[:level] == 0; end
|
162
|
+
|
163
|
+
# refers to characters which connect parent nodes
|
164
|
+
def render_parent_characters
|
165
|
+
parent_chars = []
|
166
|
+
get_parents_character(parent_chars)
|
167
|
+
parent_chars.reverse.map {|level| level + ' ' * 3 }.to_s
|
168
|
+
end
|
169
|
+
|
170
|
+
def get_parents_character(parent_chars)
|
171
|
+
if self.parent
|
172
|
+
parent_chars << (self.parent[:last_node] ? ' ' : '|') unless self.parent.root?
|
173
|
+
self.parent.get_parents_character(parent_chars)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
data/lib/hirb/helpers.rb
CHANGED
data/lib/hirb/util.rb
CHANGED
@@ -15,5 +15,15 @@ module Hirb
|
|
15
15
|
nil
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
# Recursively merge hash1 with hash2.
|
20
|
+
def recursive_hash_merge(hash1, hash2)
|
21
|
+
hash1.merge(hash2) {|k,o,n| (o.is_a?(Hash)) ? recursive_hash_merge(o,n) : n}
|
22
|
+
end
|
23
|
+
|
24
|
+
# from Rails ActiveSupport
|
25
|
+
def camelize(string)
|
26
|
+
string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
27
|
+
end
|
18
28
|
end
|
19
29
|
end
|
data/lib/hirb/view.rb
CHANGED
@@ -43,6 +43,7 @@ module Hirb
|
|
43
43
|
# [:method] Specifies a global (Kernel) method to do the formatting.
|
44
44
|
# [:class] Specifies a class to do the formatting, using its render() class method. The render() method's arguments are the output and
|
45
45
|
# an options hash.
|
46
|
+
# [:output_method] Specifies a method to call on output before passing it to a formatter.
|
46
47
|
# [:options] Options to pass the formatter method or class.
|
47
48
|
def render_output(output, options={}, &block)
|
48
49
|
if block && block.arity > 0
|
@@ -70,7 +71,7 @@ module Hirb
|
|
70
71
|
|
71
72
|
# Config hash which maps classes to view hashes. View hashes are the same as the options hash of render_output().
|
72
73
|
def output_config
|
73
|
-
|
74
|
+
config[:output]
|
74
75
|
end
|
75
76
|
|
76
77
|
def output_config=(value)
|
@@ -83,20 +84,45 @@ module Hirb
|
|
83
84
|
current_config = self.config.dup.merge(:output=>output_config)
|
84
85
|
load_config(current_config)
|
85
86
|
end
|
86
|
-
|
87
|
-
|
88
|
-
|
87
|
+
|
88
|
+
# A console version of render_output which takes its same options but allows for some shortcuts.
|
89
|
+
# Examples:
|
90
|
+
# console_render_output output, :tree, :type=>:directory
|
91
|
+
# # is the same as:
|
92
|
+
# render_output output, :class=>"Hirb::Helpers::Tree", :options=> {:type=>:directory}
|
93
|
+
#
|
94
|
+
def console_render_output(*args, &block)
|
95
|
+
load_config unless @config
|
96
|
+
output = args.shift
|
97
|
+
if args[0].is_a?(Symbol) && (view = args.shift)
|
98
|
+
symbol_options = find_view(view)
|
99
|
+
end
|
100
|
+
options = args[-1].is_a?(Hash) ? args[-1] : {}
|
101
|
+
options.merge!(symbol_options) if symbol_options
|
89
102
|
# iterates over format_output options that aren't :options
|
90
|
-
real_options = [:method, :class].inject({}) do |h, e|
|
91
|
-
h[e] = options.delete(e) if options[e]
|
92
|
-
h
|
103
|
+
real_options = [:method, :class, :output_method].inject({}) do |h, e|
|
104
|
+
h[e] = options.delete(e) if options[e]; h
|
93
105
|
end
|
94
106
|
render_output(output, real_options.merge(:options=>options), &block)
|
95
107
|
end
|
96
108
|
|
109
|
+
#:stopdoc:
|
110
|
+
def find_view(name)
|
111
|
+
name = name.to_s
|
112
|
+
if (view_method = output_config.values.find {|e| e[:method] == name })
|
113
|
+
{:method=>view_method[:method]}
|
114
|
+
elsif (view_class = Hirb::Helpers.constants.find {|e| e == Util.camelize(name)})
|
115
|
+
{:class=>"Hirb::Helpers::#{view_class}"}
|
116
|
+
else
|
117
|
+
{}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
97
121
|
def format_output(output, options={})
|
98
122
|
output_class = determine_output_class(output)
|
99
|
-
options = output_class_options(output_class)
|
123
|
+
options = Util.recursive_hash_merge(output_class_options(output_class), options)
|
124
|
+
output = options[:output_method] ? (output.is_a?(Array) ? output.map {|e| e.send(options[:output_method])} :
|
125
|
+
output.send(options[:output_method]) ) : output
|
100
126
|
args = [output]
|
101
127
|
args << options[:options] if options[:options] && !options[:options].empty?
|
102
128
|
if options[:method]
|
@@ -115,11 +141,8 @@ module Hirb
|
|
115
141
|
end
|
116
142
|
end
|
117
143
|
|
118
|
-
# Loads config
|
119
144
|
def load_config(additional_config={})
|
120
|
-
|
121
|
-
new_config[:output].merge!(additional_config.delete(:output) || {})
|
122
|
-
self.config = new_config.merge(additional_config)
|
145
|
+
self.config = Util.recursive_hash_merge default_config, additional_config
|
123
146
|
true
|
124
147
|
end
|
125
148
|
|
@@ -155,9 +178,7 @@ module Hirb
|
|
155
178
|
end
|
156
179
|
|
157
180
|
def default_config
|
158
|
-
|
159
|
-
conf[:output] = default_output_config.merge(conf[:output] || {})
|
160
|
-
conf
|
181
|
+
Hirb::Util.recursive_hash_merge({:output=>default_output_config}, Hirb.config[:view] || {} )
|
161
182
|
end
|
162
183
|
|
163
184
|
def default_output_config
|
data/test/test_helper.rb
CHANGED
data/test/tree_test.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Hirb::Helpers::TreeTest < Test::Unit::TestCase
|
4
|
+
def tree(*args)
|
5
|
+
Hirb::Helpers::Tree.render(*args)
|
6
|
+
end
|
7
|
+
|
8
|
+
context "basic tree" do
|
9
|
+
test "with hash nodes renders" do
|
10
|
+
expected_tree = <<-TREE.unindent(6)
|
11
|
+
0.0
|
12
|
+
1.1
|
13
|
+
2.2
|
14
|
+
3.2
|
15
|
+
4.1
|
16
|
+
TREE
|
17
|
+
tree([{:level=>0, :value=>'0.0'}, {:level=>1, :value=>'1.1'}, {:level=>2, :value=>'2.2'},{:level=>2, :value=>'3.2'},
|
18
|
+
{:level=>1, :value=>'4.1'}]).should == expected_tree
|
19
|
+
end
|
20
|
+
|
21
|
+
test "with array nodes renders" do
|
22
|
+
expected_tree = <<-TREE.unindent(6)
|
23
|
+
0.0
|
24
|
+
1.1
|
25
|
+
2.2
|
26
|
+
3.2
|
27
|
+
4.1
|
28
|
+
TREE
|
29
|
+
tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]]).should == expected_tree
|
30
|
+
end
|
31
|
+
|
32
|
+
test "with non-string values renders" do
|
33
|
+
expected_tree = <<-TREE.unindent(6)
|
34
|
+
0.0
|
35
|
+
1.1
|
36
|
+
2.2
|
37
|
+
3.2
|
38
|
+
4.1
|
39
|
+
TREE
|
40
|
+
tree([[0,0.0],[1,1.1],[2,2.2],[2,3.2],[1,4.1]]).should == expected_tree
|
41
|
+
end
|
42
|
+
|
43
|
+
test "with indent option renders" do
|
44
|
+
expected_tree = <<-TREE.unindent(6)
|
45
|
+
0.0
|
46
|
+
1.1
|
47
|
+
2.2
|
48
|
+
3.2
|
49
|
+
4.1
|
50
|
+
TREE
|
51
|
+
tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :indent=>2).should == expected_tree
|
52
|
+
end
|
53
|
+
|
54
|
+
test "with limit option renders" do
|
55
|
+
expected_tree = <<-TREE.unindent(6)
|
56
|
+
0.0
|
57
|
+
1.1
|
58
|
+
4.1
|
59
|
+
TREE
|
60
|
+
tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :limit=>1).should == expected_tree
|
61
|
+
end
|
62
|
+
|
63
|
+
test "with description option renders" do
|
64
|
+
expected_tree = <<-TREE.unindent(6)
|
65
|
+
0.0
|
66
|
+
1.1
|
67
|
+
2.2
|
68
|
+
3.2
|
69
|
+
4.1
|
70
|
+
|
71
|
+
5 nodes in tree
|
72
|
+
TREE
|
73
|
+
tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :description=>true).should == expected_tree
|
74
|
+
end
|
75
|
+
|
76
|
+
test "with type directory renders" do
|
77
|
+
expected_tree = <<-TREE.unindent
|
78
|
+
0.0
|
79
|
+
|-- 1.1
|
80
|
+
| |-- 2.2
|
81
|
+
| `-- 3.2
|
82
|
+
`-- 4.1
|
83
|
+
TREE
|
84
|
+
tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :type=>:directory).should == expected_tree
|
85
|
+
end
|
86
|
+
|
87
|
+
test "with type directory and multiple children per level renders" do
|
88
|
+
expected_tree = <<-TREE.unindent
|
89
|
+
0.0
|
90
|
+
|-- 1.1
|
91
|
+
| |-- 2.2
|
92
|
+
| | `-- 3.3
|
93
|
+
| `-- 4.2
|
94
|
+
| `-- 5.3
|
95
|
+
`-- 6.1
|
96
|
+
TREE
|
97
|
+
tree([[0,'0.0'], [1,'1.1'], [2,'2.2'],[3,'3.3'],[2,'4.2'],[3,'5.3'],[1,'6.1']], :type=>:directory).should == expected_tree
|
98
|
+
end
|
99
|
+
|
100
|
+
test "with type number renders" do
|
101
|
+
expected_tree = <<-TREE.unindent(6)
|
102
|
+
1. 0
|
103
|
+
1. 1
|
104
|
+
1. 2
|
105
|
+
2. 3
|
106
|
+
2. 4
|
107
|
+
TREE
|
108
|
+
tree([[0,'0'],[1,'1'],[2,'2'],[2,'3'],[1,'4']], :type=>:number)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def mock_node(value, value_method)
|
113
|
+
children = []
|
114
|
+
value,children = *value if value.is_a?(Array)
|
115
|
+
mock(value_method=>value, :children=>children.map {|e| mock_node(e, value_method)})
|
116
|
+
end
|
117
|
+
|
118
|
+
context "parent_child_tree" do
|
119
|
+
test "with name value renders" do
|
120
|
+
expected_tree = <<-TREE.unindent
|
121
|
+
0.0
|
122
|
+
|-- 1.1
|
123
|
+
|-- 2.1
|
124
|
+
| `-- 3.2
|
125
|
+
`-- 4.1
|
126
|
+
TREE
|
127
|
+
root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :name)
|
128
|
+
Hirb::Helpers::ParentChildTree.render(root, :type=>:directory).should == expected_tree
|
129
|
+
end
|
130
|
+
|
131
|
+
test "with object_id value renders" do
|
132
|
+
expected_tree = <<-TREE.unindent
|
133
|
+
0.0
|
134
|
+
|-- 1.1
|
135
|
+
|-- 2.1
|
136
|
+
| `-- 3.2
|
137
|
+
`-- 4.1
|
138
|
+
TREE
|
139
|
+
root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :object_id)
|
140
|
+
Hirb::Helpers::ParentChildTree.render(root, :type=>:directory).should == expected_tree
|
141
|
+
end
|
142
|
+
|
143
|
+
test "with value_method option renders" do
|
144
|
+
expected_tree = <<-TREE.unindent
|
145
|
+
0.0
|
146
|
+
|-- 1.1
|
147
|
+
|-- 2.1
|
148
|
+
| `-- 3.2
|
149
|
+
`-- 4.1
|
150
|
+
TREE
|
151
|
+
root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :blah)
|
152
|
+
Hirb::Helpers::ParentChildTree.render(root, :type=>:directory, :value_method=>:blah).should == expected_tree
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
test "tree with parentless nodes renders ParentlessNodeError" do
|
157
|
+
assert_raises(Hirb::Helpers::Tree::ParentlessNodeError) { tree([[0, "0.0"], [2, '1.2']], :validate=>true) }
|
158
|
+
end
|
159
|
+
|
160
|
+
test "tree with hash nodes missing level raises MissingLevelError" do
|
161
|
+
assert_raises(Hirb::Helpers::Tree::Node::MissingLevelError) { tree([{:value=>'ok'}]) }
|
162
|
+
end
|
163
|
+
|
164
|
+
test "tree with hash nodes missing level raises MissingValueError" do
|
165
|
+
assert_raises(Hirb::Helpers::Tree::Node::MissingValueError) { tree([{:level=>0}]) }
|
166
|
+
end
|
167
|
+
end
|
data/test/util_test.rb
CHANGED
@@ -12,4 +12,10 @@ class Hirb::UtilTest < Test::Unit::TestCase
|
|
12
12
|
test "any_const_get returns class when given class" do
|
13
13
|
Hirb::Util.any_const_get(String).should == String
|
14
14
|
end
|
15
|
+
|
16
|
+
test "recursive_hash_merge merges" do
|
17
|
+
expected_hash = {:output=>{:fields=>["f1", "f2"], :method=>"blah"}, :key1=>"hash1", :key2=>"hash2"}
|
18
|
+
Hirb::Util.recursive_hash_merge({:output=>{:fields=>%w{f1 f2}}, :key1=>'hash1'},
|
19
|
+
{:output=>{:method=>'blah'}, :key2=>'hash2'}).should == expected_hash
|
20
|
+
end
|
15
21
|
end
|
data/test/view_test.rb
CHANGED
@@ -140,6 +140,18 @@ class Hirb::ViewTest < Test::Unit::TestCase
|
|
140
140
|
Hirb::View.render_output('dude', :class=>"Commify")
|
141
141
|
end
|
142
142
|
|
143
|
+
test "formats with output_method option" do
|
144
|
+
set_config 'String'=>{:class=>"Blahify"}
|
145
|
+
Hirb::View.render_method.expects(:call).with('d,u,d')
|
146
|
+
Hirb::View.render_output('dude', :class=>"Commify", :output_method=>:chop)
|
147
|
+
end
|
148
|
+
|
149
|
+
test "formats output array with output_method option" do
|
150
|
+
set_config 'String'=>{:class=>"Blahify"}
|
151
|
+
Hirb::View.render_method.expects(:call).with("d,u,d\nm,a")
|
152
|
+
Hirb::View.render_output(['dude', 'man'], :class=>"Commify", :output_method=>:chop)
|
153
|
+
end
|
154
|
+
|
143
155
|
test "formats with block" do
|
144
156
|
Hirb::View.load_config
|
145
157
|
Hirb::View.render_method.expects(:call).with('=dude=')
|
@@ -147,5 +159,11 @@ class Hirb::ViewTest < Test::Unit::TestCase
|
|
147
159
|
"=#{output}="
|
148
160
|
}
|
149
161
|
end
|
162
|
+
|
163
|
+
test "console_render_output merge options option" do
|
164
|
+
set_config "String"=>{:class=>"Commify", :options=>{:fields=>%w{f1 f2}}}
|
165
|
+
Commify.expects(:render).with('dude', :max_width=>10, :fields=>%w{f1 f2})
|
166
|
+
Hirb::View.render_output('dude', :options=>{:max_width=>10})
|
167
|
+
end
|
150
168
|
end
|
151
169
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cldwalker-hirb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Horner
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-18 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -35,7 +35,9 @@ files:
|
|
35
35
|
- lib/hirb/helpers/active_record_table.rb
|
36
36
|
- lib/hirb/helpers/auto_table.rb
|
37
37
|
- lib/hirb/helpers/object_table.rb
|
38
|
+
- lib/hirb/helpers/parent_child_tree.rb
|
38
39
|
- lib/hirb/helpers/table.rb
|
40
|
+
- lib/hirb/helpers/tree.rb
|
39
41
|
- lib/hirb/helpers.rb
|
40
42
|
- lib/hirb/import_object.rb
|
41
43
|
- lib/hirb/util.rb
|
@@ -47,6 +49,7 @@ files:
|
|
47
49
|
- test/import_test.rb
|
48
50
|
- test/table_test.rb
|
49
51
|
- test/test_helper.rb
|
52
|
+
- test/tree_test.rb
|
50
53
|
- test/util_test.rb
|
51
54
|
- test/view_test.rb
|
52
55
|
has_rdoc: true
|