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) ⇒ Object



81
82
83
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 81

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

#map_asset(asset) ⇒ Object



65
66
67
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 65

def map_asset(asset)
  {'title' => asset.title, 'url' => asset.file.url}
end

#map_entry(child) ⇒ Object



69
70
71
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 69

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

#map_field(field_name, field_value) ⇒ Object



43
44
45
46
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 43

def map_field(field_name, field_value)
  value_mapping = map_value(field_value)
  return field_name.to_s, value_mapping
end


77
78
79
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 77

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

#map_location(location) ⇒ Object



73
74
75
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 73

def map_location(location)
  location.properties
end

#map_value(value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 48

def map_value(value)
  case value
  when ::Contentful::Asset
    map_asset(value)
  when ::Contentful::Location
    map_location(value)
  when ::Contentful::Link
    map_link(value)
  when ::Contentful::DynamicEntry
    map_entry(value)
  when ::Array
    map_array(value)
  else
    value
  end
end