bbcloud 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. data/.gitignore +4 -0
  2. data/LICENSE +19 -0
  3. data/README +34 -0
  4. data/Rakefile +2 -0
  5. data/bbcloud.gemspec +29 -0
  6. data/bin/brightbox-accounts +9 -0
  7. data/bin/brightbox-cloudips +9 -0
  8. data/bin/brightbox-config +9 -0
  9. data/bin/brightbox-images +9 -0
  10. data/bin/brightbox-servers +9 -0
  11. data/bin/brightbox-types +9 -0
  12. data/bin/brightbox-users +9 -0
  13. data/bin/brightbox-zones +9 -0
  14. data/lib/bbcloud/accounts.rb +39 -0
  15. data/lib/bbcloud/api.rb +107 -0
  16. data/lib/bbcloud/cli.rb +127 -0
  17. data/lib/bbcloud/cloud_ips.rb +40 -0
  18. data/lib/bbcloud/commands/accounts-list.rb +9 -0
  19. data/lib/bbcloud/commands/accounts-reset-ftp-password.rb +27 -0
  20. data/lib/bbcloud/commands/accounts-show.rb +18 -0
  21. data/lib/bbcloud/commands/cloudips-create.rb +21 -0
  22. data/lib/bbcloud/commands/cloudips-destroy.rb +37 -0
  23. data/lib/bbcloud/commands/cloudips-list.rb +11 -0
  24. data/lib/bbcloud/commands/cloudips-map.rb +55 -0
  25. data/lib/bbcloud/commands/cloudips-show.rb +13 -0
  26. data/lib/bbcloud/commands/cloudips-unmap.rb +32 -0
  27. data/lib/bbcloud/commands/config-client-add.rb +45 -0
  28. data/lib/bbcloud/commands/config-client-default.rb +24 -0
  29. data/lib/bbcloud/commands/config-client-remove.rb +25 -0
  30. data/lib/bbcloud/commands/images-destroy.rb +18 -0
  31. data/lib/bbcloud/commands/images-list.rb +19 -0
  32. data/lib/bbcloud/commands/images-show.rb +17 -0
  33. data/lib/bbcloud/commands/servers-create.rb +65 -0
  34. data/lib/bbcloud/commands/servers-destroy.rb +25 -0
  35. data/lib/bbcloud/commands/servers-list.rb +13 -0
  36. data/lib/bbcloud/commands/servers-restart.rb +30 -0
  37. data/lib/bbcloud/commands/servers-show.rb +47 -0
  38. data/lib/bbcloud/commands/servers-shutdown.rb +17 -0
  39. data/lib/bbcloud/commands/servers-snapshot.rb +17 -0
  40. data/lib/bbcloud/commands/servers-start.rb +18 -0
  41. data/lib/bbcloud/commands/servers-stop.rb +18 -0
  42. data/lib/bbcloud/commands/types-list.rb +10 -0
  43. data/lib/bbcloud/commands/types-show.rb +22 -0
  44. data/lib/bbcloud/commands/users-list.rb +10 -0
  45. data/lib/bbcloud/commands/users-show.rb +27 -0
  46. data/lib/bbcloud/commands/users-update.rb +39 -0
  47. data/lib/bbcloud/commands/zones-list.rb +9 -0
  48. data/lib/bbcloud/config.rb +92 -0
  49. data/lib/bbcloud/images.rb +44 -0
  50. data/lib/bbcloud/servers.rb +66 -0
  51. data/lib/bbcloud/tables.rb +95 -0
  52. data/lib/bbcloud/types.rb +43 -0
  53. data/lib/bbcloud/users.rb +47 -0
  54. data/lib/bbcloud/vendor/hirb/.gemspec +22 -0
  55. data/lib/bbcloud/vendor/hirb/CHANGELOG.rdoc +106 -0
  56. data/lib/bbcloud/vendor/hirb/LICENSE.txt +22 -0
  57. data/lib/bbcloud/vendor/hirb/README.rdoc +182 -0
  58. data/lib/bbcloud/vendor/hirb/Rakefile +35 -0
  59. data/lib/bbcloud/vendor/hirb/lib/bond/completions/hirb.rb +15 -0
  60. data/lib/bbcloud/vendor/hirb/lib/hirb/console.rb +43 -0
  61. data/lib/bbcloud/vendor/hirb/lib/hirb/dynamic_view.rb +113 -0
  62. data/lib/bbcloud/vendor/hirb/lib/hirb/formatter.rb +116 -0
  63. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/auto_table.rb +24 -0
  64. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/object_table.rb +14 -0
  65. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/parent_child_tree.rb +24 -0
  66. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table/filters.rb +10 -0
  67. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table/resizer.rb +82 -0
  68. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table.rb +323 -0
  69. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/tree.rb +181 -0
  70. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/vertical_table.rb +37 -0
  71. data/lib/bbcloud/vendor/hirb/lib/hirb/helpers.rb +17 -0
  72. data/lib/bbcloud/vendor/hirb/lib/hirb/import_object.rb +10 -0
  73. data/lib/bbcloud/vendor/hirb/lib/hirb/menu.rb +221 -0
  74. data/lib/bbcloud/vendor/hirb/lib/hirb/pager.rb +95 -0
  75. data/lib/bbcloud/vendor/hirb/lib/hirb/string.rb +44 -0
  76. data/lib/bbcloud/vendor/hirb/lib/hirb/util.rb +96 -0
  77. data/lib/bbcloud/vendor/hirb/lib/hirb/version.rb +3 -0
  78. data/lib/bbcloud/vendor/hirb/lib/hirb/view.rb +284 -0
  79. data/lib/bbcloud/vendor/hirb/lib/hirb/views/couch_db.rb +11 -0
  80. data/lib/bbcloud/vendor/hirb/lib/hirb/views/misc_db.rb +15 -0
  81. data/lib/bbcloud/vendor/hirb/lib/hirb/views/mongo_db.rb +14 -0
  82. data/lib/bbcloud/vendor/hirb/lib/hirb/views/orm.rb +11 -0
  83. data/lib/bbcloud/vendor/hirb/lib/hirb/views/rails.rb +19 -0
  84. data/lib/bbcloud/vendor/hirb/lib/hirb/views.rb +8 -0
  85. data/lib/bbcloud/vendor/hirb/lib/hirb.rb +81 -0
  86. data/lib/bbcloud/vendor/hirb/test/auto_table_test.rb +30 -0
  87. data/lib/bbcloud/vendor/hirb/test/console_test.rb +27 -0
  88. data/lib/bbcloud/vendor/hirb/test/deps.rip +4 -0
  89. data/lib/bbcloud/vendor/hirb/test/dynamic_view_test.rb +94 -0
  90. data/lib/bbcloud/vendor/hirb/test/formatter_test.rb +171 -0
  91. data/lib/bbcloud/vendor/hirb/test/hirb_test.rb +39 -0
  92. data/lib/bbcloud/vendor/hirb/test/import_test.rb +9 -0
  93. data/lib/bbcloud/vendor/hirb/test/menu_test.rb +239 -0
  94. data/lib/bbcloud/vendor/hirb/test/object_table_test.rb +79 -0
  95. data/lib/bbcloud/vendor/hirb/test/pager_test.rb +162 -0
  96. data/lib/bbcloud/vendor/hirb/test/resizer_test.rb +62 -0
  97. data/lib/bbcloud/vendor/hirb/test/table_test.rb +550 -0
  98. data/lib/bbcloud/vendor/hirb/test/test_helper.rb +61 -0
  99. data/lib/bbcloud/vendor/hirb/test/tree_test.rb +184 -0
  100. data/lib/bbcloud/vendor/hirb/test/util_test.rb +59 -0
  101. data/lib/bbcloud/vendor/hirb/test/view_test.rb +172 -0
  102. data/lib/bbcloud/vendor/hirb/test/views_test.rb +13 -0
  103. data/lib/bbcloud/version.rb +3 -0
  104. data/lib/bbcloud/zones.rb +27 -0
  105. metadata +241 -0
