Class: Eco::API::UseCases::OozeSamples::Helpers::ExportableOoze
- 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
-
#ooze ⇒ Object
readonly
Returns the value of attribute ooze.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .field_ref(field) ⇒ Object
- .indexed?(field) ⇒ Boolean
- .key_ref_label(field, default: nil) ⇒ Object
- .key_to_label(str) ⇒ Object
- .key_to_ref(str) ⇒ Object
- .label(field, default: nil) ⇒ Object
- .label?(field) ⇒ Boolean
- .with_ref?(field) ⇒ Boolean
Instance Method Summary collapse
- #delimiter ⇒ Object
-
#initialize(ooze, **options) ⇒ ExportableOoze
constructor
A new instance of ExportableOoze.
-
#key_typed_data(**options) ⇒ Array[Hash]
It offers an intermediate structure that can be aligned with other oozes.
-
#with_field_section_pairs {|field, section| ... } ⇒ Array<Array] Ordered `Array` of pairs of field and section thereof.
Helper to go through fields and sections in the order they appear.
Methods included from OozeHandlers
#array_indexes, #merge_arrays, #merge_values
Constructor Details
#initialize(ooze, **options) ⇒ ExportableOoze
Returns a new instance of ExportableOoze.
75 76 77 78 79 80 81 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 75 def initialize(ooze, **) msg = "Expecting Ecoportal::API::V2::Page. Given: #{ooze.class}" raise msg unless ooze.is_a?(Ecoportal::API::V2::Page) @ooze = ooze = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#ooze ⇒ Object (readonly)
Returns the value of attribute ooze.
72 73 74 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 72 def ooze @ooze end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
73 74 75 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 73 def end |
Class Method Details
.field_ref(field) ⇒ Object
65 66 67 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 65 def field_ref(field) field.ref_backend || field.ref(any_length: true) end |
.indexed?(field) ⇒ Boolean
57 58 59 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 57 def indexed?(field) !field.deindex end |
.key_ref_label(field, default: nil) ⇒ Object
40 41 42 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 40 def key_ref_label(field, default: nil) "#{field_ref(field)}#{MERGE_DELIMITER}#{label(field, default: default)}" end |
.key_to_label(str) ⇒ Object
44 45 46 47 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 44 def key_to_label(str) return str unless str.include?(MERGE_DELIMITER) str.split(MERGE_DELIMITER).last end |
.key_to_ref(str) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 49 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 38 |
# 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
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
61 62 63 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 61 def with_ref?(field) !field.ref_backend.to_s.strip.empty? end |
Instance Method Details
#delimiter ⇒ Object
83 84 85 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 83 def delimiter [: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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 95 def key_typed_data(**) .merge!() .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:
- It prevents duplicated sections
Helper to go through fields and sections in the order they appear.
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb', line 122 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 |