Class: KerbalDyn::Part::Base
- Inherits:
-
Object
- Object
- KerbalDyn::Part::Base
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
-
#attributes ⇒ Object
readonly
Return the raw attributes hash.
-
#errors ⇒ Object
readonly
Return any errors with this part (usually found during parsing), or an empty array if there were none.
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
#attributes ⇒ Object
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
|
#errors ⇒ Object
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)
dir = Pathname.new(directory)
return nil unless dir.directory?
spec_file = dir + 'part.cfg'
return nil unless spec_file.file?
attributes = {}
errors = []
line_count = 0
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*$/
when /^\s*\/\//
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
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
|
#category ⇒ Object
115
116
117
|
# File 'lib/kerbaldyn/part/base.rb', line 115
def category
return self['category'].to_i
end
|
#category_name ⇒ Object
119
120
121
|
# File 'lib/kerbaldyn/part/base.rb', line 119
def category_name
return CATEGORIES.invert[self.category]
end
|
#cost ⇒ Object
159
160
161
|
# File 'lib/kerbaldyn/part/base.rb', line 159
def cost
return self['cost'].to_i
end
|
#crash_tolerance ⇒ Object
151
152
153
|
# File 'lib/kerbaldyn/part/base.rb', line 151
def crash_tolerance
return self['crashTolerance'].to_f
end
|
#description ⇒ Object
111
112
113
|
# File 'lib/kerbaldyn/part/base.rb', line 111
def description
return self['description']
end
|
#drag ⇒ Object
139
140
141
|
# File 'lib/kerbaldyn/part/base.rb', line 139
def drag
return self.maximum_drag
end
|
#impact_tolerance ⇒ Object
155
156
157
|
# File 'lib/kerbaldyn/part/base.rb', line 155
def impact_tolerance
return self.crash_tolerance
end
|
#mass ⇒ Object
131
132
133
|
# File 'lib/kerbaldyn/part/base.rb', line 131
def mass
return self['mass'].to_f
end
|
#max_temp ⇒ Object
147
148
149
|
# File 'lib/kerbaldyn/part/base.rb', line 147
def max_temp
return self['maxTemp'].to_f
end
|
#maximum_drag ⇒ Object
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_drag ⇒ Object
143
144
145
|
# File 'lib/kerbaldyn/part/base.rb', line 143
def minimum_drag
return self['minimum_drag'] && self['minimum_drag'].to_f
end
|
#module ⇒ Object
123
124
125
|
# File 'lib/kerbaldyn/part/base.rb', line 123
def module
return self['module']
end
|
#module_class ⇒ Object
127
128
129
|
# File 'lib/kerbaldyn/part/base.rb', line 127
def module_class
return self.class.module_class(self.module)
end
|
#name ⇒ Object
103
104
105
|
# File 'lib/kerbaldyn/part/base.rb', line 103
def name
return self['name']
end
|
#title ⇒ Object
107
108
109
|
# File 'lib/kerbaldyn/part/base.rb', line 107
def title
return self['title']
end
|
#to_hash ⇒ Object
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
|