academic_benchmarks 0.0.3 → 0.0.4

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
  SHA1:
3
- metadata.gz: 3b5ab7ec49f0432d39ca8e694515ba35b46e1893
4
- data.tar.gz: 0f54223ad276f77badc5b05c336aab2108b1a199
3
+ metadata.gz: 5d61a80eca5cb7536525e3359da6693d928d24d4
4
+ data.tar.gz: 254a3dc74b516c970eb2ec49fb160ef6f285ebbf
5
5
  SHA512:
6
- metadata.gz: 0eebb41e7bf31faec07dd7d985cba41e95db2551ba0b2602a4ad53621798f92b656b1017bcc308aea037e1eeac6cfb729804c3e35d921ffa2817ec0054007ae9
7
- data.tar.gz: 3be9d15c88ad4b18c84d691c2bda4c9c3c080881f7557483a15094f14e117b9edde0c51653ba90d361a9aa4bef7584d3d3f288127d8f2152a3a744c69f1b180b
6
+ metadata.gz: 69667639168d5e6fb7c90f6fe927a0fedfe21675820432182b0f88776b453ee530fd477f6ae0b35aaa8bd85e8780c0a794e86628ba08a98619a4be5146dbcf2d
7
+ data.tar.gz: 09e2bacc1aae34f23180c0f0e313e2c49e918f61cbbe4fdc2ce01e65adf8e4b9fd262840fc9a83869fa87360e4d0b861824662cbc62520c0200340ef002c191a
@@ -50,15 +50,50 @@ module AcademicBenchmarks
50
50
  end
51
51
 
52
52
  def authorities
53
- raw_search(list: "authority").map{|a| a["data"]["authority"]}.map{|a| AcademicBenchmarks::Standards::Authority.from_hash(a)}
53
+ raw_search(list: "authority").map do |a|
54
+ AcademicBenchmarks::Standards::Authority.from_hash(a["data"]["authority"])
55
+ end
54
56
  end
55
57
 
56
58
  def documents
57
- raw_search(list: "document").map{|a| a["data"]["document"]}.map{|a| AcademicBenchmarks::Standards::Document.from_hash(a)}
59
+ raw_search(list: "document").map do |a|
60
+ AcademicBenchmarks::Standards::Document.from_hash(a["data"]["document"])
61
+ end
62
+ end
63
+
64
+ def authority_tree(authority_or_authority_code_guid_or_desc)
65
+ authority = if authority_or_authority_code_guid_or_desc.is_a?(Authority)
66
+ authority_or_authority_code_guid_or_desc
67
+ else
68
+ find_authority(authority_or_authority_code_guid_or_desc)
69
+ end
70
+ auth_children = search(authority: authority.code)
71
+ StandardsForest.new(auth_children).consolidate_under_root(authority)
58
72
  end
59
73
 
60
74
  private
61
75
 
76
+ def find_authority(authority_code_guid_or_desc)
77
+ auths = match_authority(authority_code_guid_or_desc)
78
+ if auths.empty?
79
+ raise StandardError,
80
+ "No authority code, guid, or description matched '#{authority_code_guid_or_desc}'"
81
+ elsif auths.count > 1
82
+ raise StandardError,
83
+ "Authority code, guid, or description matched more than one authority. " \
84
+ "matched '#{auths.map(&:to_json).join('; ')}'"
85
+ end
86
+ auths.first
87
+ end
88
+
89
+ def match_authority(authority_code_guid_or_desc)
90
+ authorities.select do |auth|
91
+ auth.code == authority_code_guid_or_desc ||
92
+ auth.guid == authority_code_guid_or_desc ||
93
+ auth.descr == authority_code_guid_or_desc
94
+ end
95
+ end
96
+
62
97
  def raw_search(opts = {})
63
98
  request_search_pages_and_concat_resources(opts.merge(auth_query_params))
64
99
  end
@@ -1,14 +1,50 @@
1
1
 
2
+ #
3
+ # This module will allow you to properly to_s, to_h, and to_json
4
+ # on classes in which it is included.
5
+ #
6
+ # It is not intended to be general purpose, but rather should
7
+ # be used only with standards and standards-related data
8
+ # structures, which themselves are mirrors of the JSON
9
+ # definitions used by Academic Benchmarks
10
+ #
11
+
2
12
  module InstVarsToHash
3
- def to_s
4
- to_h.to_s
13
+ def to_s(omit_parent: true)
14
+ to_h(omit_parent: omit_parent).to_s
5
15
  end
6
16
 
7
- def to_h
17
+ def to_h(omit_parent: true)
8
18
  retval = {}
9
19
  instance_variables.each do |iv|
10
- retval[iv.to_s.gsub('@', '').to_sym] = instance_variable_get(iv)
20
+ unless omit_parent && (iv =~ /^@?parent$/i)
21
+ retval[iv.to_s.delete('@').to_sym] = elem_to_h(instance_variable_get(iv))
22
+ end
11
23
  end
