code_ape 2.0.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f4950121e8fd3953ee799d787ea5ed7ba556bf1e2072aeec3afe2516d7d7e19
4
- data.tar.gz: dcb4d480021614c13ad3132b0b4378e7524dc32c04531bc9b77dd2b1e777ef8a
3
+ metadata.gz: e09df670abbb4f129bfd0d7863ae2b7a18e0465fe1430ba6fa0dfa1257f7e1f0
4
+ data.tar.gz: 65c006c76b7cc4cf8a7eea854399a656341ef50bfabb15ceb1bf0d0b9c8aedbd
5
5
  SHA512:
6
- metadata.gz: 817c44e5f430f0368aa94e6c81c732171b3c952bb6b54c6e77f235d57f077ff2bdb1c7a39a93ebf5fdc9041b3ade9d3868745897dccaa40f1fe3a849479ab590
7
- data.tar.gz: 68788da4f284b4dc8dc37a5c1639e453718cfc20f58d77c8ececb6815038cee5ed6ccbbac8e10d856b26a837ea1265fc7166da659b220af6e0fa050be85f3c91
6
+ metadata.gz: cec0f4acc73693ca049d9ee78056c868cdb85091e71be78f4b8ee81fea8347885dba5bd6007571d32f36ba240ab855919d2de6c1c77468494c8f3d73f62d5f3d
7
+ data.tar.gz: ee3058ecd2cd103ada9c53a340b48a6708d921531175dc39ea9a70e8e678b1e0cfd1b58c83e486ecab382f65e3eddbaedc28027686c15b2725811a7f363aee32
@@ -0,0 +1,54 @@
1
+ module CodeApe
2
+ # Regex to parse ape code class (notably in {CodeApe#ape})
3
+ REGEX_CLASS = /\A(\d{2})\.?(\d{2})([A-Z])\z/i
4
+
5
+ # Represent final ape code (all digits + letter)
6
+ class Class
7
+ attr_reader :key, :label
8
+ attr_reader :group_key, :division_key, :subsection_key, :section_key
9
+
10
+ # Creates a new {Class}
11
+ # @param key [Symbol, String] The class key of ape code
12
+ # @param label [String] The class label of ape code
13
+ # @param section_key [Symbol, String] The section key of ape code
14
+ # @param subsection_key [Symbol, String] The subsection key of ape code
15
+ # @param division_key [Symbol, String] The division key of ape code
16
+ # @param group_key [Symbol, String] The group key of ape code
17
+ def initialize(key, label, section_key, subsection_key, division_key, group_key)
18
+ @key = key.to_s
19
+ @label = label
20
+ @section_key = section_key.to_s
21
+ @subsection_key = subsection_key.to_s
22
+ @division_key = division_key.to_s
23
+ @group_key = group_key.to_s
24
+ end
25
+
26
+ # @return [CodeApe::Group] the group of this ape code
27
+ def group
28
+ @group ||= GROUPS.find { |e| e.key == @group_key }
29
+ end
30
+
31
+ # @return [CodeApe::Division] the division of this ape code
32
+ def division
33
+ @division ||= DIVISIONS.find { |e| e.key == @division_key }
34
+ end
35
+
36
+ # @return [CodeApe::Subsection] the subsection of this ape code
37
+ def subsection
38
+ @subsection ||= SUBSECTIONS.find { |e| e.key == @subsection_key }
39
+ end
40
+
41
+ # @return [CodeApe::Section] the section of this ape code
42
+ def section
43
+ @section ||= SECTIONS.find { |e| e.key == @section_key }
44
+ end
45
+
46
+ # @return [Hash] the hash representation of class label
47
+ def to_h
48
+ {label: @label}
49
+ end
50
+ end
51
+
52
+ # List of ape code classes
53
+ CLASSES = NAF_REV2[:classes].map { |k, v| Class.new(k, v[:label], v[:section], v[:subsection], v[:division], v[:group]) }.freeze
54
+ end
@@ -0,0 +1,6 @@
1
+ module CodeApe
2
+ require "json"
3
+
4
+ # The content of naf_rev2.json (all ape code data)
5
+ NAF_REV2 = JSON.parse(File.read(File.join(File.dirname(__FILE__), "../naf_rev2.json")), symbolize_names: true)
6
+ end
@@ -0,0 +1,45 @@
1
+ module CodeApe
2
+ # Regex to parse ape code division (notably in {CodeApe#ape})
3
+ REGEX_DIVISION = /\A(\d{2})\.?(\d{1})\z/i
4
+
5
+ # Represent division of ape code (first 3 digits)
6
+ class Division
7
+ attr_reader :key, :label
8
+
9
+ # Creates a new {Division}
10
+ # @param key [Symbol, String] The division key of ape code
11
+ # @param label [String] The division label of ape code
12
+ def initialize(key, label)
13
+ @key = key.to_s
14
+ @label = label
15
+ end
16
+
17
+ # @return [Array<CodeApe::Class>] the classes associated with division of this ape code
18
+ def classes
19
+ @classes ||= CLASSES.select { |e| e.division_key == @key }
20
+ end
21
+
22
+ # @return [Array<CodeApe::Group>] the groups associated with division of this ape code
23
+ def groups
24
+ @groups ||= GROUPS.select { |e| classes.map(&:group_key).include?(e.key) }
25
+ end
26
+
27
+ # @return [CodeApe::Subsection] the subsection of this ape code
28
+ def subsection
29
+ @subsection ||= SUBSECTIONS.find { |e| e.key == classes[0]&.subsection_key }
30
+ end
31
+
32
+ # @return [CodeApe::Section] the section of this ape code
33
+ def section
34
+ @section ||= SECTIONS.find { |e| e.key == classes[0]&.section_key }
35
+ end
36
+
37
+ # @return [Hash] the hash representation of division label
38
+ def to_h
39
+ {label: @label}
40
+ end
41
+ end
42
+
43
+ # List of ape code divisions
44
+ DIVISIONS = NAF_REV2[:divisions].map { |k, v| Division.new(k, v) }.freeze
45
+ end
@@ -0,0 +1,45 @@
1
+ module CodeApe
2
+ # Regex to parse ape code group (notably in {CodeApe#ape})
3
+ REGEX_GROUP = /\A(\d{2})\.?(\d{2})\z/i
4
+
5
+ # Represent group of ape code (all digits)
6
+ class Group
7
+ attr_reader :key, :label
8
+
9
+ # Creates a new {Group}
10
+ # @param key [Symbol, String] The group key of ape code
11
+ # @param label [String] The group label of ape code
12
+ def initialize(key, label)
13
+ @key = key.to_s
14
+ @label = label
15
+ end
16
+
17
+ # @return [Array<CodeApe::Class>] the classes associated with group of this ape code
18
+ def classes
19
+ @classes ||= CLASSES.select { |e| e.group_key == @key }
20
+ end
21
+
22
+ # @return [CodeApe::Division] the division of this ape code
23
+ def division
24
+ @division ||= DIVISIONS.find { |e| e.key == classes[0]&.division_key }
25
+ end
26
+
27
+ # @return [CodeApe::Subsection] the subsection of this ape code
28
+ def subsection
29
+ @subsection ||= SUBSECTIONS.find { |e| e.key == classes[0]&.subsection_key }
30
+ end
31
+
32
+ # @return [CodeApe::Section] the section of this ape code
33
+ def section
34
+ @section ||= SECTIONS.find { |e| e.key == classes[0]&.section_key }
35
+ end
36
+
37
+ # @return [Hash] the hash representation of group label
38
+ def to_h
39
+ {label: @label}
40
+ end
41
+ end
42
+
43
+ # List of ape code groups
44
+ GROUPS = NAF_REV2[:groups].map { |k, v| Group.new(k, v) }.freeze
45
+ end
@@ -0,0 +1,42 @@
1
+ module CodeApe
2
+ # Represent section of ape code (A letter assigned to a subdivision grouping not represented in the final ape code)
3
+ class Section
4
+ attr_reader :key, :label
5
+
6
+ # Creates a new {Section}
7
+ # @param key [Symbol, String] The section key of ape code
8
+ # @param label [String] The section label of ape code
9
+ def initialize(key, label)
10
+ @key = key.to_s
11
+ @label = label
12
+ end
13
+
14
+ # @return [Array<CodeApe::Class>] the classes associated with section of this ape code
15
+ def classes
16
+ @classes ||= CLASSES.select { |e| e.section_key == @key }
17
+ end
18
+
19
+ # @return [Array<CodeApe::Group>] the groups associated with section of this ape code
20
+ def groups
21
+ @groups ||= GROUPS.select { |e| classes.map(&:group_key).include?(e.key) }
22
+ end
23
+
24
+ # @return [Array<CodeApe::Division>] the divisions associated with section of this ape code
25
+ def divisions
26
+ @divisions ||= DIVISIONS.select { |e| classes.map(&:division_key).include?(e.key) }
27
+ end
28
+
29
+ # @return [Array<CodeApe::Subsection>] the subsections associated with section of this ape code
30
+ def subsections
31
+ @subsections ||= SUBSECTIONS.select { |e| classes.map(&:subsection_key).include?(e.key) }
32
+ end
33
+
34
+ # @return [Hash] the hash representation of section label
35
+ def to_h
36
+ {label: @label}
37
+ end
38
+ end
39
+
40
+ # List of ape code sections
41
+ SECTIONS = NAF_REV2[:sections].map { |k, v| Section.new(k, v) }.freeze
42
+ end
@@ -0,0 +1,45 @@
1
+ module CodeApe
2
+ # Regex to parse ape code subsection (notably in {CodeApe#ape})
3
+ REGEX_SUBSECTION = /\A(\d{2})\z/i
4
+
5
+ # Represent subsection of ape code (first 2 digits)
6
+ class Subsection
7
+ attr_reader :key, :label
8
+
9
+ # Creates a new {Subsection}
10
+ # @param key [Symbol, String] The subsection key of ape code
11
+ # @param label [String] The subsection label of ape code
12
+ def initialize(key, label)
13
+ @key = key.to_s
14
+ @label = label
15
+ end
16
+
17
+ # @return [Array<CodeApe::Class>] the classes associated with subsection of this ape code
18
+ def classes
19
+ @classes ||= CLASSES.select { |e| e.subsection_key == @key }
20
+ end
21
+
22
+ # @return [Array<CodeApe::Group>] the groups associated with subsection of this ape code
23
+ def groups
24
+ @groups ||= GROUPS.select { |e| classes.map(&:group_key).include?(e.key) }
25
+ end
26
+
27
+ # @return [Array<CodeApe::Division>] the divisions associated with subsection of this ape code
28
+ def divisions
29
+ @divisions ||= DIVISIONS.select { |e| classes.map(&:division_key).include?(e.key) }
30
+ end
31
+
32
+ # @return [CodeApe::Section] the section of this ape code
33
+ def section
34
+ @section ||= SECTIONS.find { |e| e.key == classes[0]&.section_key }
35
+ end
36
+
37
+ # @return [Hash] the hash representation of subsection label
38
+ def to_h
39
+ {label: @label}
40
+ end
41
+ end
42
+
43
+ # List of ape code subsections
44
+ SUBSECTIONS = NAF_REV2[:subsections].map { |k, v| Subsection.new(k, v) }.freeze
45
+ end
@@ -1,3 +1,4 @@
1
1
  module CodeApe
