-
Notifications
You must be signed in to change notification settings - Fork 0
/
codelist.rb
131 lines (101 loc) · 3 KB
/
codelist.rb
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Codelist
class UnreadableCodelist < StandardError; end
class UnrecognisedSource < StandardError; end
class KeyNotFound < StandardError; end
include Enumerable
attr_reader :type, :source, :list
def initialize(type:, source: "iati")
raise UnrecognisedSource unless source.in?(%w[iati beis])
@type = type
@source = source
@list = fetch_codelist
end
class << self
def codelists
if Rails.env.development?
initialize_codelists
else
@codelists ||= initialize_codelists
end
end
def codelist_to_hash(path)
Dir.glob("#{Rails.root}/vendor/data/codelists/#{path}/**/*.yml").map { |filename|
[File.basename(filename).split(".").first, YAML.safe_load(File.read(filename))]
}.to_h
end
def initialize_codelists
{
"iati" => codelist_to_hash("IATI/#{IATI_VERSION}"),
"beis" => codelist_to_hash("BEIS")
}
end
end
def hash_of_coded_names
coded_values = {}
list.each do |code|
coded_values[ActiveSupport::Inflector.parameterize(code["name"], separator: "_")] = code["code"]
end
coded_values
end
def hash_of_integer_coded_names
hash_of_coded_names.map { |key, value|
[key, value.to_i]
}.to_h
end
def hash_of_named_codes
coded_values = {}
list.each do |code|
coded_values[code["code"].to_s] = code["name"]
end
coded_values
end
def to_objects(with_empty_item: true)
objects = list.collect { |item|
next if item["status"] == "withdrawn"
OpenStruct.new(name: item["name"], code: item["code"])
}.compact.sort_by(&:name)
if with_empty_item
empty_item = OpenStruct.new(name: "", code: "")
objects.unshift(empty_item)
end
objects
end
def to_objects_with_description(code_displayed_in_name: false, entity: "activity", type: "")
data = list.collect { |item|
name = code_displayed_in_name ? "#{item["name"]} (#{item["code"]})" : item["name"]
description = I18n.t("form.hint.activity.options.#{type}.#{item["code"]}", default: item["description"])
OpenStruct.new(name: name, code: item["code"], description: description)
}
data.sort_by(&:code)
end
def to_objects_with_categories(include_withdrawn: false)
return [] if list.empty?
list.collect { |item|
next if item["status"] == "withdrawn" && include_withdrawn == false
OpenStruct.new(name: item["name"], code: item["code"], category: item["category"])
}.compact.sort_by(&:name)
end
def each
list.map { |item| yield item }
end
def empty?
list.count == 0
end
def values
list.values
end
def values_for(key)
values = list.pluck(key).compact
raise KeyNotFound if values.empty?
values
end
def find_item_by_code(code)
return nil unless list.first.key?("code")
list.find { |c| c["code"] == code }
end
private def fetch_codelist
codelist = self.class.codelists[source][type]
raise UnreadableCodelist if codelist.blank?
codelist["data"]
end
end