class KerbalDyn::Part::Base

The base-class for all rocket parts. Instances are always of another type, which is determined by the module attribute. For parts that don't have special implementations, the Generic part class is used.

Attributes

attributes[R]

Return the raw attributes hash.

Generally speaking it is better to use to_hash to keep from accidentally altering the part by altering the attributes hash by reference. That being said, this is provided for special/power use cases.

errors[W]

Private method used to set the errors.

Public Class Methods

load_part(directory) click to toggle source

Load the part from a given part directory. This will automatically instantiate the correct subclass.

# File lib/kerbaldyn/part/base.rb, line 13
def self.load_part(directory)
  # Process the argument.
  dir = Pathname.new(directory)
  return nil unless dir.directory?

  # Get a handle on the spec file.
  spec_file = dir + 'part.cfg'
  return nil unless spec_file.file?

  # Initialize the attributes container.
  attributes = {}
  errors = []
  line_count = 0

  # Parse the lines.
  spec_file.read.each_line do |line|
    line_count += 1
    line.chomp!
    line = line.encode('ASCII-8BIT', :invalid => :replace, :replace => '?') unless line.valid_encoding?

    case line
    when /^\s*$/
      # Blank
    when /^\s*\/\//
      # Comments
    when /^(.*)=(.*)/
      key,value = line.split('=', 2).map {|s| s.strip}
      attributes[key] = value
    else
      errors << {:type => :unknown_line, :message => "Unhandled line in #{spec_file.to_s}:#{line_count}: #{line.inspect}"}
    end
  end

  # Now instantiate the right kind of part.
  return self.module_class(attributes['module']).new(attributes).tap {|p| p.send(:errors=, errors)}
end
module_class(module_name) click to toggle source

Return the class to instantiate for a given module attribute.

# File lib/kerbaldyn/part/base.rb, line 51
def self.module_class(module_name)
  ref_mod = Module.nesting[1]
  if( ref_mod.constants.include?(module_name.to_sym) )
    return ref_mod.const_get(module_name.to_sym)
  else
    return ref_mod.const_get(:Generic)
  end
end
new(attributes) click to toggle source

Initialize the part from the hash. Note that this does NOT auto-select the subclass.

# File lib/kerbaldyn/part/base.rb, line 62
def initialize(attributes)
  @attributes = attributes.dup
end

Public Instance Methods

[](attr) click to toggle source

Return the raw attribute value by string or symbol.

It is generally preferrable to use the accessor method.

# File lib/kerbaldyn/part/base.rb, line 86
def [](attr)
  return self.attributes[attr.to_s]
end
category() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 115
def category
  return self['category'].to_i
end
category_name() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 119
def category_name
  return CATEGORIES.invert[self.category]
end
cost() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 159
def cost
  return self['cost'].to_i
end
crash_tolerance() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 151
def crash_tolerance
  return self['crashTolerance'].to_f
end
description() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 111
def description
  return self['description']
end
drag() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 139
def drag
  return self.maximum_drag
end
errors() click to toggle source

Return any errors with this part (usually found during parsing), or an empty array if there were none.

# File lib/kerbaldyn/part/base.rb, line 75
def errors
  return @errors || []
end
impact_tolerance() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 155
def impact_tolerance
  return self.crash_tolerance
end
mass() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 131
def mass
  return self['mass'].to_f
end
max_temp() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 147
def max_temp
  return self['maxTemp'].to_f
end
maximum_drag() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 135
def maximum_drag
  return self['maximum_drag'] && self['maximum_drag'].to_f
end
minimum_drag() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 143
def minimum_drag
  return self['minimum_drag'] && self['minimum_drag'].to_f
end
module() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 123
def module
  return self['module']
end
module_class() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 127
def module_class
  return self.class.module_class(self.module)
end
name() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 103
def name
  return self['name']
end
title() click to toggle source
# File lib/kerbaldyn/part/base.rb, line 107
def title
  return self['title']
end
to_hash() click to toggle source

Return a the part parameters as a hash.

Currently this is implemented as a raw dump of the attributes hash, but in the future it is planned to convert numeric types appropriately.

# File lib/kerbaldyn/part/base.rb, line 94
def to_hash
  return attributes.dup
end
to_json(*args) click to toggle source

Returns a JSON encoded form of the to_hash result.

# File lib/kerbaldyn/part/base.rb, line 99
def to_json(*args)
  return self.to_hash.to_json(*args)
end