bbcloud 0.6.2 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/bbcloud.gemspec +1 -3
- data/lib/bbcloud/commands/servers-show.rb +1 -1
- data/lib/bbcloud/version.rb +1 -1
- metadata +26 -61
- data/lib/bbcloud/commands/servers-restart.rb +0 -30
- data/lib/bbcloud/vendor/hirb/.gemspec +0 -22
- data/lib/bbcloud/vendor/hirb/CHANGELOG.rdoc +0 -106
- data/lib/bbcloud/vendor/hirb/LICENSE.txt +0 -22
- data/lib/bbcloud/vendor/hirb/README.rdoc +0 -182
- data/lib/bbcloud/vendor/hirb/Rakefile +0 -35
- data/lib/bbcloud/vendor/hirb/lib/bond/completions/hirb.rb +0 -15
- data/lib/bbcloud/vendor/hirb/lib/hirb/console.rb +0 -43
- data/lib/bbcloud/vendor/hirb/lib/hirb/dynamic_view.rb +0 -113
- data/lib/bbcloud/vendor/hirb/lib/hirb/formatter.rb +0 -116
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/auto_table.rb +0 -24
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/object_table.rb +0 -14
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/parent_child_tree.rb +0 -24
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table/filters.rb +0 -10
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table/resizer.rb +0 -82
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table.rb +0 -323
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/tree.rb +0 -181
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/vertical_table.rb +0 -37
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers.rb +0 -17
- data/lib/bbcloud/vendor/hirb/lib/hirb/import_object.rb +0 -10
- data/lib/bbcloud/vendor/hirb/lib/hirb/menu.rb +0 -221
- data/lib/bbcloud/vendor/hirb/lib/hirb/pager.rb +0 -95
- data/lib/bbcloud/vendor/hirb/lib/hirb/string.rb +0 -44
- data/lib/bbcloud/vendor/hirb/lib/hirb/util.rb +0 -96
- data/lib/bbcloud/vendor/hirb/lib/hirb/version.rb +0 -3
- data/lib/bbcloud/vendor/hirb/lib/hirb/view.rb +0 -284
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/couch_db.rb +0 -11
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/misc_db.rb +0 -15
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/mongo_db.rb +0 -14
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/orm.rb +0 -11
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/rails.rb +0 -19
- data/lib/bbcloud/vendor/hirb/lib/hirb/views.rb +0 -8
- data/lib/bbcloud/vendor/hirb/lib/hirb.rb +0 -81
- data/lib/bbcloud/vendor/hirb/test/auto_table_test.rb +0 -30
- data/lib/bbcloud/vendor/hirb/test/console_test.rb +0 -27
- data/lib/bbcloud/vendor/hirb/test/deps.rip +0 -4
- data/lib/bbcloud/vendor/hirb/test/dynamic_view_test.rb +0 -94
- data/lib/bbcloud/vendor/hirb/test/formatter_test.rb +0 -171
- data/lib/bbcloud/vendor/hirb/test/hirb_test.rb +0 -39
- data/lib/bbcloud/vendor/hirb/test/import_test.rb +0 -9
- data/lib/bbcloud/vendor/hirb/test/menu_test.rb +0 -239
- data/lib/bbcloud/vendor/hirb/test/object_table_test.rb +0 -79
- data/lib/bbcloud/vendor/hirb/test/pager_test.rb +0 -162
- data/lib/bbcloud/vendor/hirb/test/resizer_test.rb +0 -62
- data/lib/bbcloud/vendor/hirb/test/table_test.rb +0 -550
- data/lib/bbcloud/vendor/hirb/test/test_helper.rb +0 -61
- data/lib/bbcloud/vendor/hirb/test/tree_test.rb +0 -184
- data/lib/bbcloud/vendor/hirb/test/util_test.rb +0 -59
- data/lib/bbcloud/vendor/hirb/test/view_test.rb +0 -172
- data/lib/bbcloud/vendor/hirb/test/views_test.rb +0 -13
@@ -1,113 +0,0 @@
|
|
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
|
@@ -1,116 +0,0 @@
|
|
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
|
@@ -1,24 +0,0 @@
|
|
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
|
@@ -1,14 +0,0 @@
|
|
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
|
@@ -1,24 +0,0 @@
|
|
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
|
@@ -1,10 +0,0 @@
|
|
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
|
@@ -1,82 +0,0 @@
|
|
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
|