locabulary 0.5.1 → 0.6.0

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: d6860e6b46a871aa5f6fa44d61e4a71549fe997d
4
- data.tar.gz: 8e45a3991ac5fac5b35944094db338ac8d484c0a
3
+ metadata.gz: 278eebac39c183be338728d67f3be9fab289bc96
4
+ data.tar.gz: 6ed77a8d3f22e607c0a083cbe1f14c22e83ef835
5
5
  SHA512:
6
- metadata.gz: d0934da270aa054be0eae9d1dea2db1739d1fdabc79142f6845efe944b0b332816330b0ab74712c4c165f1526747d1f82d563e4e43d0734d0191ca6d0fea4cd8
7
- data.tar.gz: 4e137b994cc4d406e2a5787ac565e20eeca0b2aa7325707c2bf7d536a5e4543fe353fbdc05f19fcd04eb20dd2575b108dbc8e52bfcb7ce1838448ac7777fc65e
6
+ metadata.gz: be8235aa07b3e25f76d51ec1559d0d1050430ce54bf3156be255e8bbb7b88d426f60dc1ea1680a11c49000567663dc83615a343a5410fcfb82b2b87639601478
7
+ data.tar.gz: 58887e1e050416cec0011b9f4d1e8848e2a06107c2387401987cb2262cd97f48a32b6f0358a1ede2ec52f8987e947d3ca02603939e7fa3aba237e823043fe338
@@ -2,6 +2,7 @@ require 'set'
2
2
  require 'date'
3
3
  require 'locabulary/item'
4
4
  require 'locabulary/exceptions'
5
+ require 'locabulary/hierarchy_processor'
5
6
 
6
7
  module Locabulary
7
8
  module Commands
@@ -37,42 +38,34 @@ module Locabulary
37
38
  def initialize(options = {})
38
39
  @predicate_name = options.fetch(:predicate_name)
39
40
  @as_of = options.fetch(:as_of) { Date.today }
40
- @builder = Item.builder_for(predicate_name: predicate_name)
41
+ @locabulary_item_class = Item.class_to_instantiate(predicate_name: predicate_name)
41
42
  @utility_service = options.fetch(:utility_service) { default_utility_service }
42
43
  end
43
44
 
44
45
  def call
45
- items = []
46
- hierarchy_graph_keys = {}
47
- top_level_slugs = Set.new
48
- utility_service.with_active_extraction_for(predicate_name, as_of) do |data|
49
- item = builder.call(data.merge('predicate_name' => predicate_name))
50
- items << item
51
- top_level_slugs << item.root_slug
52
- hierarchy_graph_keys[item.term_label] = item
53
- end
54
- associate_parents_and_childrens_for(hierarchy_graph_keys, items)
55
- top_level_slugs.map { |slug| hierarchy_graph_keys.fetch(slug) }
46
+ HierarchyProcessor.call(
47
+ enumerator: data_enumerator,
48
+ item_builder: item_builder,
49
+ predicate_name: predicate_name
50
+ )
56
51
  end
57
52
 
58
53
  private
59
54
 
60
- attr_reader :predicate_name, :as_of, :builder, :utility_service
55
+ def data_enumerator
56
+ ->(&block) { utility_service.with_active_extraction_for(predicate_name, as_of, &block) }
57
+ end
58
+
59
+ def item_builder
60
+ ->(data) { locabulary_item_class.new(data.merge('predicate_name' => predicate_name)) }
61
+ end
62
+
63
+ attr_reader :predicate_name, :as_of, :locabulary_item_class, :utility_service
61
64
 
62
65
  def default_utility_service
63
66
  require 'locabulary/utility'
64
67
  Utility
65
68
  end
66
-
67
- def associate_parents_and_childrens_for(hierarchy_graph_keys, items)
68
- items.each do |item|
69
- begin
70
- hierarchy_graph_keys.fetch(item.parent_term_label).add_child(item) unless item.parent_slugs.empty?
71
- rescue KeyError => error
72
- raise Exceptions::MissingHierarchicalParentError.new(predicate_name, error)
73
- end
74
- end
75
- end
76
69
  end
77
70
  end
78
71
  end
@@ -3,6 +3,7 @@ require 'locabulary'
3
3
  require 'locabulary/exceptions'
4
4
  require 'locabulary/item'
5
5
  require 'locabulary/facet_wrapper_for_item'
6
+ require 'locabulary/hierarchy_processor'
6
7
 
7
8
  module Locabulary
8
9
  module Commands
@@ -31,33 +32,17 @@ module Locabulary
31
32
  end
32
33
 
33
34
  def call
34
- items = []
35
- hierarchy_graph_keys = {}
36
- top_level_slugs = Set.new
37
- faceted_items.each do |faceted_item|
38
- item = build_item(faceted_item)
39
- items << item
40
- top_level_slugs << item.root_slug
41
- hierarchy_graph_keys[item.term_label] = item
42
- end
43
- associate_parents_and_childrens_for(hierarchy_graph_keys, items)
44
- top_level_slugs.map { |slug| hierarchy_graph_keys.fetch(slug) }.sort
35
+ HierarchyProcessor.call(
36
+ enumerator: faceted_items.method(:each),
37
+ item_builder: method(:build_item),
38
+ predicate_name: predicate_name
39
+ )
45
40
  end
