Sha256: 99a26c201e62a9721decb4a0a6d87f864804de58a31038086d000ffcaac5f733

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module CodeApe
  # Regex to parse ape code subsection (notably in {CodeApe#ape})
  REGEX_SUBSECTION = /\A(\d{2})\z/i

  # Represent subsection of ape code (first 2 digits)
  class Subsection
    attr_reader :key, :label

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

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

    # @return [Array<CodeApe::Group>] the groups associated with subsection of this ape code
    def groups
      @groups ||= GROUPS.select { |e| classes.map(&:group_key).include?(e.key) }
    end

    # @return [Array<CodeApe::Division>] the divisions associated with subsection of this ape code
    def divisions
      @divisions ||= DIVISIONS.select { |e| classes.map(&:division_key).include?(e.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 subsection label
    def to_h
      {label: @label}
    end
  end

  # List of ape code subsections
  SUBSECTIONS = NAF_REV2[:subsections].map { |k, v| Subsection.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/subsection.rb