Sha256: aba524b7bca5994795d74032998eef31cdfd68839297a9a5e61fd52a96cc86ec

Contents?: true

Size: 1.38 KB

Versions: 26

Compression:

Stored size: 1.38 KB

Contents

#!/usr/bin/env ruby

data = ''

STDIN.each_line do |line|
  data << line
end

hash = eval(data)

def format(hash, level = 1)
  padding_string = '  '
  indent = (padding_string * (level - 1))
  result = ''
  result << "#{indent}{\n" 
  indent = (padding_string * (level))
  hash.keys.sort.each do |key|
    if hash[key].is_a?(Hash)
      result << "\n"
      result << "#{indent}#{key.inspect} => #{format(hash[key], level + 1)} \n"
      result << "\n"
    else
      result << "#{indent}#{key.inspect} => #{hash[key].inspect}, \n"
    end
  end
  indent = (padding_string * (level - 1))
  result << "#{indent}}" 
  result << ',' if level > 1
  result
end


puts format(hash)


# TODO: namespace, custom type objects, delegation
#
# must expect one hash per line.
# must detect object within arrays.
# must padd array contents.
# must dump array one per line or expand in case of objects.
# must detect string new lines.
# must receive flag for string indentation (default to false?)
# must dump keys in alphabetic order
#
# Formatter::Base
# Formatter::String < Formatter::Base
# Formatter::Integer < Formatter::Base
# Formatter::Array < Formatter::Base
# Formatter::Hash < Formatter::Base
#
# class Formatter::Base
#   def serialize(indent)
#     raise 'child must implement'
#   end
# end
#
# basic object overrides/extensions.
# "".to_formatter #=> Formatter::String
# "".to_formatter.serialize(1) #=> String
#

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
ix-cli-0.0.6 bin/ix-format-ruby-hash
ix-cli-0.0.5 bin/ix-format-ruby-hash
ix-cli-0.0.4 bin/ix-format-ruby-hash
ix-cli-0.0.3 bin/ix-format-ruby-hash
ix-cli-0.0.2 bin/ix-format-ruby-hash
ix-cli-0.0.1 bin/ix-format-ruby-hash