46
41
 
47
42
  private
48
43
 
49
44
  attr_reader :locabulary_item_class, :predicate_name, :faceted_items, :faceted_item_hierarchy_delimiter
50
45
 
51
- def associate_parents_and_childrens_for(hierarchy_graph_keys, items)
52
- items.each do |item|
53
- begin
54
- hierarchy_graph_keys.fetch(item.parent_term_label).add_child(item) unless item.parent_slugs.empty?
55
- rescue KeyError => error
56
- raise Exceptions::MissingHierarchicalParentError.new(predicate_name, error)
57
- end
58
- end
59
- end
60
-
61
46
  def build_item(faceted_node)
62
47
  term_label = convert_faceted_node_value_to_term_label(faceted_node.value)
63
48
  locabulary_item = find_locabulary_item(predicate_name: predicate_name, term_label: term_label)
@@ -79,10 +64,7 @@ module Locabulary
79
64
 
80
65
  # Responsible for converting the hierarchy delimiter of the facet item to the hierarchy delimiter of the Locabulary::Item.
81
66
  def convert_faceted_node_value_to_term_label(value)
82
- value.gsub(
83
- /(?<!#{faceted_item_hierarchy_delimiter})#{faceted_item_hierarchy_delimiter}(?!#{faceted_item_hierarchy_delimiter})/,
84
- locabulary_item_class.hierarchy_delimiter
85
- )
67
+ value.split(faceted_item_hierarchy_delimiter).join(locabulary_item_class.hierarchy_delimiter)
86
68
  end
87
69
  end
88
70
  end
@@ -55,5 +55,6 @@ module Locabulary
55
55
 
56
56
  extend Forwardable
57
57
  def_delegators :__faceted_node__, :value, :hits
58
+ alias count hits
58
59
  end
59
60
  end
@@ -0,0 +1,53 @@
1
+ # :nodoc:
2
+ module Locabulary
3
+ # @api private
4
+ #
5
+ # Responsible for processing an flat enumeration of data and creating
6
+ # a sorted hierarchy.
7
+ #
8
+ # @note This is an extraction of common logic found in two separate classes.
9
+ class HierarchyProcessor
10
+ def self.call(options = {})
11
+ new(options).call
12
+ end
13
+
14
+ # @param options [Hash]
15
+ # @option options [#call] :item_builder - when called will create a Locabulary::Items::Base object
16
+ # @option options [#call] :predicate_name -
17
+ # @option options [#call] :enumerator - when called will yield an enumerated_object to the item_builder
18
+ def initialize(options = {})
19
+ @item_builder = options.fetch(:item_builder)
20
+ @predicate_name = options.fetch(:predicate_name)
21
+ @enumerator = options.fetch(:enumerator)
22
+ end
23
+
24
+ def call
25
+ items = []
26
+ hierarchy_graph_keys = {}
27
+ top_level_slugs = Set.new
28
+ enumerator.call do |enumerated_object|
29
+ item = item_builder.call(enumerated_object)
30
+ items << item
31
+ top_level_slugs << item.root_slug
32
+ hierarchy_graph_keys[item.term_label] = item
33
+ end
34
+ associate_parents_and_childrens_for(hierarchy_graph_keys, items)
35
+ top_level_slugs.map { |slug| hierarchy_graph_keys.fetch(slug) }.sort
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :item_builder, :predicate_name, :enumerator
41
+
42
+ def associate_parents_and_childrens_for(hierarchy_graph_keys, items)
43
+ items.each do |item|
44
+ begin
45
+ hierarchy_graph_keys.fetch(item.parent_term_label).add_child(item) unless item.parent_slugs.empty?
46
+ rescue KeyError => error
47
+ raise Exceptions::MissingHierarchicalParentError.new(predicate_name, error)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ private_constant :HierarchyProcessor
53
+ end
@@ -135,11 +135,17 @@ module Locabulary
135
135
  children.count.zero?
136
136
  end
137
137
 
138
+ # When rendered as part of a select list
138
139
  def selectable_label
139
140
  slugs[-1]
140
141
  end
141
142
 
142
143
  alias selectable_id id
144
+
145
+ # When rendered as part of a facet list
146
+ def hierarchy_facet_label
147
+ slugs[-1]
148
+ end
143
149
  end
144
150
  end
145
151
  end
@@ -1,3 +1,3 @@
1
1
  module Locabulary
2
- VERSION = '0.5.1'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locabulary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Friesen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-05 00:00:00.000000000 Z
11
+ date: 2016-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -263,6 +263,7 @@ files:
263
263
  - lib/locabulary/commands/build_ordered_hierarchical_tree_command.rb
264
264
  - lib/locabulary/exceptions.rb
265
265
  - lib/locabulary/facet_wrapper_for_item.rb
266
+ - lib/locabulary/hierarchy_processor.rb
266
267
  - lib/locabulary/item.rb
267
268
  - lib/locabulary/items.rb
268
269
  - lib/locabulary/items/administrative_unit.rb