Permalink
Browse files

Initial import and setup tests

  • Loading branch information...
1 parent a6c7ad9 commit 4be0898c0489bbdf176d04b3fd0c7052aee5cd14 @soffes soffes committed Jul 10, 2012
Showing with 99 additions and 3 deletions.
  1. +4 −0 Gemfile
  2. +7 −0 Rakefile
  3. +2 −0 lib/pizzazz.rb
  4. +74 −0 lib/pizzazz/ify.rb
  5. +1 −3 pizzazz.gemspec
  6. +4 −0 test/test_helper.rb
  7. +7 −0 test/units/pizzazz_test.rb
View
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in pizzazz.gemspec
gemspec
+
+gem 'rake'
+gem 'minitest'
+gem 'minitest-wscolor'
View
@@ -1,2 +1,9 @@
#!/usr/bin/env rake
require 'bundler/gem_tasks'
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+end
+task default: :test
View
@@ -1,4 +1,6 @@
+require 'pizzazz/ify'
require 'pizzazz/version'
module Pizzazz
+ TAB_SIZE = 2
end
View
@@ -0,0 +1,74 @@
+# encoding: UTF-8
+
+class Pizzazz
+ def self.ify(object, options = nil)
+ p = self.new(object, options)
+ p.ify
+ end
+
+ def initialize(object, options = {})
+ @object = object
+ @indent = 0
+ @limit = (options[:limit] or 0)
+ end
+
+ def ify
+ node(@object, @limit)
+ end
+
+private
+
+ def tab
+ " " * @indent * TAB_SIZE
+ end
+
+ def node(object, limit = 0)
+ case object
+ when String
+ %Q{<span class="string">"#{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="constant">null</span>}
+ when Numeric
+ %Q{<span class="number">#{object}</span>}
+ when Hash
+ s = "{\n"
+ @indent += TAB_SIZE
+ 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}"</span>: #{node(value)}}
+ end
+ s << rows.join(",\n") + "\n"
+ @indent -= TAB_SIZE
+ s << "#{tab}}"
+ s
+ when Array
+ if object.length == 0
+ "[]"
+ else
+ s = "[\n"
+ @indent += TAB_SIZE
+ rows = []
+ array = @limit > 0 ? object[0...limit] : object
+ array.each do |value|
+ rows << tab + node(value)
+ end
+
+ if limit > 0 and object.length > limit
+ rows << tab + (object[0].is_a?(Hash) ? '{ … }' : '')
+ end
+
+ s << rows.join(",\n") + "\n"
+ @indent -= TAB_SIZE
+ s << "#{tab}]"
+ s
+ end
+ end
+ end
+end
View
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# encoding: UTF-8
require File.expand_path('../lib/pizzazz/version', __FILE__)
Gem::Specification.new do |gem|
@@ -14,6 +14,4 @@ Gem::Specification.new do |gem|
gem.name = 'pizzazz'
gem.require_paths = ['lib']
gem.version = Pizzazz::VERSION
-
- gem.add_development_dependency 'rake'
end
View
@@ -0,0 +1,4 @@
+require 'rubygems'
+gem 'minitest'
+require 'minitest/wscolor'
+require 'minitest/autorun'
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TestPizzazz < MiniTest::Unit::TestCase
+ def test_that_it_around
+ assert Pizzazz
+ end
+end

0 comments on commit 4be0898

Please sign in to comment.