-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbmt.rb
More file actions
76 lines (62 loc) · 2.05 KB
/
bmt.rb
File metadata and controls
76 lines (62 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'bmt/item'
require 'bmt/methodology'
require 'bmt/step'
require 'date'
require 'json'
require 'pathname'
module BMT
class VersionNotFoundError < StandardError; end
class MethodologyNotFoundError < StandardError; end
DATA_DIR = Pathname.new(__dir__).join('data')
# memoize loaded
@methodologies = {}
@methodology_keys = {}
module_function
# returns a Methodology object given a key and a version
def find(key, version: current_version)
raise VersionNotFoundError unless versions.include?(version)
raise MethodologyNotFoundError unless methodology_keys(version:).include?(key)
@methodologies[version].nil? && @methodologies[version] = {}
@methodologies[version][key] ||= Methodology.new(
key:,
version:,
attributes: methodology_json(key, version:)
)
@methodologies[version][key]
end
def current_version
versions.first
end
# returns available methodology keys for a given version
def methodology_keys(version: current_version)
@methodology_keys[version] ||=
DATA_DIR.join(version, 'methodologies').entries
.map(&:basename)
.map(&:to_s)
.grep(/json/)
.map { |filepath| File.basename(filepath, File.extname(filepath)) }
end
# Infer the available versions of the BMT from the names of the files
# in the repo.
# The returned list is in order with the current version first.
def versions
# START Contributions by Cursor.
@versions ||= json_dir_names.sort_by { |v| Gem::Version.new(v) }.reverse!
# END Cursor.
end
def methodology_json(key, version: current_version)
JSON.parse(methodology_pathname(key, version:).read)
end
def methodology_pathname(key, version: current_version)
DATA_DIR.join(version, 'methodologies', "#{key}.json")
end
# Get names of directories matching lib/data/<major>-<minor>/
def json_dir_names
DATA_DIR.entries
.map(&:basename)
.map(&:to_s)
# START Contributions by Cursor.
.grep(/^[0-9]+\.[0-9]/)
# END Cursor.
end
end