lib/ika3/weapons.rb in ika3-0.8.1 vs lib/ika3/weapons.rb in ika3-0.9.0

- old
+ new

@@ -1,32 +1,32 @@ # frozen_string_literal: true module Ika3 class Weapon - ATTRIBUTES = %i[ - name sub special - ].freeze + class << self + class W + attr_reader :name, :sub, :special - ATTRIBUTES.each do |attribute| - define_method attribute do - self[attribute] + def initialize(data) + @name = data[:name] + @sub = data[:sub] + @special = data[:special] + end end - end - class << self - include Ika3::Concerns::Utils + private_constant :W def find(weapon_key) raise "unknown weapon: #{weapon_key}" unless valid?(weapon_key) @cache ||= {} unless @cache[weapon_key] weapon_config = config[weapon_key] || {} @cache[weapon_key] = weapon_config end - @cache[weapon_key] + W.new(@cache[weapon_key]) end def find_by_name(weapon_name) raise "unknown weapon: #{weapon_name}" unless weapons.values.include?(weapon_name) @@ -35,11 +35,12 @@ end def filter_by_sub(sub_name) raise "unknown sub weapon: #{sub_name}" unless sub_weapons.values.include?(sub_name) - config.values.filter { |weapon| weapon[:sub] == sub_name } + weapons = config.values.filter { |weapon| weapon[:sub] == sub_name } + weapons.map { |weapon| W.new(weapon) } end def reload_config! @cache = {} @config = nil @@ -84,9 +85,13 @@ @sub_weapons end def valid?(weapon_key) names.include?(weapon_key) + end + + def load_yaml_file(file) + YAML.safe_load_file(file, aliases: true) end end end end