lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb in eco-helpers-2.6.4 vs lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb in eco-helpers-2.7.0
- old
+ new
@@ -3,32 +3,37 @@
class UseCases
class OozeSamples
module Helpers
# Class to ease the export process
class ExportableOoze
- MERGE_DELIMITER = "#:#"
-
- META_FIELDS = {
- "id" => "Internal ID",
- "uid" => "Unique ID",
- "name" => "Name of Page",
- "state" => "State of Page",
- "time_zone" => "Time Zone",
+ MERGE_DELIMITER = "#:#".freeze
+ META_FIELDS = {
+ "id" => "Internal ID",
+ "uid" => "Unique ID",
+ "name" => "Name of Page",
+ "state" => "State of Page",
+ "time_zone" => "Time Zone",
"created_at" => "Page Created",
"updated_at" => "Last updated",
- "tags" => "Location Tags"
- }
+ "tags" => "Location Tags"
+ }.freeze
+ DEFAULT_OPTIONS = {
+ delimiter: "\n",
+ only_indexed: true,
+ only_labeled: true,
+ only_with_ref: true
+ }.freeze
class << self
def label?(field)
!label(field).to_s.strip.empty?
end
def label(field, default: nil)
- value = nil
+ value = nil
value ||= field.label.to_s.strip if field.respond_to?(:label)
- value = (!value || value.empty?) ? nil : value
+ value = nil unless value && !value.empty?
return value if value || !default
default || "Unnamed field:"
end
def key_ref_label(field, default: nil)
@@ -40,12 +45,14 @@
str.split(MERGE_DELIMITER).last
end
def key_to_ref(str)
return str unless str.include?(MERGE_DELIMITER)
+
ref = str.split(MERGE_DELIMITER).first
- ref.to_s.strip.empty?? nil : ref
+ return ref unless ref.to_s.strip.empty?
+ nil
end
def indexed?(field)
!field.deindex
end
@@ -63,18 +70,15 @@
attr_reader :ooze
attr_reader :options
def initialize(ooze, **options)
- raise "Expecting Ecoportal::API::V2::Page. Given: #{ooze.class}" unless ooze.is_a?(Ecoportal::API::V2::Page)
- @ooze = ooze
- @options = options.merge({
- delimiter: "\n",
- only_indexed: true,
- only_labeled: true,
- only_with_ref: true
- })
+ msg = "Expecting Ecoportal::API::V2::Page. Given: #{ooze.class}"
+ raise msg unless ooze.is_a?(Ecoportal::API::V2::Page)
+
+ @ooze = ooze
+ @options = DEFAULT_OPTIONS.merge(options)
end
def delimiter
options[:delimiter] || "\n"
end
@@ -90,12 +94,21 @@
def key_typed_data(**options)
options.merge!(options)
meta_fields.tap do |data|
with_field_section_pairs do |field, section|
next unless export?(field)
- key = self.class.key_ref_label(field, default: alternative_label(field, section))
- data[key] = merge_values(data[key], to_value(field), klass: field.class, delimiter: delimiter)
+
+ key = self.class.key_ref_label(
+ field,
+ default: alternative_label(field, section)
+ )
+ data[key] = merge_values(
+ data[key],
+ to_value(field),
+ klass: field.class,
+ delimiter: delimiter
+ )
end
end
end
# Helper to go through fields and sections in the order they appear.
@@ -107,10 +120,11 @@
# @return [Array<Array] Ordered `Array` of pairs of field and section thereof.
def with_field_section_pairs
sections = []
ordered_sections.each_with_object([]) do |section, out|
next if sections.include?(section)
+
sections << section
section.components.each do |field|
out << [field, section]
yield(field, section) if block_given?
end
@@ -138,11 +152,11 @@
end
def export?(field, **opts)
opts = options.merge(opts)
return false unless field.respond_to?(:to_s)
- return false if opts[:only_indexed] && !self.class.indexed?(field)
- return false if opts[:only_labeled] && !self.class.label?(field)
+ return false if opts[:only_indexed] && !self.class.indexed?(field)
+ return false if opts[:only_labeled] && !self.class.label?(field)
return false if opts[:only_with_ref] && !self.class.with_ref?(field)
true
end
def alternative_label(field, section)