12
24
  retval
13
25
  end
26
+
27
+ def to_json(omit_parent: true)
28
+ to_h(omit_parent: omit_parent).to_json
29
+ end
30
+
31
+ private
32
+
33
+ def expandable_classes
34
+ [ Hash, InstVarsToHash ]
35
+ end
36
+
37
+ def expandable_to_hash(klass)
38
+ expandable_classes.any?{ |k| klass == k || klass < k }
39
+ end
40
+
41
+ def elem_to_h(elem)
42
+ if elem.class == Array
43
+ elem.map { |el| elem_to_h(el) }
44
+ elsif expandable_to_hash(elem.class)
45
+ elem.to_h
46
+ else
47
+ elem
48
+ end
49
+ end
14
50
  end
@@ -5,7 +5,7 @@ module AcademicBenchmarks
5
5
  class Authority
6
6
  include InstVarsToHash
7
7
 
8
- attr_accessor :code, :guid, :description
8
+ attr_accessor :code, :guid, :description, :children
9
9
 
10
10
  alias_method :descr, :description
11
11
 
@@ -13,10 +13,11 @@ module AcademicBenchmarks
13
13
  self.new(code: hash["code"], guid: hash["guid"], description: hash["descr"])
14
14
  end
15
15
 
16
- def initialize(code:, guid:, description:)
16
+ def initialize(code:, guid:, description:, children: [])
17
17
  @code = code
18
18
  @guid = guid
19
19
  @description = description
20
+ @children = children
20
21
  end
21
22
  end
22
23
  end
@@ -30,6 +30,15 @@ module AcademicBenchmarks
30
30
  end
31
31
  end
32
32
 
33
+ def consolidate_under_root(root)
34
+ trees.each do |tree|
35
+ tree.root.parent = root
36
+ tree.root.parent_guid = root.guid
37
+ root.children.push(tree.root)
38
+ end
39
+ StandardsTree.new(root)
40
+ end
41
+
33
42
  def add_standard(standard)
34
43
  if standard.is_a?(Standard)
35
44
  raise StandardError.new(
@@ -53,6 +62,18 @@ module AcademicBenchmarks
53
62
  @trees.empty?
54
63
  end
55
64
 
65
+ def to_s
66
+ trees.map{|tree| tree.to_s}
67
+ end
68
+
69
+ def to_h
70
+ trees.map{|tree| tree.to_h}
71
+ end
72
+
73
+ def to_json
74
+ trees.map{|tree| tree.to_json}
75
+ end
76
+
56
77
  private
57
78
 
58
79
  def to_standard(item)
@@ -1,14 +1,17 @@
1
+ require 'active_support/core_ext/module'
2
+
1
3
  module AcademicBenchmarks
2
4
  module Standards
3
5
  class StandardsTree
4
- attr_reader :root_standard
6
+ attr_reader :root
7
+ delegate :children, :to_s, :to_h, :to_json, to: :root
5
8
 
6
9
  # The item hash can optionally be built to permit the speedy
7
10
  # addition of standards to the tree. since the tree is unordered,
8
11
  # adding to it can be expensive without this
9
12
 
10
- def initialize(root_standard, build_item_hash: true)
11
- @root_standard = root_standard
13
+ def initialize(root, build_item_hash: true)
14
+ @root = root
12
15
  if build_item_hash
13
16
  @item_hash = {}
14
17
  go_ahead_and_build_item_hash
@@ -39,8 +42,8 @@ module AcademicBenchmarks
39
42
  private
40
43
 
41
44
  def go_ahead_and_build_item_hash
42
- @item_hash[@root_standard.guid] = @root_standard
43
- add_children_to_item_hash(@root_standard)
45
+ @item_hash[@root.guid] = @root
46
+ add_children_to_item_hash(@root)
44
47
  end
45
48
 
46
49
  def add_children_to_item_hash(parent)
@@ -51,8 +54,8 @@ module AcademicBenchmarks
51
54
  end
52
55
 
53
56
  def find_parent(standard)
54
- return @root_standard if @root_standard.guid == standard.parent_guid
55
- check_children_for_parent(standard.parent_guid, @root_standard)
57
+ return @root if @root.guid == standard.parent_guid
58
+ check_children_for_parent(standard.parent_guid, @root)
56
59
  end
57
60
 
58
61
  # does a depth-first search
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: academic_benchmarks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Porter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-18 00:00:00.000000000 Z
11
+ date: 2016-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -64,14 +64,14 @@ dependencies:
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '3.1'
67
+ version: '3.4'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '3.1'
74
+ version: '3.4'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: byebug
77
77
  requirement: !ruby/object:Gem::Requirement