class Depix::Describe

Returns terminal-ready colorized descriptions of DPX headers per file

Public Instance Methods

describe(path, compact = false) click to toggle source

Returns a printable report on all the headers present in the file at the path passed

# File lib/depix/describe.rb, line 6
def describe(path, compact = false)
  struct = Depix.from_file(path, compact)
  describe_struct(struct) + describe_synthetics_of_struct(struct)
end
describe_synthetics(path, compact) click to toggle source

Returns descriptions of the shorthand synthetic properties

# File lib/depix/describe.rb, line 12
def describe_synthetics(path, compact)
  struct = Depix.from_file(path, compact)
  describe_synthetics_of_struct(struct)
end

Private Instance Methods

describe_struct(result, pad_offset = 0) click to toggle source

Describe a filled DPX structure

# File lib/depix/describe.rb, line 20
def describe_struct(result, pad_offset = 0)
  result.class.fields.inject([]) do | info, field |
    value = result.send(field.name)
    parts = []
    if value
      parts << " " * pad_offset
      parts << red { field.name.to_s }
      parts << "(#{field.desc})" if field.desc
      parts << if field.is_a?(Depix::Binary::Fields::InnerField)
        describe_struct(value, pad_offset + 1)
      elsif field.is_a?(Depix::Binary::Fields::ArrayField)
        value.map { | v | v.is_a?(Depix::Binary::Structure) ? describe_struct(v, pad_offset + 1) : v }
      else
        blue { value.to_s }
      end
    end
    if parts.any?
      info << parts.join(' ')
    end
    info
  end.map{|e| ('  ' * pad_offset) + e }.join("\n")
end
describe_synthetics_of_struct(struct) click to toggle source
# File lib/depix/describe.rb, line 44
def describe_synthetics_of_struct(struct)
  fields = Depix::Synthetics.instance_methods.reject{|m| m.to_s.include?('=')}.map do | m |
    [red{ m.to_s }, blue { struct.send(m).to_s }].join(' : ')
  end
  fields.unshift("============")
  fields.unshift(bold { "\nSynthetic properties" })
  fields.join("\n")
end