module Eco module API class UseCases class OozeSamples module Helpers # Class to ease the export process class ExportableOoze 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" }.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 ||= field.label.to_s.strip if field.respond_to?(:label) value = nil unless value && !value.empty? return value if value || !default default || "Unnamed field:" end def key_ref_label(field, default: nil) "#{field_ref(field)}#{MERGE_DELIMITER}#{label(field, default: default)}" end def key_to_label(str) return str unless str.include?(MERGE_DELIMITER) str.split(MERGE_DELIMITER).last end def key_to_ref(str) return str unless str.include?(MERGE_DELIMITER) ref = str.split(MERGE_DELIMITER).first return ref unless ref.to_s.strip.empty? nil end def indexed?(field) !field.deindex end def with_ref?(field) !field.ref_backend.to_s.strip.empty? end def field_ref(field) field.ref_backend || field.ref(any_length: true) end end include Eco::API::UseCases::OozeSamples::Helpers::OozeHandlers attr_reader :ooze attr_reader :options def initialize(ooze, **options) 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 # It offers an intermediate structure that can be aligned with other oozes # @note # - This method merges indexed values # - It overrides even between fields of different type # @param only_indexed: [Boolean] # @param only_labeled: [Boolean] # @param only_with_ref: [Boolean] # @return [Array[Hash]] 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 ) end end end # Helper to go through fields and sections in the order they appear. # @note # 1. It prevents duplicated sections # @yield [field, section] once per field and section. # @yieldparam field [Component] a field. # @yieldparam section [Section] the section that holds the field. # @return [Array