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.27 bin/ix-format-ruby-hash
ix-cli-0.0.26 bin/ix-format-ruby-hash
ix-cli-0.0.25 bin/ix-format-ruby-hash
ix-cli-0.0.24 bin/ix-format-ruby-hash
ix-cli-0.0.23 bin/ix-format-ruby-hash
ix-cli-0.0.22 bin/ix-format-ruby-hash
ix-cli-0.0.21 bin/ix-format-ruby-hash
ix-cli-0.0.20 bin/ix-format-ruby-hash
ix-cli-0.0.19 bin/ix-format-ruby-hash
ix-cli-0.0.18 bin/ix-format-ruby-hash
ix-cli-0.0.17 bin/ix-format-ruby-hash
ix-cli-0.0.16 bin/ix-format-ruby-hash
ix-cli-0.0.15 bin/ix-format-ruby-hash
ix-cli-0.0.14 bin/ix-format-ruby-hash
ix-cli-0.0.13 bin/ix-format-ruby-hash
ix-cli-0.0.12 bin/ix-format-ruby-hash
ix-cli-0.0.11 bin/ix-format-ruby-hash
ix-cli-0.0.10 bin/ix-format-ruby-hash
ix-cli-0.0.9 bin/ix-format-ruby-hash
ix-cli-0.0.7 bin/ix-format-ruby-hash