epb_view_models 1.0.9 → 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94ef952761872299d77aff99b1c9ac3d82d1df98a63cd74d0520c1628475f319
4
- data.tar.gz: 4466ab181f638cd85fc7ef943f6b78709b770a742dcd07529e708f49c3e9669f
3
+ metadata.gz: 8d6b7baac28f53821c21ca4a9e6517bb3f3f0b8abf990aa3899bc8a61ef3c982
4
+ data.tar.gz: 9e1a9e515367fca063c6be4b06dc4a7a23ee3563f87fe5ae0d8a40a649d6d5ea
5
5
  SHA512:
6
- metadata.gz: 69ceeb585c7014f6b47c4a71241d8a5120cf9dee08e41bd53f90ea3b9bf1a7b4e5b29c8dabf643060b471acee26778295c04c83a4fb6bd1000be0deff6ccb959
7
- data.tar.gz: 3fe39ccc50a2ddff5fa2040d214b95c56554e45b8332fd5ba9aa29ed57648c7c68bdf9a1210143babd0518d777033fd0db9e8778487d4d0e477a34a4d6c0dd75
6
+ metadata.gz: 43c9884e01d2242b4d89d9cb0f0274a6721aecd484944f523a7aac399d5b890472728384fe35d2892bff273f396e82bf03ca1e5a2e68fc72d430b23509109394
7
+ data.tar.gz: dbcdc6c7fac809075b206c908df02ee82affc3a4b13e8115b9e4dccc51ea673d11655a79de7374dd0d3fe54f83e43afbf91dbc3e3d8fa3625b201cb5ad927134
@@ -5,7 +5,7 @@ loader = Zeitwerk::Loader.for_gem
5
5
  loader.setup
6
6
 
7
7
  module EpbViewModels
8
- VERSION = "1.0.9"
8
+ VERSION = "1.0.13"
9
9
  end
10
10
 
11
11
  # Monkey patching to avoid using ActiveRecord::Type::Boolean.new.cast
data/lib/presenter/xsd.rb CHANGED
@@ -1,31 +1,32 @@
1
1
  module Presenter
2
2
  class Xsd
3
- def initialize(assessment_type:, xsd_dir_path: "api/schemas/xml/*/")
4
- @assessment_type = assessment_type
5
- @xsd_dir_path = xsd_dir_path
6
- end
3
+ def get_enums_by_type(domain_arguments)
4
+ xsd_files_gateway = ViewModelGateway::XsdFilesGateway.new(domain_arguments)
5
+
6
+ begin
7
+ xsd_files = xsd_files_gateway.xsd_files
8
+ rescue ViewModelBoundary::XsdFilesNotFound => e
9
+ raise ViewModelBoundary::XsdFilesNotFound, e.message.to_s
10
+ end
7
11
 
8
- def get_enums_by_type(simple_type)
9
12
  hash = {}
10
- xpath = "//xs:simpleType[@name='#{simple_type}']//xs:enumeration"
11
- xsd_files.each do |file_name|
12
- doc = REXML::Document.new(File.read(file_name))
13
- enums_hash = {}
14
- REXML::XPath.each(doc, "#{xpath}/@value") do |e|
15
- desc_path = "#{xpath}[@value='#{e.value}']//xs:annotation//xs:documentation"
16
- enums_hash.merge!(e.value => REXML::XPath.first(doc, desc_path).children.first)
17
- end
13
+
14
+ xsd_files.each do |file|
15
+ enums_hash = File.extname(file).downcase == ".xsd" ? read_xsd(file, domain_arguments.simple_type) : read_xml(file, domain_arguments.simple_type, domain_arguments.node_hash)
18
16
 
19
17
  next if enums_hash.empty?
20
18
 
21
- hash[schema_version(file_name)] = enums_hash
19
+ hash[xsd_files_gateway.schema_version(file)] = enums_hash
22
20
  end
21
+
22
+ raise ViewModelBoundary::NodeNotFound, "Node #{domain_arguments.simple_type} was not found in any of the files in #{domain_arguments.xsd_dir_path} directory" if hash.empty?
23
+
23
24
  hash
24
25
  end
25
26
 
26
- def unique_enums(simple_type)
27
+ def unique_enums(domain_arguments)
27
28
  uniq_enums = []
28
- enums = get_enums_by_type(simple_type).values
29
+ enums = get_enums_by_type(domain_arguments).values
29
30
 
30
31
  enums.each_with_index do |_hash, i|
31
32
  if i.positive? && (enums[i].to_a != enums[i + 1].to_a)
@@ -41,36 +42,26 @@ module Presenter
41
42
 
42
43
  private
43
44
 
44
- def schema_version(file_name)
45
- schema_version = file_name.delete_prefix("api/schemas/xml/").split("/").first
46
- sap_defnied_in_rdsap_dir?(file_name) ? "#{schema_version}/SAP" : schema_version
47
- end
48
-
49
- def xsd_files
50
- case @assessment_type
51
- when "SAP"
52
- sap_xsd_files
53
- when "RdSAP"
54
- rdsap_xsd_files
55
- when "CEPC"
56
- cepc_xsd_files
45
+ def read_xsd(file_name, simple_type)
46
+ xpath = "//xs:simpleType[@name='#{simple_type}']//xs:enumeration"
47
+ doc = REXML::Document.new(File.read(file_name))
48
+ enums_hash = {}
49
+ REXML::XPath.each(doc, "#{xpath}/@value") do |node|
50
+ desc_path = "#{xpath}[@value='#{node.value}']//xs:annotation//xs:documentation"
51
+ enums_hash.merge!(node.value => REXML::XPath.first(doc, desc_path).children.first)
57
52
  end
