Class: KerbalDyn::Part::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kerbaldyn/part/base.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

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



62
63
64
# File 'lib/kerbaldyn/part/base.rb', line 62

def initialize(attributes)
  @attributes = attributes.dup
end

Instance Attribute Details

#attributesObject (readonly)

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.



71
72
73
# File 'lib/kerbaldyn/part/base.rb', line 71

def attributes
  @attributes
end

#errorsObject

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



75
76
77
# File 'lib/kerbaldyn/part/base.rb', line 75

def errors
  return @errors || []
end

Class Method Details

.load_part(directory) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object

Return the class to instantiate for a given module attribute.



51
52
53
54
55
56
57
58
# 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

Instance Method Details

#[](attr) ⇒ Object

Return the raw attribute value by string or symbol.

It is generally preferrable to use the accessor method.



86
87
88
# File 'lib/kerbaldyn/part/base.rb', line 86

def [](attr)
  return self.attributes[attr.to_s]
end

#categoryObject



115
116
117
# File 'lib/kerbaldyn/part/base.rb', line 115

def category
  return self['category'].to_i
end

#category_nameObject



119
120
121
# File 'lib/kerbaldyn/part/base.rb', line 119

def category_name
  return CATEGORIES.invert[self.category]
end

#costObject



159
160
161
# File 'lib/kerbaldyn/part/base.rb', line 159

def cost
  return self['cost'].to_i
end

#crash_toleranceObject



151
152
153
# File 'lib/kerbaldyn/part/base.rb', line 151

def crash_tolerance
  return self['crashTolerance'].to_f
end

#descriptionObject



111
112
113
# File 'lib/kerbaldyn/part/base.rb', line 111

def description
  return self['description']
end

#dragObject



139
140
141
# File 'lib/kerbaldyn/part/base.rb', line 139

def drag
  return self.maximum_drag
end

#impact_toleranceObject



155
156
157
# File 'lib/kerbaldyn/part/base.rb', line 155

def impact_tolerance
  return self.crash_tolerance
end

#massObject



131
132
133
# File 'lib/kerbaldyn/part/base.rb', line 131

def mass
  return self['mass'].to_f
end

#max_tempObject



147
148
149
# File 'lib/kerbaldyn/part/base.rb', line 147

def max_temp
  return self['maxTemp'].to_f
end

#maximum_dragObject



135
136
137
# File 'lib/kerbaldyn/part/base.rb', line 135

def maximum_drag
  return self['maximum_drag'] && self['maximum_drag'].to_f
end

#minimum_dragObject



143
144
145
# File 'lib/kerbaldyn/part/base.rb', line 143

def minimum_drag
  return self['minimum_drag'] && self['minimum_drag'].to_f
end

#moduleObject



123
124
125
# File 'lib/kerbaldyn/part/base.rb', line 123

def module
  return self['module']
end

#module_classObject



127
128
129
# File 'lib/kerbaldyn/part/base.rb', line 127

def module_class
  return self.class.module_class(self.module)
end

#nameObject



103
104
105
# File 'lib/kerbaldyn/part/base.rb', line 103

def name
  return self['name']
end

#titleObject



107
108
109
# File 'lib/kerbaldyn/part/base.rb', line 107

def title
  return self['title']
end

#to_hashObject

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.



94
95
96
# File 'lib/kerbaldyn/part/base.rb', line 94

def to_hash
  return attributes.dup
end

#to_json(*args) ⇒ Object

Returns a JSON encoded form of the to_hash result.



99
100
101
# File 'lib/kerbaldyn/part/base.rb', line 99

def to_json(*args)
  return self.to_hash.to_json(*args)
end