2
- VERSION = '2.0.2'.freeze
2
+ # The current version of code_ape
3
+ VERSION = "3.0.0".freeze unless const_defined?(:VERSION)
3
4
  end
data/lib/code_ape.rb CHANGED
@@ -1,38 +1,37 @@
1
- require 'code_ape/version'
1
+ require "code_ape/version"
2
2
 
3
- require 'yaml'
3
+ require "code_ape/constants"
4
4
 
5
- module CodeApe
6
- NAF_REV2 ||= YAML.load_file(File.join(File.dirname(__FILE__), 'naf_rev2.yml'))
7
-
8
- #DEPRECATED
9
- DIVISION_APE ||= YAML.load_file(File.join(File.dirname(__FILE__), 'division_ape.yml'))
10
- CODE_APE ||= YAML.load_file(File.join(File.dirname(__FILE__), 'code_ape.yml'))
5
+ require "code_ape/section"
6
+ require "code_ape/subsection"
7
+ require "code_ape/division"
8
+ require "code_ape/group"
9
+ require "code_ape/class"
11
10
 
11
+ # All CodeApe functionality
12
+ module CodeApe
13
+ # Takes a ape code and returns data if possible
14
+ # @param code [String] The ape code to search
15
+ # @return [CodeApe::Subsection, CodeApe::Division, CodeApe::Group, CodeApe::Class, nil] the ape code data
12
16
  def self.ape(code)
