lib/code_ape.rb in code_ape-2.0.2 vs lib/code_ape.rb in code_ape-3.0.0

- old
+ new

@@ -1,38 +1,37 @@ -require 'code_ape/version' +require "code_ape/version" -require 'yaml' +require "code_ape/constants" -module CodeApe - NAF_REV2 ||= YAML.load_file(File.join(File.dirname(__FILE__), 'naf_rev2.yml')) +require "code_ape/section" +require "code_ape/subsection" +require "code_ape/division" +require "code_ape/group" +require "code_ape/class" - #DEPRECATED - DIVISION_APE ||= YAML.load_file(File.join(File.dirname(__FILE__), 'division_ape.yml')) - CODE_APE ||= YAML.load_file(File.join(File.dirname(__FILE__), 'code_ape.yml')) - +# All CodeApe functionality +module CodeApe + # Takes a ape code and returns data if possible + # @param code [String] The ape code to search + # @return [CodeApe::Subsection, CodeApe::Division, CodeApe::Group, CodeApe::Class, nil] the ape code data def self.ape(code) - if NAF_REV2.key?(code.to_s) - NAF_REV2[code.to_s] - else - nil - end - end + upcased_code = code&.upcase - # DEPRECATED - def self.division(code) - if DIVISION_APE.key?(code) - DIVISION_APE[code] - else - nil + case upcased_code + when REGEX_SUBSECTION + SUBSECTIONS.find { |e| e.key == upcased_code } + when REGEX_DIVISION + DIVISIONS.find { |e| e.key == upcased_code || e.key.delete(".") == upcased_code } + when REGEX_GROUP + GROUPS.find { |e| e.key == upcased_code || e.key.delete(".") == upcased_code } + when REGEX_CLASS + CLASSES.find { |e| e.key == upcased_code || e.key.delete(".") == upcased_code } end end - #DEPRECATED - def self.activity(code) - if CODE_APE.key?(code) - CODE_APE[code] - else - nil - end + # Takes a section code and returns data if possible + # @param code [String] The section code to search + # @return [CodeApe::Section, nil] the section code data + def self.section(code) + SECTIONS.find { |e| e.key == code&.upcase } end - end