Class: Eco::API::UseCases::OozeSamples::Helpers::ExportableOoze

Inherits:
Object
  • Object
show all
Includes:
OozeHandlers
Defined in:
lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb

Overview

Class to ease the export process

Constant Summary collapse

MERGE_DELIMITER =
"#:#"
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"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OozeHandlers

#array_indexes, #merge_arrays, #merge_values

Constructor Details

#initialize(ooze, **options) ⇒ ExportableOoze

Returns a new instance of ExportableOoze.



67
68
69
70
71
72
73
74
75
76
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 67

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
  })
end

Instance Attribute Details

#oozeObject (readonly)

Returns the value of attribute ooze.



64
65
66
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 64

def ooze
  @ooze
end

#optionsObject (readonly)

Returns the value of attribute options.



65
66
67
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 65

def options
  @options
end

Class Method Details

.field_ref(field) ⇒ Object



57
58
59
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 57

def field_ref(field)
  field.ref_backend || field.ref(any_length: true)
end

.indexed?(field) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 49

def indexed?(field)
  !field.deindex
end

.key_ref_label(field, default: nil) ⇒ Object



34
35
36
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 34

def key_ref_label(field, default: nil)
  "#{field_ref(field)}#{MERGE_DELIMITER}#{label(field, default: default)}"
end

.key_to_label(str) ⇒ Object



38
39
40
41
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 38

def key_to_label(str)
  return str unless str.include?(MERGE_DELIMITER)
  str.split(MERGE_DELIMITER).last
end

.key_to_ref(str) ⇒ Object



43
44
45
46
47
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 43

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
end

.label(field, default: nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 26

def label(field, default: nil)
  value = nil
  value ||= field.label.to_s.strip if field.respond_to?(:label)
  value = (!value || value.empty?) ? nil : value
  return value if value || !default
  default || "Unnamed field:"
end

.label?(field) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 22

def label?(field)
  !label(field).to_s.strip.empty?
end

.with_ref?(field) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 53

def with_ref?(field)
  !field.ref_backend.to_s.strip.empty?
end

Instance Method Details

#delimiterObject



78
79
80
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 78

def delimiter
  options[:delimiter] || "\n"
end

#key_typed_data(**options) ⇒ Array[Hash]

Note:
  • This method merges indexed values
  • It overrides even between fields of different type

It offers an intermediate structure that can be aligned with other oozes

Parameters:

  • only_indexed: (Boolean)
  • only_labeled: (Boolean)
  • only_with_ref: (Boolean)

Returns:



90
91
92
93
94
95
96
97
98
99
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 90

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

#with_field_section_pairs {|field, section| ... } ⇒ Array<Array] Ordered `Array` of pairs of field and section thereof.

Note:
  1. It prevents duplicated sections

Helper to go through fields and sections in the order they appear.

Yields:

  • (field, section)

    once per field and section.

Yield Parameters:

  • field (Component)

    a field.

  • section (Section)

    the section that holds the field.

Returns:

  • (Array<Array] Ordered `Array` of pairs of field and section thereof.)

    Array<Array] Ordered Array of pairs of field and section thereof.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 108

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
  end
end