Class: Ballistics::Gun
- Inherits:
-
Object
- Object
- Ballistics::Gun
- Defined in:
- lib/ballistics/gun.rb
Defined Under Namespace
Classes: ChamberNotFound
Constant Summary collapse
- YAML_DIR =
'guns'- MANDATORY =
{ "name" => :string, "chamber" => :string, "barrel_length" => :float, "sight_height" => :float, }
- OPTIONAL =
{ "zero_range" => :float, }
- CHAMBER_CARTRIDGE =
map a chamber name to a built-in cartridge YAML filename
{ '5.56' => '5_56', '300 BLK' => '300_blk', # TODO: create these cartridge yamls # '.45 ACP' => '45_acp', # '12ga' => '12ga', }
Instance Attribute Summary collapse
-
#extra ⇒ Object
readonly
Returns the value of attribute extra.
-
#yaml_data ⇒ Object
readonly
Returns the value of attribute yaml_data.
Class Method Summary collapse
-
.find(file: nil, id: nil) ⇒ Object
Load a built-in YAML file and instantiate gun objects Return a hash of gun objects keyed by gun id (per the YAML).
Instance Method Summary collapse
- #cartridge_file ⇒ Object
-
#cartridges ⇒ Object
this will pull in cartridges and projectiles based on the gun chamber.
- #chamber=(new_chamber) ⇒ Object
-
#initialize(hsh) ⇒ Gun
constructor
A new instance of Gun.
- #multiline ⇒ Object
- #params ⇒ Object
Constructor Details
#initialize(hsh) ⇒ Gun
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ballistics/gun.rb', line 38 def initialize(hsh) @yaml_data = hsh MANDATORY.each { |field, type| val = hsh.fetch(field) val = val.to_s if field == "chamber" Ballistics::YAML.check_type!(val, type) self.instance_variable_set("@#{field}", val) } OPTIONAL.each { |field, type| if hsh.key?(field) val = hsh[field] Ballistics::YAML.check_type!(val, type) self.instance_variable_set("@#{field}", val) end } # Keep track of fields that we don't expect @extra = {} (hsh.keys - MANDATORY.keys - OPTIONAL.keys).each { |k| @extra[k] = hsh[k] } @cartridges = {} end |
Instance Attribute Details
#extra ⇒ Object (readonly)
Returns the value of attribute extra.
36 37 38 |
# File 'lib/ballistics/gun.rb', line 36 def extra @extra end |
#yaml_data ⇒ Object (readonly)
Returns the value of attribute yaml_data.
36 37 38 |
# File 'lib/ballistics/gun.rb', line 36 def yaml_data @yaml_data end |
Class Method Details
.find(file: nil, id: nil) ⇒ Object
Load a built-in YAML file and instantiate gun objects Return a hash of gun objects keyed by gun id (per the YAML)
30 31 32 |
# File 'lib/ballistics/gun.rb', line 30 def self.find(file: nil, id: nil) Ballistics::YAML.find(klass: self, file: file, id: id) end |
Instance Method Details
#cartridge_file ⇒ Object
68 69 70 |
# File 'lib/ballistics/gun.rb', line 68 def cartridge_file CHAMBER_CARTRIDGE[@chamber] or raise(ChamberNotFound, @chamber) end |
#cartridges ⇒ Object
this will pull in cartridges and projectiles based on the gun chamber
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ballistics/gun.rb', line 73 def cartridges if @cartridges.empty? require 'ballistics/cartridge' cartridge_file = CHAMBER_CARTRIDGE[@chamber] or raise(ChamberNotFound, @chamber) @cartridges = Ballistics::Cartridge.find_with_projectiles(cartridge_file) end @cartridges end |
#chamber=(new_chamber) ⇒ Object
63 64 65 66 |
# File 'lib/ballistics/gun.rb', line 63 def chamber=(new_chamber) @cartridges = {} @chamber = new_chamber end |
#multiline ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ballistics/gun.rb', line 91 def multiline lines = ["GUN: #{name}", "==="] fields = { "Chamber" => @chamber, "Barrel length" => @barrel_length, "Sight height" => @sight_height, } fields["Zero Range"] = @zero_range if @zero_range fields.each { |name, val| lines << [name.rjust(13, ' ' ), val].join(': ') } lines.join("\n") end |
#params ⇒ Object
85 86 87 88 89 |
# File 'lib/ballistics/gun.rb', line 85 def params params = { sight_height: @sight_height } params[:zero_range] = @zero_range if @zero_range params end |