Class: KerbalDyn::PartLibrary

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kerbaldyn/part_library.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*parts) ⇒ PartLibrary

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



25
26
27
# File 'lib/kerbaldyn/part_library.rb', line 25

def initialize(*parts)
  @parts = parts.to_a.flatten
end

Class Method Details

.load_parts(directory) ⇒ Object

Loads parts from a given directory into the library.

Raises:

  • (nil)


10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#[](val) ⇒ Object

Returns the part with the given index or directory name



42
43
44
45
46
47
48
49
# 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) ⇒ Object

Iterates over each part using a block.

This is the root for all Enumerable methods.



32
33
34
# File 'lib/kerbaldyn/part_library.rb', line 32

def each(&block)
  return @parts.each(&block)
end

#lengthObject

The length



37
38
39
# File 'lib/kerbaldyn/part_library.rb', line 37

def length
  return @parts.length
end

#to_aObject

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



52
53
54
# File 'lib/kerbaldyn/part_library.rb', line 52

def to_a
  return @parts.to_a.dup
end

#to_json(*args) ⇒ Object

Returns the parts library as JSONified parts.



57
58
59
# File 'lib/kerbaldyn/part_library.rb', line 57

def to_json(*args)
  return @parts.to_json(*args)
end