Class: BridgetownDirectus::DataMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown_directus/data_mapper.rb

Overview

Data mapper for transforming Directus data into Bridgetown resources

Class Method Summary collapse

Class Method Details

.map(collection_config, data) ⇒ Hash

Map Directus data to Bridgetown format based on collection configuration

Parameters:

  • collection_config (CollectionConfig)

    The collection configuration

  • data (Hash)

    The Directus data

Returns:

  • (Hash)

    The mapped data



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bridgetown_directus/data_mapper.rb', line 11

def map(collection_config, data)
  mapped_data = {}

  collection_config.fields.each do |bridgetown_field, field_config|
    if field_config.is_a?(Hash)
      directus_field = field_config[:directus_field]
      converter = field_config[:converter]

      value = extract_value(data, directus_field)

      # Apply converter if provided
      value = converter.call(value) if converter.respond_to?(:call)

      mapped_data[bridgetown_field] = value
    else
      # Support for simple string mapping for backward compatibility
      directus_field = field_config.to_s
      mapped_data[bridgetown_field] = extract_value(data, directus_field)
    end
  end

  mapped_data
end

.map_translations(collection_config, data, locale) ⇒ Hash

Map translated fields from Directus data

Parameters:

  • collection_config (CollectionConfig)

    The collection configuration

  • data (Hash)

    The Directus data

  • locale (Symbol)

    The locale to map

Returns:

  • (Hash)

    The mapped data with translations



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bridgetown_directus/data_mapper.rb', line 40

def map_translations(collection_config, data, locale)
  # First map the base data
  mapped_data = map(collection_config, data)

  # If translations are enabled and the data has translations
  return mapped_data unless collection_config.translations_enabled && data["translations"]

  # Find the translation for the requested locale
  translation = find_translation_for_locale(data["translations"], locale)

  # Apply translations if found
  if translation
    apply_translations(collection_config, translation, mapped_data)
    mapped_data[:locale] = locale
  end

  mapped_data
end

.resolve_relationships(client, collection_config, data, relationships) ⇒ Hash

Resolve relationships in the data

Parameters:

  • client (Client)

    The Directus client

  • collection_config (CollectionConfig)

    The collection configuration

  • data (Hash)

    The mapped data

  • relationships (Hash)

    Relationship configuration

Returns:

  • (Hash)

    The data with resolved relationships



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bridgetown_directus/data_mapper.rb', line 65

def resolve_relationships(client, collection_config, data, relationships)
  return data unless relationships

  resolved_data = data.dup

  relationships.each do |field, relationship_config|
    relation_id = data[field]
    next unless relation_id

    related_collection = relationship_config[:collection]
    related_fields = relationship_config[:fields] || "*"

    # Fetch the related item
    related_item = client.fetch_item(
      related_collection,
      relation_id,
      { fields: related_fields }
    )

    # Add the related data to the resolved data
    if related_item && related_item["data"]
      resolved_data["#{field}_data"] = related_item["data"]
    end
  end

  resolved_data
end