Sha256: 0acf46a2eca7fe213e299632ec7fb09712ff358a5790cc08e6c9586af07038d6

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module CodeApe
  # Regex to parse ape code group (notably in {CodeApe#ape})
  REGEX_GROUP = /\A(\d{2})\.?(\d{2})\z/i

  # Represent group of ape code (all digits)
  class Group
    attr_reader :key, :label

    # Creates a new {Group}
    # @param key [Symbol, String] The group key of ape code
    # @param label [String] The group label of ape code
    def initialize(key, label)
      @key = key.to_s
      @label = label
    end

    # @return [Array<CodeApe::Class>] the classes associated with group of this ape code
    def classes
      @classes ||= CLASSES.select { |e| e.group_key == @key }
    end

    # @return [CodeApe::Division] the division of this ape code
    def division
      @division ||= DIVISIONS.find { |e| e.key == classes[0]&.division_key }
    end

    # @return [CodeApe::Subsection] the subsection of this ape code
    def subsection
      @subsection ||= SUBSECTIONS.find { |e| e.key == classes[0]&.subsection_key }
    end

    # @return [CodeApe::Section] the section of this ape code
    def section
      @section ||= SECTIONS.find { |e| e.key == classes[0]&.section_key }
    end

    # @return [Hash] the hash representation of group label
    def to_h
      {label: @label}
    end
  end

  # List of ape code groups
  GROUPS = NAF_REV2[:groups].map { |k, v| Group.new(k, v) }.freeze
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
code_ape-3.0.0 lib/code_ape/group.rb