Class: Jekyll::Contentful::Mappers::Base
- Inherits:
-
Object
- Object
- Jekyll::Contentful::Mappers::Base
- Defined in:
- lib/jekyll-contentful-data-import/mappers/base.rb
Overview
Base Mapper Class
Logic for mapping entries into simplified serialized representations
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#entry ⇒ Object
readonly
Returns the value of attribute entry.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(entry, config) ⇒ Base
constructor
A new instance of Base.
- #map ⇒ Object
- #map_array(array, locale) ⇒ Object
- #map_asset(asset, locale = nil) ⇒ Object
- #map_asset_metadata(asset) ⇒ Object
- #map_entry(child) ⇒ Object
- #map_entry_metadata ⇒ Object
- #map_field(field_name, field_value, multiple_locales = false) ⇒ Object
- #map_link(link) ⇒ Object
- #map_location(location) ⇒ Object
- #map_value(value, locale = nil) ⇒ Object
- #multiple_locales? ⇒ Boolean
Constructor Details
#initialize(entry, config) ⇒ Base
Returns a new instance of Base.
25 26 27 28 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 25 def initialize(entry, config) @entry = entry @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
23 24 25 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 23 def config @config end |
#entry ⇒ Object (readonly)
Returns the value of attribute entry.
23 24 25 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 23 def entry @entry end |
Class Method Details
.mapper_for(entry, config) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 11 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
#map ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 40 def map result = { 'sys' => } fields = multiple_locales? ? entry.fields_with_locales : entry.fields fields.each do |k, v| name, value = map_field(k, v, multiple_locales?) result[name] = value end result end |
#map_array(array, locale) ⇒ Object
148 149 150 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 148 def map_array(array, locale) array.map { |element| map_value(element, locale) } end |
#map_asset(asset, locale = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 105 def map_asset(asset, locale = nil) if locale file = asset.fields(locale)[:file] file_url = file.nil? ? '' : file.url return { 'sys' => (asset), 'title' => asset.fields(locale)[:title], 'description' => asset.fields(locale)[:description], 'url' => file_url } end file = asset.file file_url = file.nil? ? '' : file.url { 'sys' => (asset), 'title' => asset.title, 'description' => asset.description, 'url' => file_url } end |
#map_asset_metadata(asset) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 97 def (asset) { 'id' => asset.id, 'created_at' => asset.sys.fetch(:created_at, nil), 'updated_at' => asset.sys.fetch(:updated_at, nil) } end |
#map_entry(child) ⇒ Object
129 130 131 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 129 def map_entry(child) self.class.mapper_for(child, config).map end |
#map_entry_metadata ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 30 def content_type = entry.sys.fetch(:content_type, nil) { 'id' => entry.sys.fetch(:id, nil), 'created_at' => entry.sys.fetch(:created_at, nil), 'updated_at' => entry.sys.fetch(:updated_at, nil), 'content_type_id' => content_type.nil? ? nil : content_type.id } end |
#map_field(field_name, field_value, multiple_locales = false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 57 def map_field(field_name, field_value, multiple_locales = false) value_mapping = nil if 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 [field_name.to_s, value_mapping] end |
#map_link(link) ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 140 def map_link(link) { 'sys' => { 'id' => link.id } } end |
#map_location(location) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 133 def map_location(location) { 'lat' => location.latitude, 'lon' => location.longitude } end |
#map_value(value, locale = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 72 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::Entry 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 |
#multiple_locales? ⇒ Boolean
53 54 55 |
# File 'lib/jekyll-contentful-data-import/mappers/base.rb', line 53 def multiple_locales? config.fetch('cda_query', {}).fetch('locale', nil) == '*' end |