Class: GeoLabels::Exporter
- Inherits:
-
Object
- Object
- GeoLabels::Exporter
- Defined in:
- lib/geo_labels/exporter.rb
Class Method Summary collapse
- .contacts_h ⇒ Object
- .export_h ⇒ Object
- .export_str ⇒ Object
-
.labels_tree(add_punctuation: nil) ⇒ Object
Formats the tree structure from labels_tree_h to a string output can be called with add_punctuation: ‘-’ to add dashes.
-
.labels_tree_h ⇒ Object
Returns an array having the tree structure as ruby objects.
Class Method Details
.contacts_h ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/geo_labels/exporter.rb', line 32 def self.contacts_h GeoLabels::Contact.includes(:labels).map do |contact| contact .attributes .slice(*%w[name street subsection city department country state description latitude longitude]) .merge('labels' => contact.labels.map(&:name)) .select { |_, v| v.present? } .transform_values do |value| case value when Date, Time then value.iso8601 when BigDecimal then value.to_f else value end end end end |
.export_h ⇒ Object
6 7 8 9 10 11 |
# File 'lib/geo_labels/exporter.rb', line 6 def self.export_h { 'labels' => labels_tree, 'contacts' => contacts_h } end |
.export_str ⇒ Object
13 14 15 |
# File 'lib/geo_labels/exporter.rb', line 13 def self.export_str export_h.to_yaml end |
.labels_tree(add_punctuation: nil) ⇒ Object
Formats the tree structure from labels_tree_h to a string output can be called with add_punctuation: ‘-’ to add dashes
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/geo_labels/exporter.rb', line 19 def self.labels_tree(add_punctuation: nil) output = '' tree_formatter = proc do |items, lead_space| items.each do |item| output += "#{lead_space}#{item[:name]}\n" tree_formatter.call(item[:children], "#{lead_space} ") if item[:children].present? end end tree_formatter.call(labels_tree_h, '') output.gsub!(/^(\s*) /, "\1#{add_punctuation} ") if add_punctuation.present? output end |
.labels_tree_h ⇒ Object
Returns an array having the tree structure as ruby objects
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/geo_labels/exporter.rb', line 50 def self.labels_tree_h labels = GeoLabels::Label.order(:name).to_a tree = Hash.new { |h, k| h[k] = {name: nil, id: nil, children: []} } labels.each do |label| id, name, parent_id = label.attributes.values_at('id', 'name', 'parent_id') tree[id][:name] = name tree[id][:id] = id tree[parent_id][:children].push tree[id] end tree[nil][:children] end |