Module: Razor::CLI::Format
- Defined in:
- lib/razor/cli/format.rb
Constant Summary collapse
- PriorityKeys =
%w[ id name ]
- SpecNames =
{ "/spec/object/policy" => "Policy", "/spec/object/tag" => "Tag", "/spec/object/reference" => "reference" }
Class Method Summary collapse
Instance Method Summary collapse
- #format_default_object(object, indent = 0) ⇒ Object
- #format_document(doc) ⇒ Object
- #format_object(object, indent = 0) ⇒ Object
-
#format_objects(objects, indent = 0) ⇒ Object
We assume that all collections are homogenous.
- #format_reference_object(ref, indent = 0) ⇒ Object
Class Method Details
.spec_name(spec) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/razor/cli/format.rb', line 12 def self.spec_name(spec) path = spec && URI.parse(spec).path SpecNames[path] || path rescue => e spec end |
Instance Method Details
#format_default_object(object, indent = 0) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/razor/cli/format.rb', line 47 def format_default_object(object, indent = 0 ) fields = (PriorityKeys & object.keys) + (object.keys - PriorityKeys) key_indent = indent + fields.map {|f| f.length}.max output = "" fields.map do |f| value = object[f] output = "#{f.rjust key_indent + 2}: " output << case value when Hash if value.empty? "{}" else "\n" + format_object(value, key_indent + 4).rstrip end when Array if value.all? { |v| v.is_a?(String) } "[" + value.map(&:inspect).join(",") + "]" else "[\n" + format_objects(value, key_indent + 6) + ("\n"+' '*(key_indent+4)+"]") end else case f when "spec" then "\"#{Format.spec_name(value)}\"" else value.inspect end end end.join "\n" end |
#format_document(doc) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/razor/cli/format.rb', line 19 def format_document(doc) case doc when Array then format_objects(doc) when Hash then format_object(doc) else doc.to_s end.chomp end |
#format_object(object, indent = 0) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/razor/cli/format.rb', line 39 def format_object(object, indent = 0) if object.keys == ["id", "name"] format_reference_object(object, indent) else format_default_object(object, indent) end end |
#format_objects(objects, indent = 0) ⇒ Object
We assume that all collections are homogenous
28 29 30 31 32 |
# File 'lib/razor/cli/format.rb', line 28 def format_objects(objects, indent = 0) objects.map do |obj| obj.is_a?(Hash) ? format_object(obj, indent) : ' '*indent + obj.inspect end.join "\n\n" end |
#format_reference_object(ref, indent = 0) ⇒ Object
34 35 36 |
# File 'lib/razor/cli/format.rb', line 34 def format_reference_object(ref, indent = 0) output = ' '* indent + "#{ref['name']} => #{ref['id'].to_s.ljust 4}" end |