@@ -0,0 +1,44 @@
1
+ module Brightbox
2
+ class Image < Api
3
+
4
+ def to_row
5
+ o = fog_model.attributes
6
+ o[:id] = fog_model.id
7
+ if status == "available"
8
+ o[:status] = (public ? 'public' : 'private')
9
+ else
10
+ o[:status] = status
11
+ end
12
+ o[:arch] = arch
13
+ o[:name] = name.to_s + " (#{arch})"
14
+ o[:owner] = owner_id
15
+ if official
16
+ o[:type] = "official"
17
+ o[:owner] = "brightbox"
18
+ else
19
+ o[:type] = source_type
20
+ end
21
+ o[:created_at] = created_at
22
+ o[:created_on] = created_at.to_s.split('T').first
23
+ o[:description] = description if description
24
+ o[:size] = virtual_size
25
+ o
26
+ end
27
+
28
+ def public?
29
+ public
30
+ end
31
+
32
+ def self.all
33
+ conn.images
34
+ end
35
+
36
+ def self.get(id)
37
+ conn.images.get(id)
38
+ end
39
+
40
+ def self.default_field_order
41
+ [:id, :owner, :type, :created_on, :status, :size, :name]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,66 @@
1
+ module Brightbox
2
+ class Server < Api
3
+
4
+ def self.create(options)
5
+ new(conn.servers.create(options))
6
+ end
7
+
8
+ def server_type
9
+ @server_type ||= Type.new(flavor_id)
10
+ end
11
+
12
+ def image
13
+ @image ||= Image.new(image_id)
14
+ end
15
+
16
+ def attributes
17
+ a = fog_model.attributes
18
+ a[:image] = image
19
+ a[:description] = description.to_s.strip
20
+ a[:created_at] = created_at
21
+ a[:created_on] = fog_model.created_at.strftime("%Y-%m-%d")
22
+ a[:type] = server_type
23
+ a[:zone] = Zone.new(zone_id)
24
+ a[:hostname] = hostname
25
+ a[:public_hostname] = public_hostname unless cloud_ips.empty?
26
+ a
27
+ end
28
+
29
+ def to_row
30
+ o = attributes
31
+ o[:cloud_ips] = cloud_ips.collect { |i| i['public_ip'] }
32
+ o[:ips] = interfaces.collect { |i| i['ipv4_address'] }.join(', ')
33
+ o
34
+ end
35
+
36
+ def deleted?
37
+ fog_model.status == "deleted"
38
+ end
39
+
40
+ def destroy
41
+ fog_model.destroy
42
+ rescue Excon::Errors::Conflict => e
43
+ raise Conflict, "Cannot delete server #{id}"
44
+ end
45
+
46
+ def self.get(id)
47
+ conn.servers.get id
48
+ end
49
+
50
+ def self.all
51
+ conn.servers
52
+ end
53
+
54
+ def self.default_field_order
55
+ [:id, :status, :type, :zone, :created_on, :image_id, :cloud_ips, :name]
56
+ end
57
+
58
+ def hostname
59
+ "#{id}.gb1.brightbox.com"
60
+ end
61
+
62
+ def public_hostname
63
+ "public.#{hostname}"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,95 @@
1
+ require 'hirb'
2
+
3
+ # Remove most of the ascii art table output
4
+ class SimpleTable < Hirb::Helpers::Table
5
+ def render_table_header
6
+ title_row = ' ' + @fields.map {|f|
7
+ format_cell(@headers[f], @field_lengths[f])
8
+ }.join(' ')
9
+ ["", title_row, render_border]
10
+ end
11
+
12
+ def render_footer
13
+ [render_border, ""]
14
+ end
15
+
16
+ def render_border
17
+ '-' + @fields.map {|f| '-' * @field_lengths[f] }.join('--') + '-'
18
+ end
19
+
20
+ def render_rows
21
+ @rows.map do |row|
22
+ row = ' ' + @fields.map {|f|
23
+ format_cell(row[f], @field_lengths[f])
24
+ }.join(' ')
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ # Vertical table for "show" views
31
+ class ShowTable < Hirb::Helpers::Table
32
+
33
+ def self.render(rows, options={})
34
+ new(rows, {:escape_special_chars=>false, :resize=>false}.merge(options)).render
35
+ end
36
+
37
+ def setup_field_lengths
38
+ @field_lengths = default_field_lengths
39
+ end
40
+
41
+ def render_header; []; end
42
+ def render_footer; []; end
43
+
44
+ def render_rows
45
+ longest_header = Hirb::String.size @headers.values.sort_by {|e| Hirb::String.size(e) }.last
46
+ @rows.map do |row|
47
+ fields = @fields.map {|f|
48
+ "#{Hirb::String.rjust(@headers[f], longest_header)}: #{row[f]}"
49
+ }
50
+ fields << "" if @rows.size > 1
51
+ fields.compact.join("\n")
52
+ end
53
+ end
54
+ end
55
+
56
+ # Print nice ascii tables (or tab separated lists, depending on mode)
57
+ # Has lots of magic.
58
+ def render_table(rows, options = {})
59
+ options = { :description => false }.merge options
60
+ # Figure out the fields from the :model option
61
+ if options[:model] and options[:fields].nil?
62
+ options[:fields] = options[:model].default_field_order
63
+ end
64
+ # Figure out the fields from the first row
65
+ if options[:fields].nil? and rows.first.class.respond_to?(:default_field_order)
66
+ options[:fields] = rows.first.class.default_field_order
67
+ end
68
+ # Call to_row on all the rows
69
+ rows = rows.collect do |row|
70
+ row.respond_to?(:to_row) ? row.to_row : row
71
+ end
72
+ # Call render_cell on all the cells
73
+ rows.each do |row|
74
+ row.keys.each do |k|
75
+ row[k] = row[k].render_cell if row[k].respond_to? :render_cell
76
+ end
77
+ end
78
+ if options[:s]
79
+ # Simple output
80
+ rows.each do |row|
81
+ if options[:vertical]
82
+ data options[:fields].collect { |k| [k, row[k]].join("\t") }.join("\n")
83
+ else
84
+ data options[:fields].collect { |k| row[k] }.join("\t")
85
+ end
86
+ end
87
+ else
88
+ # "graphical" table
89
+ if options[:vertical]
90
+ data ShowTable.render(rows, options)
91
+ else
92
+ data SimpleTable.render(rows, options)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,43 @@
1
+ module Brightbox
2
+ class Type < Api
3
+
4
+ def attributes
5
+ o = fog_model.attributes
6
+ o[:ram] = ram
7
+ o[:disk] = disk
8
+ o
9
+ end
10
+
11
+ def ram
12
+ fog_model.ram.to_i
13
+ end
14
+
15
+ def disk
16
+ fog_model.disk.to_i
17
+ end
18
+
19
+ def to_row
20
+ attributes
21
+ end
22
+
23
+ def render_cell
24
+ handle
25
+ end
26
+
27
+ def id
28
+ fog_model.id
29
+ end
30
+
31
+ def self.all
32
+ conn.flavors
33
+ end
34
+
35
+ def self.get id
36
+ conn.flavors.get id
37
+ end
38
+
39
+ def self.default_field_order
40
+ [:id, :name, :handle, :ram, :disk, :cores, :description]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,47 @@
1
+ module Brightbox
2
+ class User < Api
3
+
4
+ def attributes
5
+ fog_model.attributes
6
+ end
7
+
8
+ def to_row
9
+ attributes.merge({ :accounts => accounts.size })
10
+ end
11
+
12
+ def self.all
13
+ conn.users
14
+ end
15
+
16
+ def self.get(id)
17
+ u = conn.users.get id
18
+ (u.nil? or u.id != id) ? nil : u
19
+ end
20
+
21
+ def self.default_field_order
22
+ [:id, :name, :email_address, :accounts]
23
+ end
24
+
25
+ def accounts
26
+ @accounts ||= fog_model.accounts.collect { |a| Account.new(a["id"]) }
27
+ end
28
+
29
+ def to_s
30
+ @id
31
+ end
32
+
33
+ def save
34
+ fog_model.save
35
+ rescue Excon::Errors::UnprocessableEntity
36
+ raise InvalidRecord, "Could not save user #{self.id}"
37
+ end
38
+
39
+ def ssh_key_set?
40
+ !ssh_key.to_s.strip.empty?
41
+ end
42
+
43
+ def render_cell
44
+ handle if fog_model
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems' unless Object.const_defined?(:Gem)
3
+ require File.dirname(__FILE__) + "/lib/hirb/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hirb"
7
+ s.version = Hirb::VERSION
8
+ s.authors = ["Gabriel Horner"]
9
+ s.email = "gabriel.horner@gmail.com"
10
+ s.homepage = "http://tagaholic.me/hirb/"
11
+ s.summary = "A mini view framework for console/irb that's easy to use, even while under its influence."
12
+ s.description = "Hirb provides a mini view framework for console applications and uses it to improve irb's default inspect output. Given an object or array of objects, hirb renders a view based on the object's class and/or ancestry. Hirb offers reusable views in the form of helper classes. The two main helpers, Hirb::Helpers::Table and Hirb::Helpers::Tree, provide several options for generating ascii tables and trees. Using Hirb::Helpers::AutoTable, hirb has useful default views for at least ten popular database gems i.e. Rails' ActiveRecord::Base. Other than views, hirb offers a smart pager and a console menu. The smart pager only pages when the output exceeds the current screen size. The menu is used in conjunction with tables to offer two dimensional menus."
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.rubyforge_project = 'tagaholic'
15
+ s.add_development_dependency 'bacon', '>= 1.1.0'
16
+ s.add_development_dependency 'mocha'
17
+ s.add_development_dependency 'mocha-on-bacon'
18
+ s.add_development_dependency 'bacon-bits'
19
+ s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c} **/deps.rip]) + %w{Rakefile .gemspec}
20
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
21
+ s.license = 'MIT'
22
+ end
@@ -0,0 +1,106 @@
1
+ == 0.3.4
2
+ * Added auto format of array-like objects i.e. ActiveRecord::Relation and Set.
3
+ * Fixed bug when Hirb::Console#table is used without Hirb enabled.
4
+ * Fixed bug when hirb is running within cron and uses tput.
5
+
6
+ == 0.3.3
7
+ * Added ignore_errors option to ignore view errors and continue with original view.
8
+ * Added support for array menu items.
9
+ * Added support to ObjectTable for objects with an undefined :send method.
10
+
11
+ == 0.3.2
12
+ * Added irb autocompletions for bond.
13
+ * Fixed tests for ruby 1.9.
14
+ * Changed tests to use bacon.
15
+ * Removed jeweler in Rakefile and pointless $LOAD_PATH manipulation.
16
+
17
+ == 0.3.1
18
+ * Bug fix on DynamicView.class_to_method to allow overrides of default views.
19
+ * Modified mongo_mapper view to have _id first.
20
+
21
+ == 0.3.0
22
+ * Added dynamic views.
23
+ * Added default table views for the following database classes/modules:
24
+ CouchFoo::Base, CouchPotato::Persistence, CouchRest::ExtendedDocument,
25
+ DBI::Row, DataMapper::Resource, Friendly::Document, MongoMapper::Document, MongoMapper::EmbeddedDocument,
26
+ Mongoid::Document, Ripple::Document and Sequel::Model.
27
+ * Added Hirb.add_view and Hirb.add_dynamic_view for easier view manipulation.
28
+ * Added :multi_line_nodes option for Tree.
29
+ * Fixed :change_fields option bug in Table.
30
+ * Fixed no headers and nil fields bug in Table.
31
+ * Removed deprecations in Hirb.config_file + View.enable.
32
+ * Removed Views classes and View.format_class.
33
+ * Removed :return_rows option for Table.
34
+
35
+ == 0.2.10
36
+ * Added multiple options to Menu, most importantly :two_d and :action.
37
+ * Improved table resizing algorithm.
38
+ * Added merging of configs for multiple Hirb.enable calls.
39
+ * Added :max_fields, :hide_empty, :delete_callbacks, :resize, :header_filter
40
+ and :return_rows options to Table.
41
+ * Added escaping for \t and \r in Table.
42
+ * Renamed Table's :no_newlines option to :escape_special_chars.
43
+ * Removed Table's :field_lengths option.
44
+ * Removed Menu's :validate_one option.
45
+ * Bug fix for table header of a basic array.
46
+ * Deprecating Hirb.config_file + View.enable in next release.
47
+
48
+ == 0.2.9
49
+ * Added newline filtering and :no_newlines option for table helper.
50
+ * Added default filters for hashes that have hash values.
51
+ * Bug fix for deprecated to_a call.
52
+
53
+ == 0.2.8
54
+ * Added callbacks to Hirb::Helpers::Table.
55
+ * Added :change_fields option to Hirb::Helpers::Table.
56
+ * Added terminal size detection for jruby.
57
+ * Bug fix for paging long outputs.
58
+ * Bug fix to make unexpected hirb rendering errors more clear.
59
+
60
+ == 0.2.7
61
+ * 2 ruby 1.9 bug fixes.
62
+ * Bug fix in :fields of Hirb::Helpers::ObjectTable.
63
+ * Made :class option in Hirb::Formatter friendlier to external apps.
64
+
65
+ == 0.2.6
66
+ * Added :description option and added proc ability to :children_method option for helpers.
67
+ * Bug fix for no ENV['HOME'] on Windows.
68
+ * Bug fix on unaliasing output_method.
69
+ * Bug fix on multiple renders of vertical table.
70
+
71
+ == 0.2.5
72
+ * Added ability to use Hirb.enable with non-irb ruby shells.
73
+ * Helper configs now recursively merge when inheriting from others via :ancestor option.
74
+
75
+ == 0.2.4
76
+ * Bug fix on UTF-8 support.
77
+
78
+ == 0.2.3
79
+ * Added UTF-8 support for Ruby 1.8.x
80
+ * Added :all_fields option to Table helper.
81
+
82
+ == 0.2.2
83
+ * Added a friendlier default (a vertical table) to incorrectly configured tables.
84
+ * Added vertical table helper thanks to chrononaut.
85
+ * Added detection of :select option from ActiveRecord queries in ActiveRecordTable helper.
86
+ * Added handling anything that responds to :to_a in AutoTable helper.
87
+
88
+ == 0.2.1
89
+ * Fixed typo in Hirb::Console.view
90
+
91
+ == 0.2.0
92
+ * Major refactoring with bug fixes and better tests.
93
+ * Improved table algorithm to ensure that tables don't wrap.
94
+ * Added a pager which detects if output should be paged, Hirb::Pager.
95
+ * Added a selection menu, Hirb::Menu
96
+ * Following API changes: Hirb::Helpers::Table.max_width removed and config files don't use
97
+ the :view key anymore.
98
+ == 0.1.2
99
+ * Added tree views.
100
+ * Added output_method option to Hirb::View.render_output.
101
+
102
+ == 0.1.1
103
+ * Fixed bug when rendering table with many fields.
104
+
105
+ == 0.1.0
106
+ * Initial release
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2010 Gabriel Horner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,182 @@
1
+ To read a linked version of this README, {click here}[http://tagaholic.me/hirb/doc/].
2
+
3
+ == Description
4
+
5
+ Hirb provides a mini view framework for console applications and uses it to improve irb's default inspect output.
6
+ Given an object or array of objects, hirb renders a view based on the object's class and/or ancestry. Hirb offers reusable
7
+ views in the form of helper classes. The two main helpers, Hirb::Helpers::Table and Hirb::Helpers::Tree, provide several options
8
+ for generating ascii tables and trees. Using Hirb::Helpers::AutoTable, hirb has useful default views for at least ten popular database gems
9
+ i.e. Rails' ActiveRecord::Base. Other than views, hirb offers a smart pager and a console menu. The smart pager
10
+ only pages when the output exceeds the current screen size. The menu is used in conjunction with tables to offer
11
+ {two dimensional menus}[http://tagaholic.me/2010/02/16/two-dimensional-console-menus-with-hirb.html].
12
+
13
+ == Install
14
+
15
+ Install the gem with:
16
+
17
+ sudo gem install hirb
18
+
19
+ == View Tutorials
20
+
21
+ * To create and configure views, see Hirb::View or {here if on the web}[http://tagaholic.me/hirb/doc/classes/Hirb/View.html].
22
+ * To create dynamic views, see Hirb::DynamicView or {here if on the web}[http://tagaholic.me/hirb/doc/classes/Hirb/DynamicView.html].
23
+
24
+ == Printing Ascii Tables
25
+
26
+ To print ascii tables from an array of arrays, hashes or any objects:
27
+
28
+ puts Hirb::Helper::AutoTable.render(ARRAY_OF_OBJECTS)
29
+
30
+ Hirb will intelligently pick up on field names from an array of hashes and create properly-aligned
31
+ fields from an array of arrays. See
32
+ {here}[http://tagaholic.me/2009/10/15/boson-and-hirb-interactions.html#hirbs_handy_tables] for examples.
33
+
34
+ == Rails Example
35
+
36
+ Let's load and enable the view framework:
37
+ $ script/console
38
+ Loading local environment (Rails 2.3.5)
39
+ >> require 'hirb'
40
+ => true
41
+ >> Hirb.enable
42
+ => nil
43
+
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.
46
+ >> Hirb::Formatter.dynamic_config['ActiveRecord::Base']
47
+ => {:class=>Hirb::Helpers::AutoTable, :ancestor=>true}
48
+
49
+ # Tag is a model class and descendant of ActiveRecord::Base
50
+ >> 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
+ >> Hirb::Formatter.dynamic_config['String']
59
+ => nil
60
+ >> 'plain ol irb'
61
+ => 'plain ol irb'
62
+ >> Hirb::Formatter.dynamic_config['Symbol']
63
+ => nil
64
+ >> :blah
65
+ => :blah
66
+
67
+ From above you can see there are no views configured for a String or a Symbol so Hirb defaults to
68
+ irb's echo mode. On the other hand, Tag has a view thanks to being a descendant of ActiveRecord::Base
69
+ and there being an :ancestor option.
70
+
71
+ Having seen hirb display views based on an output object's class, let's see it handle an array of objects:
72
+
73
+ >> Tag.all :limit=>3, :order=>"id DESC"
74
+ +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
75
+ | id | created_at | description | name | namespace | predicate | value |
76
+ +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
77
+ | 907 | 2009-03-06 21:10:41 UTC | | gem:tags=yaml | gem | tags | yaml |
78
+ | 906 | 2009-03-06 08:47:04 UTC | | gem:tags=nomonkey | gem | tags | nomonkey |
79
+ | 905 | 2009-03-04 00:30:10 UTC | | article:tags=ruby | article | tags | ruby |
80
+ +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
81
+ 3 rows in set
82
+
83
+ At any time you can disable Hirb if you really like irb's lovely echo mode:
84
+ >> Hirb.disable
85
+ => nil
86
+ >> Tag.all :limit=>3, :order=>"id DESC"
87
+ => [#<Tag id: 907, name: "gem:tags=yaml", description: nil, created_at: "2009-03-06 21:10:41",
88
+ namespace: "gem", predicate: "tags", value: "yaml">, #<Tag id: 906, name: "gem:tags=nomonkey",
89
+ description: nil, created_at: "2009-03-06 08:47:04", namespace: "gem", predicate: "tags", value:
90
+ "nomonkey">, #<Tag id: 905, name: "article:tags=ruby", description: nil, created_at: "2009-03-04
91
+ 00:30:10", namespace: "article", predicate: "tags", value: "ruby">]
92
+
93
+ == Views: Anytime, Anywhere
94
+ While preconfigured tables are great for database records, sometimes you just want to create
95
+ tables/views for any output object:
96
+
97
+ #These examples don't need to have Hirb::View enabled.
98
+ >> Hirb.disable
99
+ => nil
100
+
101
+ # Imports table() and view()
102
+ >> extend Hirb::Console
103
+ => main
104
+
105
+ # Create a table of Dates comparing them with different formats.
106
+ >> table [Date.today, Date.today.next_month], :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
107
+ +------------+--------+-----------+-------+--------------------------+
108
+ | to_s | ld | ajd | amjd | asctime |
109
+ +------------+--------+-----------+-------+--------------------------+
110
+ | 2009-03-11 | 155742 | 4909803/2 | 54901 | Wed Mar 11 00:00:00 2009 |
111
+ | 2009-04-11 | 155773 | 4909865/2 | 54932 | Sat Apr 11 00:00:00 2009 |
112
+ +------------+--------+-----------+-------+--------------------------+
113
+ 2 rows in set
114
+
115
+ # Same table as the previous method. However view() will be able to call any helper.
116
+ >> view [Date.today, Date.today.next_month], :class=>:object_table,
117
+ :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
118
+
119
+ If these console methods weren't convenient enough, try:
120
+
121
+ # Imports view() to all objects.
122
+ >> require 'hirb/import_object'
123
+ =>true
124
+ # Yields same table as above examples.
125
+ >> [Date.today, Date.today.next_month].view :class=>:object_table,
126
+ :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
127
+
128
+ Although views by default are printed to STDOUT, they can be easily modified to write anywhere:
129
+ # Setup views to write to file 'console.log'.
130
+ >> Hirb::View.render_method = lambda {|output| File.open("console.log", 'w') {|f| f.write(output) } }
131
+
132
+ # Writes to file with same table output as above example.
133
+ >> view [Date.today, Date.today.next_month], :class=>:object_table,
134
+ :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
135
+
136
+ # Doesn't write to file because Symbol doesn't have a view and thus defaults to irb's echo mode.
137
+ >> :blah
138
+ =>:blah
139
+
140
+ # Go back to printing Hirb views to STDOUT.
141
+ >> Hirb::View.reset_render_method
142
+
143
+ == Pager
144
+
145
+ Hirb has both pager and formatter functionality enabled by default.
146
+ If you want to turn off the functionality of either you can pass that in at startup:
147
+
148
+ Hirb.enable :pager=>false
149
+ Hirb.enable :formatter=>false
150
+
151
+ or toggle their state at runtime:
152
+
153
+ Hirb::View.toggle_pager
154
+ Hirb::View.toggle_formatter
155
+
156
+ == Sharing Helpers and Views
157
+ If you have tested helpers you'd like to share, fork Hirb and put them under lib/hirb/helpers. To share
158
+ views for certain classes, put them under lib/hirb/views. Please submit views for gems that have a nontrivial
159
+ number of users.
160
+
161
+ == Limitations
162
+ If using Wirble, you should call Hirb after it since they both override irb's default output.
163
+
164
+ == Motivation
165
+ Table code from http://gist.github.com/72234 and {my console app's needs}[http://github.com/cldwalker/tag-tree].
166
+
167
+ == Credits
168
+ * Chrononaut for vertical table helper.
169
+ * crafterm, spastorino, xaviershay, bogdan and joshua for patches.
170
+
171
+ == Bugs/Issues
172
+ Please report them {on github}[http://github.com/cldwalker/hirb/issues].
173
+
174
+ == Links
175
+ * http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html
176
+ * http://tagaholic.me/2009/03/18/ruby-class-trees-rails-plugin-trees-with-hirb.html
177
+ * http://tagaholic.me/2009/06/19/page-irb-output-and-improve-ri-with-hirb.html
178
+
179
+ == Todo
180
+ * Consider generating views based on methods an object responds to.
181
+ * Provide helper methods to all views.
182
+ * Consider adding a template helper.
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'fileutils'
3
+
4
+ def gemspec
5
+ @gemspec ||= eval(File.read('.gemspec'), binding, '.gemspec')
6
+ end
7
+
8
+ desc "Build the gem"
9
+ task :gem=>:gemspec do
10
+ sh "gem build .gemspec"
11
+ FileUtils.mkdir_p 'pkg'
12
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
13
+ end
14
+
15
+ desc "Install the gem locally"
16
+ task :install => :gem do
17
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
18
+ end
19
+
20
+ desc "Generate the gemspec"
21
+ task :generate do
22
+ puts gemspec.to_ruby
23
+ end
24
+
25
+ desc "Validate the gemspec"
26
+ task :gemspec do
27
+ gemspec.validate
28
+ end
29
+
30
+ desc 'Run tests'
31
+ task :test do |t|
32
+ sh 'bacon -q -Ilib -I. test/*_test.rb'
33
+ end
34
+
35
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ complete(:methods=>%w{Hirb::View.enable Hirb.enable}) {
2
+ %w{config_file output_method output width height formatter pager pager_command}
3
+ }
4
+ complete(:methods=>%w{Hirb::Helpers::Table.render table}) {
5
+ %w{fields headers max_fields max_width resize number change_fields}+
6
+ %w{filters header_filter filter_any filter_classes vertical all_fields}+
7
+ %w{description escape_special_chars table_class hide_empty}
8
+ }
9
+ complete(:method=>"Hirb::Helpers::Tree.render") {
10
+ %w{type validate indent limit description multi_line_nodes value_method children_method}
11
+ }
12
+ complete(:methods=>%w{Hirb::Menu.render menu}) {
13
+ %w{helper_class prompt ask directions readline two_d default_field action multi_action} +
14
+ %w{action_object command reopen}
15
+ }