13
- if NAF_REV2.key?(code.to_s)
14
- NAF_REV2[code.to_s]
15
- else
16
- nil
17
- end
18
- end
17
+ upcased_code = code&.upcase
19
18
 
20
- # DEPRECATED
21
- def self.division(code)
22
- if DIVISION_APE.key?(code)
23
- DIVISION_APE[code]
24
- else
25
- nil
19
+ case upcased_code
20
+ when REGEX_SUBSECTION
21
+ SUBSECTIONS.find { |e| e.key == upcased_code }
22
+ when REGEX_DIVISION
23
+ DIVISIONS.find { |e| e.key == upcased_code || e.key.delete(".") == upcased_code }
24
+ when REGEX_GROUP
25
+ GROUPS.find { |e| e.key == upcased_code || e.key.delete(".") == upcased_code }
26
+ when REGEX_CLASS
27
+ CLASSES.find { |e| e.key == upcased_code || e.key.delete(".") == upcased_code }
26
28
  end
27
29
  end
28
30
 
29
- #DEPRECATED
30
- def self.activity(code)
31
- if CODE_APE.key?(code)
32
- CODE_APE[code]
33
- else
34
- nil
35
- end
31
+ # Takes a section code and returns data if possible
32
+ # @param code [String] The section code to search
33
+ # @return [CodeApe::Section, nil] the section code data
34
+ def self.section(code)
35
+ SECTIONS.find { |e| e.key == code&.upcase }
36
36
  end
37
-
38
37
  end