bbcloud 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
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,43 @@
1
+ module Hirb
2
+ # This module is meant to be extended to provide methods for use in a console/irb shell.
3
+ # For example:
4
+ # >> extend Hirb::Console
5
+ # >> view 'some string', :class=>Some::String::Formatter
6
+ # >> table [[:row1], [:row2]]
7
+ module Console
8
+ class<<self
9
+ # A console version of render_output() which takes its same options but allows for shorthand. All options are passed to
10
+ # the helper except for the formatter options. Formatter options are :class, :method and :output_method.
11
+ # Examples:
12
+ # render_output output, :class=>:tree :type=>:directory
13
+ # # is the same as:
14
+ # render_output output, :class=>:tree, :options=> {:type=>:directory}
15
+ #
16
+ def render_output(output, options={})
17
+ View.load_config unless View.config_loaded?
18
+ View.render_output(output, options.merge(:console=>true))
19
+ end
20
+
21
+ # Takes same arguments and options as render_output() but returns formatted output instead of rendering it.
22
+ def format_output(output, options={}, &block)
23
+ View.load_config unless View.config_loaded?
24
+ View.formatter.format_output(output, options.merge(:console=>true), &block)
25
+ end
26
+ end
27
+
28
+ # Renders a table for the given object. Takes same options as Hirb::Helpers::Table.render.
29
+ def table(output, options={})
30
+ Console.render_output(output, options.merge(:class=>"Hirb::Helpers::AutoTable"))
31
+ end
32
+
33
+ # Renders any specified view for the given object. Takes same options as Hirb::View.render_output.
34
+ def view(output, options={})
35
+ Console.render_output(output, options)
36
+ end
37
+
38
+ # Renders a menu given an array using Hirb::Menu.render.
39
+ def menu(output, options={}, &block)
40
+ Console.format_output(output, options.merge(:class=>"Hirb::Menu"), &block)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,113 @@
1
+ module Hirb
2
+ # This module extends a Helper with the ability to have dynamic views for configured output classes.
3
+ # After a Helper has extended this module, it can use it within a render() by calling
4
+ # dynamic_options() to get dynamically generated options for the object it's rendering. See Hirb::Helpers::AutoTable as an example.
5
+ #
6
+ # == Dynamic Views
7
+ # Whereas normal views are generated from helpers with static helper options, dynamic views are generated from helpers and
8
+ # dynamically generated helper options. Let's look at an example for Rails' ActiveRecord classes:
9
+ #
10
+ # Hirb.add_dynamic_view("ActiveRecord::Base", :helper=>:auto_table) {|obj|
11
+ # {:fields=>obj.class.column_names} }
12
+ #
13
+ # From this dynamic view definition, _any_ ActiveRecord model class will render a table with the correct fields, since the fields
14
+ # are extracted from the output object's class at runtime. Note that dynamic view definitions should return a hash of helper options.
15
+ #
16
+ # To define multiple dynamic views, create a Views module where each method ending in '\_view' maps to a class/module:
17
+ #
18
+ # module Hirb::Views::ORM
19
+ # def data_mapper__resource_view(obj)
20
+ # {:fields=>obj.class.properties.map {|e| e.name }}
21
+ # end
22
+ #
23
+ # def sequel__model_view(obj)
24
+ # {:fields=>obj.class.columns}
25
+ # end
26
+ # end
27
+ #
28
+ # Hirb.add_dynamic_view Hirb::Views::ORM, :helper=>:auto_table
29
+ #
30
+ # In this example, 'data_mapper__resource_view' maps to DataMapper::Resource and 'sequel__model_view' maps to Sequel::Model.
31
+ # Note that when mapping method names to class names, '__' maps to '::' and '_' signals the next letter to be capitalized.
32
+ module DynamicView
33
+ # Add dynamic views to output class(es) for a given helper. If defining one view, the first argument is the output class
34
+ # and a block defines the dynamic view. If defining multiple views, the first argument should be a Views::* module where
35
+ # each method in the module ending in _view defines a view for an output class. To map output classes to method names in
36
+ # a Views module, translate'::' to '__' and a capital letter translates to a '_' and a lowercase letter.
37
+ # ==== Options:
38
+ # [*:helper*] Required option. Helper class that view(s) use to format. Hirb::Helpers::AutoTable is the only valid
39
+ # helper among default helpers. Can be given in aliased form i.e. :auto_table -> Hirb::Helpers::AutoTable.
40
+ #
41
+ # Examples:
42
+ # Hirb.add_dynamic_view Hirb::Views::ORM, :helper=>:auto_table
43
+ # Hirb.add_dynamic_view("ActiveRecord::Base", :helper=>:auto_table) {|obj| {:fields=>obj.class.column_names} }
44
+ def self.add(view, options, &block)
45
+ raise ArgumentError, ":helper option is required" unless options[:helper]
46
+ helper = Helpers.helper_class options[:helper]
47
+ unless helper.is_a?(Module) && class << helper; self.ancestors; end.include?(self)
48
+ raise ArgumentError, ":helper option must be a helper that has extended DynamicView"
49
+ end
50
+ mod = block ? generate_single_view_module(view, &block) : view
51
+ raise ArgumentError, "'#{mod}' must be a module" unless mod.is_a?(Module)
52
+ helper.add_module mod
53
+ end
54
+
55
+ def self.generate_single_view_module(output_mod, &block) #:nodoc:
56
+ meth = class_to_method output_mod.to_s
57
+ view_mod = meth.capitalize
58
+ Views::Single.send(:remove_const, view_mod) if Views::Single.const_defined?(view_mod)
59
+ mod = Views::Single.const_set(view_mod, Module.new)
60
+ mod.send(:define_method, meth, block)
61
+ mod
62
+ end
63
+
64
+ def self.class_to_method(mod) #:nodoc:
65
+ mod.gsub(/(?!^)([A-Z])/) {|e| '_'+e }.gsub('::_', '__').downcase + '_view'
66
+ end
67
+
68
+ # Returns a hash of options based on dynamic views defined for the object's ancestry. If no config is found returns nil.
69
+ def dynamic_options(obj)
70
+ view_methods.each do |meth|
71
+ if obj.class.ancestors.map {|e| e.to_s }.include?(method_to_class(meth))
72
+ begin
73
+ return send(meth, obj)
74
+ rescue
75
+ raise "View failed to generate for '#{method_to_class(meth)}' "+
76
+ "while in '#{meth}' with error:\n#{$!.message}"
77
+ end
78
+ end
79
+ end
80
+ nil
81
+ end
82
+
83
+ #:stopdoc:
84
+ def add_module(mod)
85
+ new_methods = mod.instance_methods.select {|e| e.to_s =~ /_view$/ }.map {|e| e.to_s}
86
+ return if new_methods.empty?
87
+ extend mod
88
+ view_methods.replace(view_methods + new_methods).uniq!
89
+ update_config(new_methods)
90
+ end
91
+
92
+ def update_config(meths)
93
+ output_config = meths.inject({}) {|t,e|
94
+ t[method_to_class(e)] = {:class=>self, :ancestor=>true}; t
95
+ }
96
+ Formatter.dynamic_config.merge! output_config
97
+ end
98
+
99
+ def method_to_class(meth)
100
+ view_method_classes[meth] ||= Util.camelize meth.sub(/_view$/, '').gsub('__', '/')
101
+ end
102
+
103
+ def view_method_classes
104
+ @view_method_classes ||= {}
105
+ end
106
+ #:startdoc:
107
+
108
+ # Stores view methods that a Helper has been given via DynamicView.add
109
+ def view_methods
110
+ @view_methods ||= []
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,116 @@
1
+ module Hirb
2
+ # A Formatter object formats an output object (using Formatter.format_output) into a string based on the views defined
3
+ # for its class and/or ancestry.
4
+ class Formatter
5
+ class<<self
6
+ # This config is used by Formatter.format_output to lazily load dynamic views defined with Hirb::DynamicView.
7
+ # This hash has the same format as Formatter.config.
8
+ attr_accessor :dynamic_config
9
+ end
10
+ self.dynamic_config = {}
11
+
12
+ def initialize(additional_config={}) #:nodoc:
13
+ @klass_config = {}
14
+ @config = additional_config || {}
15
+ end
16
+
17
+ # A hash of Ruby class strings mapped to view hashes. A view hash must have at least a :method, :output_method
18
+ # or :class option for a view to be applied to an output. A view hash has the following keys:
19
+ # [*:method*] Specifies a global (Kernel) method to do the formatting.
20
+ # [*:class*] Specifies a class to do the formatting, using its render() class method. If a symbol it's converted to a corresponding
21
+ # Hirb::Helpers::* class if it exists.
22
+ # [*:output_method*] Specifies a method or proc to call on output before passing it to a helper. If the output is an array, it's applied
23
+ # to every element in the array.
24
+ # [*:options*] Options to pass the helper method or class.
25
+ # [*:ancestor*] Boolean which when true causes subclasses of the output class to inherit its config. This doesn't effect the current
26
+ # output class. Defaults to false. This is used by ActiveRecord classes.
27
+ #
28
+ # Examples:
29
+ # {'WWW::Delicious::Element'=>{:class=>'Hirb::Helpers::ObjectTable', :ancestor=>true, :options=>{:max_width=>180}}}
30
+ # {'Date'=>{:class=>:auto_table, :ancestor=>true}}
31
+ # {'Hash'=>{:method=>:puts}}
32
+ def config
33
+ @config
34
+ end
35
+
36
+ # Adds the view for the given class and view hash config. See Formatter.config for valid keys for view hash.
37
+ def add_view(klass, view_config)
38
+ @klass_config.delete(klass)
39
+ @config[klass.to_s] = view_config
40
+ true
41
+ end
42
+
43
+ # This method looks for an output object's view in Formatter.config and then Formatter.dynamic_config.
44
+ # If a view is found, a stringified view is returned based on the object. If no view is found, nil is returned. The options this
45
+ # class takes are a view hash as described in Formatter.config. These options will be merged with any existing helper
46
+ # config hash an output class has in Formatter.config. Any block given is passed along to a helper class.
47
+ def format_output(output, options={}, &block)
48
+ output_class = determine_output_class(output)
49
+ options = parse_console_options(options) if options.delete(:console)
50
+ options = Util.recursive_hash_merge(klass_config(output_class), options)
51
+ _format_output(output, options, &block)
52
+ end
53
+
54
+ #:stopdoc:
55
+ def _format_output(output, options, &block)
56
+ output = options[:output_method] ? (output.is_a?(Array) ?
57
+ output.map {|e| call_output_method(options[:output_method], e) } :
58
+ call_output_method(options[:output_method], output) ) : output
59
+ args = [output]
60
+ args << options[:options] if options[:options] && !options[:options].empty?
61
+ if options[:method]
62
+ send(options[:method],*args)
63
+ elsif options[:class] && (helper_class = Helpers.helper_class(options[:class]))
64
+ helper_class.render(*args, &block)
65
+ elsif options[:output_method]
66
+ output
67
+ end
68
+ end
69
+
70
+ def parse_console_options(options) #:nodoc:
71
+ real_options = [:method, :class, :output_method].inject({}) do |h, e|
72
+ h[e] = options.delete(e) if options[e]; h
73
+ end
74
+ real_options.merge! :options=>options
75
+ end
76
+
77
+ def determine_output_class(output)
78
+ output.respond_to?(:to_a) ? Array(output)[0].class : output.class
79
+ end
80
+
81
+ def call_output_method(output_method, output)
82
+ output_method.is_a?(Proc) ? output_method.call(output) : output.send(output_method)
83
+ end
84
+
85
+ # Internal view options built from user-defined ones. Options are built by recursively merging options from oldest
86
+ # ancestors to the most recent ones.
87
+ def klass_config(output_class)
88
+ @klass_config[output_class] ||= build_klass_config(output_class)
89
+ end
90
+
91
+ def build_klass_config(output_class)
92
+ output_ancestors = output_class.ancestors.map {|e| e.to_s}.reverse
93
+ output_ancestors.pop
94
+ hash = output_ancestors.inject({}) {|h, klass|
95
+ add_klass_config_if_true(h, klass) {|c,klass| c[klass] && c[klass][:ancestor] }
96
+ }
97
+ add_klass_config_if_true(hash, output_class.to_s) {|c,klass| c[klass] }
98
+ end
99
+
100
+ def add_klass_config_if_true(hash, klass)
101
+ if yield(@config, klass)
102
+ Util.recursive_hash_merge hash, @config[klass]
103
+ elsif yield(self.class.dynamic_config, klass)
104
+ @config[klass] = self.class.dynamic_config[klass].dup # copy to local
105
+ Util.recursive_hash_merge hash, self.class.dynamic_config[klass]
106
+ else
107
+ hash
108
+ end
109
+ end
110
+
111
+ def reset_klass_config
112
+ @klass_config = {}
113
+ end
114
+ #:startdoc:
115
+ end
116
+ end
@@ -0,0 +1,24 @@
1
+ # This helper wraps around the other table helpers i.e. Hirb::Helpers::Table while
2
+ # providing default helper options via Hirb::DynamicView. Using these default options, this
3
+ # helper supports views for the following modules/classes:
4
+ # ActiveRecord::Base, CouchFoo::Base, CouchPotato::Persistence, CouchRest::ExtendedDocument,
5
+ # DBI::Row, DataMapper::Resource, Friendly::Document, MongoMapper::Document, MongoMapper::EmbeddedDocument,
6
+ # Mongoid::Document, Ripple::Document, Sequel::Model.
7
+ class Hirb::Helpers::AutoTable < Hirb::Helpers::Table
8
+ extend Hirb::DynamicView
9
+
10
+ # Takes same options as Hirb::Helpers::Table.render except as noted below.
11
+ #
12
+ # ==== Options:
13
+ # [:table_class] Explicit table class to use for rendering. Defaults to
14
+ # Hirb::Helpers::ObjectTable if output is not an Array or Hash. Otherwise
15
+ # defaults to Hirb::Helpers::Table.
16
+ def self.render(output, options={})
17
+ output = Array(output)
18
+ (defaults = dynamic_options(output[0])) && (options = defaults.merge(options))
19
+ klass = options.delete(:table_class) || (
20
+ !(output[0].is_a?(Hash) || output[0].is_a?(Array)) ?
21
+ Hirb::Helpers::ObjectTable : Hirb::Helpers::Table)
22
+ klass.render(output, options)
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ class Hirb::Helpers::ObjectTable < Hirb::Helpers::Table
2
+ # Rows are any ruby objects. Takes same options as Hirb::Helpers::Table.render except as noted below.
3
+ #
4
+ # ==== Options:
5
+ # [:fields] Methods of the object to represent as columns. Defaults to [:to_s].
6
+ def self.render(rows, options ={})
7
+ options[:fields] ||= [:to_s]
8
+ options[:headers] ||= {:to_s=>'value'} if options[:fields] == [:to_s]
9
+ item_hashes = options[:fields].empty? ? [] : Array(rows).inject([]) {|t,item|
10
+ t << options[:fields].inject({}) {|h,f| h[f] = item.__send__(f); h}
11
+ }
12
+ super(item_hashes, options)
13
+ end
14
+ end
@@ -0,0 +1,24 @@
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 or proc 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 or proc 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
+ @value_method = value_method.is_a?(Proc) ? value_method : lambda {|n| n.send(value_method) }
12
+ children_method = options[:children_method] || :children
13
+ @children_method = children_method.is_a?(Proc) ? children_method : lambda {|n| n.send(children_method)}
14
+ @nodes = []
15
+ build_node(root_node, 0)
16
+ super(@nodes, options)
17
+ end
18
+
19
+ def build_node(node, level) #:nodoc:
20
+ @nodes << {:value=>@value_method.call(node), :level=>level}
21
+ @children_method.call(node).each {|e| build_node(e, level + 1)}
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ class Hirb::Helpers::Table
2
+ # Contains filter methods used by :filters option. To define a custom filter, simply open this module and create a method
3
+ # that take one argument, the value you will be filtering.
4
+ module Filters
5
+ extend self
6
+ def comma_join(arr) #:nodoc:
7
+ arr.join(', ')
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,82 @@
1
+ class Hirb::Helpers::Table
2
+ # Resizes a table's fields to the table's max width.
3
+ class Resizer
4
+ # Modifies field_lengths to fit within width. Also enforces a table's max_fields.
5
+ def self.resize!(table)
6
+ obj = new(table)
7
+ obj.resize
8
+ obj.field_lengths
9
+ end
10
+
11
+ #:stopdoc:
12
+ attr_reader :field_lengths
13
+ def initialize(table)
14
+ @table, @width, @field_size = table, table.actual_width, table.fields.size
15
+ @field_lengths = table.field_lengths
16
+ @original_field_lengths = @field_lengths.dup
17
+ end
18
+
19
+ def resize
20
+ adjust_long_fields || default_restrict_field_lengths
21
+ @table.enforce_field_constraints
22
+ add_extra_width
23
+ end
24
+
25
+ # Simple algorithm which allows smaller fields to be displayed while
26
+ # restricting longer fields to an average_long_field
27
+ def adjust_long_fields
28
+ while (total_length = sum(@field_lengths.values)) > @width
29
+ average_field = total_length / @field_size.to_f
30
+ long_lengths = @field_lengths.values.select {|e| e > average_field }
31
+ return false if long_lengths.empty?
32
+
33
+ # adjusts average long field by ratio with @width
34
+ average_long_field = sum(long_lengths)/long_lengths.size * @width/total_length
35
+ @field_lengths.each {|f,length|
36
+ @field_lengths[f] = average_long_field if length > average_long_field
37
+ }
38
+ end
39
+ true
40
+ end
41
+
42
+ # Produces a field_lengths which meets the @width requirement
43
+ def default_restrict_field_lengths
44
+ original_total_length = sum @original_field_lengths.values
45
+ # set fields by their relative weight to original length
46
+ new_lengths = @original_field_lengths.inject({}) {|t,(k,v)|
47
+ t[k] = (v / original_total_length.to_f * @width).to_i; t }
48
+
49
+ # set all fields the same if relative doesn't work
50
+ unless new_lengths.values.all? {|e| e > MIN_FIELD_LENGTH} && (sum(new_lengths.values) <= @width)
51
+ new_lengths = @field_lengths.inject({}) {|t,(k,v)| t[k] = @width / @field_size; t }
52
+ end
53
+ @field_lengths.each {|k,v| @field_lengths[k] = new_lengths[k] }
54
+ end
55
+
56
+ def add_extra_width
57
+ added_width = 0
58
+ extra_width = @width - sum(@field_lengths.values)
59
+ unmaxed_fields = @field_lengths.keys.select {|f| !remaining_width(f).zero? }
60
+ # order can affect which one gets the remainder so let's keep it consistent
61
+ unmaxed_fields = unmaxed_fields.sort_by {|e| e.to_s}
62
+
63
+ unmaxed_fields.each_with_index do |f, i|
64
+ extra_per_field = (extra_width - added_width) / (unmaxed_fields.size - i)
65
+ add_to_field = remaining_width(f) < extra_per_field ? remaining_width(f) : extra_per_field
66
+ added_width += add_to_field
67
+ @field_lengths[f] += add_to_field
68
+ end
69
+ end
70
+
71
+ def remaining_width(field)
72
+ (@remaining_width ||= {})[field] ||= begin
73
+ (@table.max_fields[field] || @original_field_lengths[field]) - @field_lengths[field]
74
+ end
75
+ end
76
+
77
+ def sum(arr)
78
+ arr.inject {|t,e| t += e }
79
+ end
80
+ #:startdoc:
81
+ end
82
+ end