Class: AwesomePrint::Formatters::BaseFormatter
- Inherits:
-
Object
- Object
- AwesomePrint::Formatters::BaseFormatter
show all
- Includes:
- Colorize
- Defined in:
- lib/awesome_print/formatters/base_formatter.rb
Constant Summary
collapse
- DEFAULT_LIMIT_SIZE =
7
Instance Method Summary
collapse
Methods included from Colorize
#colorize
Instance Method Details
#align(value, width) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 124
def align(value, width)
if options[:multiline]
if options[:indent] > 0
value.rjust(width)
elsif options[:indent] == 0
indent + value.ljust(width)
else
indent[0, indentation + options[:indent]] + value.ljust(width)
end
else
value
end
end
|
#get_limit_size ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 34
def get_limit_size
case options[:limit]
when true
DEFAULT_LIMIT_SIZE
else
options[:limit]
end
end
|
116
117
118
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 116
def indent
' ' * indentation
end
|
#indentation ⇒ Object
108
109
110
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 108
def indentation
inspector.current_indentation
end
|
#indented(&block) ⇒ Object
112
113
114
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 112
def indented(&block)
inspector.increase_indentation(&block)
end
|
#limited(data, width, is_hash = false) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 43
def limited(data, width, is_hash = false)
limit = get_limit_size
if data.length <= limit
data
else
head = limit / 2
tail = head - (limit - 1) % 2
temp = data[0, head] + [nil] + data[-tail, tail]
temp[head] = if is_hash
"#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}"
else
"#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]"
end
temp
end
end
|
#method_tuple(method) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 66
def method_tuple(method)
if method.respond_to?(:parameters) args = method.parameters.inject([]) do |arr, (type, name)|
name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
arr << case type
when :req then name.to_s
when :opt, :rest then "*#{name}"
when :block then "&#{name}"
else '?'
end
end
else args = (1..method.arity.abs).map { |i| "arg#{i}" }
args[-1] = "*#{args[-1]}" if method.arity < 0
end
if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
unbound = $1 && '(unbound)'
klass = $2
if klass && klass =~ /(\(\w+:\s.*?\))/ klass.sub!($1, '') end
owner = "#{klass}#{unbound}".gsub('(', ' (')
end
[method.name.to_s, "(#{args.join(', ')})", owner.to_s]
end
|
120
121
122
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 120
def outdent
' ' * (indentation - options[:indent].abs)
end
|
#should_be_limited? ⇒ Boolean
To support limited output, for example:
ap ('a'..'z').to_a, :limit => 3
[
[ 0] "a",
[ 1] .. [24],
[25] "z"
]
ap (1..100).to_a, :limit => true # Default limit is 7.
[
[ 0] 1,
[ 1] 2,
[ 2] 3,
[ 3] .. [96],
[97] 98,
[98] 99,
[99] 100
]
30
31
32
|
# File 'lib/awesome_print/formatters/base_formatter.rb', line 30
def should_be_limited?
options[:limit] or (options[:limit].is_a?(Integer) and options[:limit] > 0)
end
|