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

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.



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

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

Instance Attribute Details

#oozeObject (readonly)

Returns the value of attribute ooze.



71
72
73
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 71

def ooze
  @ooze
end

#optionsObject (readonly)

Returns the value of attribute options.



72
73
74
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 72

def options
  @options
end

Class Method Details

.field_ref(field) ⇒ Object



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

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

.indexed?(field) ⇒ Boolean

Returns:

  • (Boolean)


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

def indexed?(field)
  !field.deindex
end

.key_ref_label(field, default: nil) ⇒ Object



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

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

.key_to_label(str) ⇒ Object



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

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

.key_to_ref(str) ⇒ Object



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

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

.label(field, default: nil) ⇒ Object



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

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

.label?(field) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.with_ref?(field) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 60

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

Instance Method Details

#delimiterObject



82
83
84
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 82

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:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 94

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.



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 121

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