Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Commit

Permalink
Add omit_root_container and prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Mar 14, 2016
1 parent 73576f8 commit 623fc76
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem 'rake', group: [:development, :test]

group :test do
gem 'minitest'
gem 'minitest-reporters', require: 'minitest/reporters'
gem 'simplecov'
gem 'codeclimate-test-reporter'
end
2 changes: 2 additions & 0 deletions Readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ This will add an ellipses after the first element and truncate values longer tha
Pizzazz.ify(all_of_the_things, array_limit: 'etc', value_omission: '... (continued)')
```

You can also supply `omit_root_container` and `prefix`, a string that's added to the beginning of each line of the output, is really nice for doing custom things. Both options can be used independently as well.


### HTML

Expand Down
85 changes: 58 additions & 27 deletions lib/pizzazz/colorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ def initialize(object, options = nil)
@value_limit = options[:value_limit] || 0
@value_omission = options[:value_omission] || '…'
@tab = options[:tab] || ' '
@prefix = options[:prefix]
@omit_root_container = options[:omit_root_container] || false
end

def ify
return '' unless @object
node(@object)

# Parse
output = node(@object, true)
return output unless @prefix

# Add prefix
lines = output.split("\n")
@prefix + lines.join("\n#{@prefix}")
end

private
Expand All @@ -33,57 +42,79 @@ def truncate(string)
(text.length > @value_limit ? text[0...stop] + @value_omission : text).to_s
end

def node(object)
def node(object, root = false)
omit_container = root && @omit_root_container

case object
when String
%Q{<span class="string">"#{truncate(::ERB::Util.h(object))}"</span>}

when Time
%Q{<span class="string">#{object.to_json}</span>}

when TrueClass
%Q{<span class="constant">true</span>}

when FalseClass
%Q{<span class="constant">false</span>}

when NilClass
%Q{<span class="null">null</span>}

when Numeric
%Q{<span class="number">#{object}</span>}

when Hash
if object.length == 0
'{}'
return omit_container ? '' : '{}' if object.length == 0

string = if omit_container
''
else
s = "{\n"
@indent += 1
rows = []
object.keys.collect(&:to_s).sort.each do |key|
value = (object[key] != nil ? object[key] : object[key.to_sym])
rows << %Q{#{tab}<span class="string key">"#{key}"</span>: #{node(value)}}
end
s << rows.join(",\n") + "\n"
"{\n"
end

rows = []
object.keys.collect(&:to_s).sort.each do |key|
value = (object[key] != nil ? object[key] : object[key.to_sym])
rows << %Q{#{tab}<span class="string key">"#{key}"</span>: #{node(value)}}
end
string << rows.join(",\n")

unless omit_container
@indent -= 1
s << "#{tab}}"
s
string << "\n#{tab}}"
end

string

when Array
if object.length == 0
'[]'
return omit_container ? '' : '[]' if object.length == 0
string = if omit_container
''
else
s = "[\n"
@indent += 1
rows = []
array = @array_limit > 0 ? object[0...@array_limit] : object
array.each do |value|
rows << tab + node(value)
end
"[\n"
end

if @array_limit > 0 and object.length > @array_limit
rows << tab + (object[0].is_a?(Hash) ? "{ #{@array_omission} }" : @array_omission)
end
rows = []
array = @array_limit > 0 ? object[0...@array_limit] : object
array.each do |value|
rows << tab + node(value)
end

if @array_limit > 0 and object.length > @array_limit
rows << tab + (object[0].is_a?(Hash) ? "{ #{@array_omission} }" : @array_omission)
end

s << rows.join(",\n") + "\n"
string << rows.join(",\n")

unless omit_container
@indent -= 1
s << "#{tab}]"
s
string << "\n#{tab}]"
end

string
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
CodeClimate::TestReporter.start
SimpleCov.start

Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new

require 'minitest/autorun'
require 'pizzazz'

Expand Down
15 changes: 15 additions & 0 deletions test/units/colorer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ def test_tabs
}}
end

def test_prefix
colored = Pizzazz.ify({:foo => 'bar'}, prefix: '**')
assert_equal colored, %Q{**{
** <span class="string key">"foo"</span>: <span class="string">"bar"</span>
**}}
end

def test_omit_root_container
colored = Pizzazz.ify({:foo => 'bar'}, omit_root_container: true)
assert_equal colored, %Q{<span class="string key">"foo"</span>: <span class="string">"bar"</span>}

colored = Pizzazz.ify([1, 2], omit_root_container: true)
assert_equal colored, %Q{<span class="number">1</span>,\n<span class="number">2</span>}
end

def test_that_it_truncates_arrays
colored = Pizzazz.ify({:numbers => [1, 2, 3]}, :array_limit => 2)
assert_equal colored, %Q{{
Expand Down

0 comments on commit 623fc76

Please sign in to comment.