Class: Archive::Pecan

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/pecan.rb,
lib/archive/pecan/blob.rb,
lib/archive/pecan/version.rb,
lib/archive/pecan/attribute.rb

Overview

Deals with virtual electronic component files known as Pecan component archives.

Author:

Defined Under Namespace

Classes: Attribute, Blob

Constant Summary collapse

MANIFEST_FILE =
'manifest.tsv'
PARAM_FILE =
'parameters.tsv'
IMAGE_FILE =
'image.bmp'
DATASHEET_FILE =
'datasheet.pdf'
VERSION =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePecan

Initializes a brand new, empty, component archive object.



34
35
36
37
38
39
# File 'lib/archive/pecan.rb', line 34

def initialize
  @attribs = []
  @params = []
  @image = nil
  @datasheet = nil
end

Instance Attribute Details

#attribsArray<Attribute>

Returns Manifest attributes.

Returns:

  • (Array<Attribute>)

    Manifest attributes.



22
23
24
# File 'lib/archive/pecan.rb', line 22

def attribs
  @attribs
end

#datasheetBlob

Returns Binary blob of the PDF datasheet.

Returns:

  • (Blob)

    Binary blob of the PDF datasheet.



31
32
33
# File 'lib/archive/pecan.rb', line 31

def datasheet
  @datasheet
end

#imageImage

Returns Component image file blob.

Returns:

  • (Image)

    Component image file blob.



28
29
30
# File 'lib/archive/pecan.rb', line 28

def image
  @image
end

#paramsArray<Attribute>

Returns Parameter attributes.

Returns:

  • (Array<Attribute>)

    Parameter attributes.



25
26
27
# File 'lib/archive/pecan.rb', line 25

def params
  @params
end

Class Method Details

.read_archive(path) ⇒ Archive

Reads an component archive and returns an populated object that represents it.

Parameters:

  • path (String)

    Path to the component archive to be read.

Returns:

  • (Archive)

    Object representation of the component archive.



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
75
76
77
78
79
# File 'lib/archive/pecan.rb', line 47

def self.read_archive(path)
  archive = new

  # Open the TAR archive.
  Minitar::Input.open(File.open(path, 'rb')) do |tar|
    # Go through entries inside the tar file.
    tar.each do |entry|
      # Parse each file in its own way.
      case entry.full_name
      when MANIFEST_FILE
        # Manifest attributes file.
        StringIO.new(entry.read).each_line do |line|
          attr = Attribute.from_line(line)
          archive.attribs << attr unless attr.nil?
        end
      when PARAM_FILE
        # Parameters attributes file.
        StringIO.new(entry.read).each_line do |line|
          attr = Attribute.from_line(line)
          archive.params << attr unless attr.nil?
        end
      when IMAGE_FILE
        # Component image file.
        archive.image = Blob.new('image/bmp', entry.read)
      when DATASHEET_FILE
        # Component datasheet file.
        archive.datasheet = Blob.new('application/pdf', entry.read)
      end
    end
  end

  archive
end

Instance Method Details

#dump_contentsObject

Dumps the contents of the object in a human-readable format.



82
83
84
85
86
87
88
89
90
91
# File 'lib/archive/pecan.rb', line 82

def dump_contents
  puts "Manifest: [#{@attribs.length}]"
  @attribs.each { |attr| puts "    '#{attr.name}' = '#{attr.value}'" }

  puts "Parameters: [#{@params.length}]"
  @params.each { |attr| puts "    '#{attr.name}' = '#{attr.value}'" }

  puts 'Has image' unless @image.nil?
  puts 'Has datasheet' unless @datasheet.nil?
end