class KerbalDyn::PartLibrary

PartLibrary is an array like list of Part objects with convenience methods for common operations.

Public Class Methods

load_parts(directory) click to toggle source

Loads parts from a given directory into the library.

# File lib/kerbaldyn/part_library.rb, line 10
def self.load_parts(directory)
  dir = Pathname.new(directory)
  raise nil unless dir.directory?

  parts = dir.children.reject do |part_dir|
    # Reject if it starts with a dot or is not a directory.
    (part_dir.basename.to_s =~ /^\./) || !part_dir.directory?
  end.inject([]) do |parts, part_dir|
    parts << Part::Base.load_part(part_dir)
  end

  return self.new(parts)
end
new(*parts) click to toggle source

Initialize with an array of parts or a list of parts.

# File lib/kerbaldyn/part_library.rb, line 25
def initialize(*parts)
  @parts = parts.to_a.flatten
end

Public Instance Methods

[](val) click to toggle source

Returns the part with the given index or directory name

# File lib/kerbaldyn/part_library.rb, line 42
def [](val)
  case val
  when Numeric
    return @parts[val]
  else
    return @parts.find {|part| part.directory_name == val}
  end
end
each(&block) click to toggle source

Iterates over each part using a block.

This is the root for all Enumerable methods.

# File lib/kerbaldyn/part_library.rb, line 32
def each(&block)
  return @parts.each(&block)
end
length() click to toggle source

The length

# File lib/kerbaldyn/part_library.rb, line 37
def length
  return @parts.length
end
to_a() click to toggle source

Returns all the parts in the library, in an array.

# File lib/kerbaldyn/part_library.rb, line 52
def to_a
  return @parts.to_a.dup
end
to_json(*args) click to toggle source

Returns the parts library as JSONified parts.

# File lib/kerbaldyn/part_library.rb, line 57
def to_json(*args)
  return @parts.to_json(*args)
end