Class: Jekyll::Contentful::Mappers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-contentful-data-import/mappers/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, config) ⇒ Base

Returns a new instance of Base.



21
22
23
24
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 21

def initialize(entry, config)
  @entry = entry
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 19

def config
  @config
end

#entryObject (readonly)

Returns the value of attribute entry.



19
20
21
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 19

def entry
  @entry
end

Class Method Details

.mapper_for(entry, config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 7

def self.mapper_for(entry, config)
  ct = entry.content_type

  mapper_name = config.fetch(
    'content_types', {}
  ).fetch(
    ct.id, ::Jekyll::Contentful::Mappers::Base.to_s
  )

  Module.const_get(mapper_name).new(entry, config)
end

Instance Method Details

#has_multiple_locales?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 39

def has_multiple_locales?
  config.fetch('cda_query', {}).fetch('locale', nil) == '*'
end

#mapObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 26

def map
  result = {'sys' => {'id' => entry.id}}

  fields = has_multiple_locales? ? entry.fields_with_locales : entry.fields

  fields.each do |k, v|
    name, value = map_field(k, v)
    result[name] = value
  end

  result
end

#map_array(array, locale) ⇒ Object



121
122
123
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 121

def map_array(array, locale)
  array.map {|element| map_value(element, locale)}
end

#map_asset(asset, locale = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 83

def map_asset(asset, locale = nil)
  if locale
    file = asset.fields(locale)[:file]
    file_url = file.nil? ? '' : file.url

    return {
      'title' => asset.fields(locale)[:title],
      'description' => asset.fields(locale)[:description],
      'url' => file_url
    }
  end

  file = asset.file
  file_url = file.nil? ? '' : file.url

  {
    'title' => asset.title,
    'description' => asset.description,
    'url' => file_url
  }
end

#map_entry(child) ⇒ Object



105
106
107
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 105

def map_entry(child)
  self.class.mapper_for(child, config).map
end

#map_field(field_name, field_value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 43

def map_field(field_name, field_value)
  value_mapping = nil

  if has_multiple_locales?
    value_mapping = {}
    field_value.each do |locale, value|
      value_mapping[locale.to_s] = map_value(value, locale.to_s)
    end
  else
    value_mapping = map_value(field_value)
  end

  return field_name.to_s, value_mapping
end


117
118
119
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 117

def map_link(link)
  {'sys' => {'id' => link.id}}
end

#map_location(location) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 109

def map_location(location)
  result = {}
  location.properties.each do |k, v|
    result[k.to_s] = v
  end
  result
end

#map_value(value, locale = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 58

def map_value(value, locale = nil)
  case value
  when ::Contentful::Asset
    map_asset(value, locale)
  when ::Contentful::Location
    map_location(value)
  when ::Contentful::Link
    map_link(value)
  when ::Contentful::DynamicEntry
    map_entry(value)
  when ::Array
    map_array(value, locale)
  when ::Symbol
    value.to_s
  when ::Hash
    result = {}
    value.each do |k, v|
      result[k.to_s] = map_value(v, locale)
    end
    result
  else
    value
  end
end