Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 164 lines (126 sloc) 7.23 kb
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
1 == Description
2
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
3 Hirb currently provides a mini view framework for console applications, designed to improve irb's default output.
4 Hirb improves console output by providing a smart pager and auto-formatting output. The smart pager detects when an output exceeds
5 a screenful and thus only pages output as needed. Auto-formatting adds a view to an output's class. This is helpful in separating
6 views from content (MVC anyone?). The framework encourages reusing views by letting you
7 package them in classes and associate them with any number of output classes. Hirb comes with tree views (see
29d5c46 Gabriel Horner readme tweak
cldwalker authored
8 Hirb::Helpers::Tree) and table views (see Hirb::Helpers::Table). By default Hirb displays Rails'
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
9 model classes as tables. Hirb also sports a nice selection menu, Hirb::Menu.
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
10
11 == Install
12
13 Install the gem with:
14
057b6f2 Gabriel Horner changelog + readme tweaks
cldwalker authored
15 sudo gem install hirb
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
16
0468817 Gabriel Horner updated readme
cldwalker authored
17 == Pager
18
19 Hirb has both pager and formatter functionality enabled by default.
20 If you want to turn off the functionality of either you can pass that in at startup:
21
22 Hirb.enable :pager=>false
23 Hirb.enable :formatter=>false
24
25 or toggle their state at runtime:
26
27 Hirb::View.toggle_pager
28 Hirb::View.toggle_formatter
29
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
30 == Create and Configure Views
31
32 If you'd like to learn how to create and configure views, {read the docs}[http://tagaholic.me/hirb/doc/classes/Hirb/Formatter.html].
33
0468817 Gabriel Horner updated readme
cldwalker authored
34 == Rails Formatter Example
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
35
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
36 Let's load and enable the view framework:
37 bash> script/console
38 Loading local environment (Rails 2.2.2)
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
39 irb>> require 'hirb'
40 => true
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
41 irb>> Hirb.enable
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
42 => nil
43
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
44 The default configuration provides table views for ActiveRecord::Base descendants.
45 If a class isn't configured, Hirb reverts to irb's default echo mode.
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
46 irb>> Hirb::View.formatter_config
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
47 => {"ActiveRecord::Base"=>{:class=>"Hirb::Views::ActiveRecord_Base", :ancestor=>true}}
48
49 # Tag is a model class and descendant of ActiveRecord::Base
50 irb>> Tag.last
51 +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
52 | id | created_at | description | name | namespace | predicate | value |
53 +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
54 | 907 | 2009-03-06 21:10:41 UTC | | gem:tags=yaml | gem | tags | yaml |
55 +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
56 1 row in set
57
58 irb>> 'plain ol irb'
59 => 'plain ol irb'
60 irb>> :blah
61 => :blah
62
63 From above you can see there were no views configured for a String or a Symbol so Hirb defaulted to
64 irb's echo mode. Also note that Tag was able to inherit its view from the ActiveRecord::Base config
65 because it had an :ancestor option.
66
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
67 Now that you understand that Hirb displays views based on an output object's class,
68 you may appreciate it also detects configured output objects in an array:
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
69
70 irb>> Tag.all :limit=>3, :order=>"id DESC"
71 +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
72 | id | created_at | description | name | namespace | predicate | value |
73 +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
74 | 907 | 2009-03-06 21:10:41 UTC | | gem:tags=yaml | gem | tags | yaml |
75 | 906 | 2009-03-06 08:47:04 UTC | | gem:tags=nomonkey | gem | tags | nomonkey |
76 | 905 | 2009-03-04 00:30:10 UTC | | article:tags=ruby | article | tags | ruby |
77 +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
78 3 rows in set
79
80 At any time you can disable Hirb if you really like irb's lovely echo mode:
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
81 irb>> Hirb.disable
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
82 => nil
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
83 irb>> Tag.all :limit=>3, :order=>"id DESC"
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
84 => [#<Tag id: 907, name: "gem:tags=yaml", description: nil, created_at: "2009-03-06 21:10:41",
85 namespace: "gem", predicate: "tags", value: "yaml">, #<Tag id: 906, name: "gem:tags=nomonkey",
86 description: nil, created_at: "2009-03-06 08:47:04", namespace: "gem", predicate: "tags", value:
87 "nomonkey">, #<Tag id: 905, name: "article:tags=ruby", description: nil, created_at: "2009-03-04
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
88 00:30:10", namespace: "article", predicate: "tags", value: "ruby">]
89
90 == Views: Anytime, Anywhere
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
91 While preconfigured tables are great for database records, sometimes you just want to create
92 tables/views for any output object:
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
93
94 #These examples don't need to have Hirb::View enabled.
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
95 irb>>Hirb.disable
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
96 =>nil
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
97
98 # Imports table() and view()
99 irb>>extend Hirb::Console
100 =>main
101
102 # Create a table of Dates comparing them with different formats.
103 irb>> table [Date.today, Date.today.next_month], :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
104 +------------+--------+-----------+-------+--------------------------+
105 | to_s | ld | ajd | amjd | asctime |
106 +------------+--------+-----------+-------+--------------------------+
107 | 2009-03-11 | 155742 | 4909803/2 | 54901 | Wed Mar 11 00:00:00 2009 |
108 | 2009-04-11 | 155773 | 4909865/2 | 54932 | Sat Apr 11 00:00:00 2009 |
109 +------------+--------+-----------+-------+--------------------------+
110 2 rows in set
111
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
112 # Same table as the previous method. However view() will be able to call any helper.
113 irb>> view [Date.today, Date.today.next_month], :class=>:object_table,
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
114 :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
115
116 If these console methods weren't convenient enough, try:
117
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
118 # Imports view() to all objects.
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
119 irb>> require 'hirb/import_object'
120 =>true
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
121 # Yields same table as above examples.
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
122 irb>> [Date.today, Date.today.next_month].view :class=>:object_table,
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
123 :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
124
125 Although views by default are printed to STDOUT, they can be easily modified to write anywhere:
126 # Setup views to write to file 'console.log'.
127 irb>> Hirb::View.render_method = lambda {|output| File.open("console.log", 'w') {|f| f.write(output) } }
128
129 # Writes to file with same table output as above example.
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
130 irb>> view [Date.today, Date.today.next_month], :class=>:object_table,
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
131 :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
132
133 # Doesn't write to file because Symbol isn't configured to use Hirb::View and thus defaults to irb's echo mode.
134 irb>> :blah
135 =>:blah
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
136
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
137 # Go back to printing Hirb views to STDOUT.
138 irb>> Hirb::View.reset_render_method
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
139
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
140 == Limitations
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
141 If using Wirble, you should call Hirb after it since they both override irb's default output.
35ac793 Gabriel Horner more docs, rakefile,license + readme
cldwalker authored
142
8535d1b Gabriel Horner readme tweak
cldwalker authored
143 == Motivation
144 Table code from http://gist.github.com/72234 and {my console
a2cb3ef Gabriel Horner changed filesystem tree to directory tree, fixed bug in directory tree, ...
cldwalker authored
145 app's needs}[http://github.com/cldwalker/tag-tree].
8535d1b Gabriel Horner readme tweak
cldwalker authored
146
38eb74f Gabriel Horner #5: added vertical table helper thanks to @Chrononaut
cldwalker authored
147 == Credits
ea44d5a Gabriel Horner updated readme and changelog
cldwalker authored
148 * Chrononaut for vertical table helper.
057b6f2 Gabriel Horner changelog + readme tweaks
cldwalker authored
149 * crafterm, spastorino and joshua for bug fixes.
38eb74f Gabriel Horner #5: added vertical table helper thanks to @Chrononaut
cldwalker authored
150
5eda7a9 Gabriel Horner update issues link
cldwalker authored
151 == Bugs/Issues
152 Please report them {on github}[http://github.com/cldwalker/hirb/issues].
6a7ec42 Gabriel Horner added bugs/tickets link
cldwalker authored
153
37779ed Gabriel Horner readme tweak
cldwalker authored
154 == Links
155 * http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html
91584ff Gabriel Horner some doc tweaks
cldwalker authored
156 * http://tagaholic.me/2009/03/18/ruby-class-trees-rails-plugin-trees-with-hirb.html
0468817 Gabriel Horner updated readme
cldwalker authored
157 * http://tagaholic.me/2009/06/19/page-irb-output-and-improve-ri-with-hirb.html
37779ed Gabriel Horner readme tweak
cldwalker authored
158
42fbcc4 Gabriel Horner decent readme, more tweaks + tests
cldwalker authored
159 == Todo
a2cb3ef Gabriel Horner changed filesystem tree to directory tree, fixed bug in directory tree, ...
cldwalker authored
160 * Consider applying multiple views/filters to output.
873b1c6 Gabriel Horner more view api additions, updated changelog + rdoc
cldwalker authored
161 * Consider mapping a class' methods to their own views.
1bfd96b Gabriel Horner more docs + reset_render_method
cldwalker authored
162 * Provides helper methods to all view classes.
163 * Consider adding a template system as needed.
Something went wrong with that request. Please try again.