53
+ enums_hash
58
54
  end
59
55
 
60
- def sap_xsd_files
61
- Dir.glob("#{@xsd_dir_path}*-Domains.xsd")
62
- end
63
-
64
- def rdsap_xsd_files
65
- Dir.glob("#{@xsd_dir_path}SAP-Domains.xsd")
66
- end
56
+ def read_xml(file_name, node_name, node_hash)
57
+ doc = Nokogiri.XML(File.read(file_name))
58
+ enums_hash = {}
67
59
 
68
- def cepc_xsd_files
69
- Dir.glob("#{@xsd_dir_path}Reported-Data.xsd")
70
- end
60
+ doc.xpath(node_name).each do |node|
61
+ enums_hash.merge!(node.xpath(node_hash.keys[0].to_s).children.text => node.xpath(node_hash.values[0]).children.text)
62
+ end
71
63
 
72
- def sap_defnied_in_rdsap_dir?(file_name)
73
- @assessment_type == "SAP" && file_name.end_with?("SAP-Domains.xsd")
64
+ enums_hash
74
65
  end
75
66
  end
76
67
  end
@@ -0,0 +1,3 @@
1
+ module ViewModelBoundary
2
+ class NodeNotFound < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module ViewModelBoundary
2
+ class XsdFilesNotFound < StandardError; end
3
+ end
@@ -0,0 +1,13 @@
1
+ module ViewModelDomain
2
+ class XsdArguments
3
+ attr_reader :simple_type, :assessment_type, :xsd_dir_path, :gem_path, :node_hash
4
+
5
+ def initialize(simple_type:, assessment_type:, xsd_dir_path: "", gem_path: "", node_hash: nil)
6
+ @simple_type = simple_type
7
+ @assessment_type = assessment_type
8
+ @xsd_dir_path = xsd_dir_path
9
+ @gem_path = gem_path
10
+ @node_hash = node_hash
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,62 @@
1
+ module ViewModelGateway
2
+ class XsdFilesGateway
3
+ attr_reader :simple_type, :assessment_type, :xsd_dir_path, :glob_path
4
+
5
+ API_PATH = "api/schemas/xml/".freeze
6
+ XSD_DEFAULT_PATH = "/api/schemas/xml/**/".freeze
7
+
8
+ def initialize(domain_arguments)
9
+ @simple_type = domain_arguments.simple_type
10
+ @assessment_type = domain_arguments.assessment_type
11
+ @xsd_dir_path = domain_arguments.xsd_dir_path
12
+ @dir_path = Dir.pwd if domain_arguments.gem_path.nil? || domain_arguments.gem_path.empty?
13
+ end
14
+
15
+ def schema_version(file)
16
+ api_path_start = file.index(API_PATH) + API_PATH.length
17
+ schema_version = file[api_path_start..].split("/").first
18
+ sap_defined_in_rdsap_dir?(file) ? "#{schema_version}/SAP" : schema_version
19
+ end
20
+
21
+ def xsd_files
22
+ files = if @xsd_dir_path.nil? || @xsd_dir_path.empty?
23
+ case @assessment_type.downcase
24
+ when "sap"
25
+ sap_xsd_files
26
+ when "rdsap"
27
+ rdsap_xsd_files
28
+ when "cepc"
29
+ cepc_xsd_files
30
+ end
31
+ else
32
+ @glob_path = "#{@dir_path}#{@xsd_dir_path}"
33
+ Dir.glob(@glob_path)
34
+ end
35
+
36
+ raise ViewModelBoundary::XsdFilesNotFound, "No xsd files were found in #{@glob_path} directory" if files.nil? || files.empty?
37
+
38
+ files
39
+ end
40
+
41
+ private
42
+
43
+ def sap_defined_in_rdsap_dir?(file)
44
+ assessment_type == "SAP" && file.end_with?("SAP-Domains.xsd")
45
+ end
46
+
47
+ def sap_xsd_files
48
+ @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}*-Domains.xsd"
49
+ Dir.glob(@glob_path)
50
+ end
51
+
52
+ def rdsap_xsd_files
53
+ @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}*-Domains.xsd"
54
+ Dir.glob(@glob_path)
55
+ end
56
+
57
+ def cepc_xsd_files
58
+ @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}Reported-Data.xsd"
59
+ Dir.glob(@glob_path)
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epb_view_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - MHCLG Energy Performance of Buildings
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-06 00:00:00.000000000 Z
11
+ date: 2021-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -1821,6 +1821,10 @@ files:
1821
1821
  - lib/view_model/sap_schema_ni_174/common_schema.rb
1822
1822
  - lib/view_model/sap_schema_ni_1800/common_schema.rb
1823
1823
  - lib/view_model/sap_wrapper.rb
1824
+ - lib/view_model_boundary/node_not_found.rb
1825
+ - lib/view_model_boundary/xsd_files_not_found.rb
1826
+ - lib/view_model_domain/xsd_arguments.rb
1827
+ - lib/view_model_gateway/xsd_files_gateway.rb
1824
1828
  homepage: https://github.com/communitiesuk/epb-view-models
1825
1829
  licenses:
1826
1830
  - MIT