Class: Contentful::DatabaseImporter::IdGenerator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/database_importer/id_generator/base.rb

Overview

Base Id Generator

Direct Known Subclasses

ContentfulLike

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/contentful/database_importer/id_generator/base.rb', line 8

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/contentful/database_importer/id_generator/base.rb', line 6

def options
  @options
end

Instance Method Details

#find(entry_data, index, match) ⇒ Object



18
19
20
21
22
23
# File 'lib/contentful/database_importer/id_generator/base.rb', line 18

def find(entry_data, index, match)
  return options[match] if options.key?(match)
  return index.to_s if match == :index

  find_on_entry(entry_data, match)
end

#find_on_entry(entry_data, match) ⇒ Object



25
26
27
28
29
30
# File 'lib/contentful/database_importer/id_generator/base.rb', line 25

def find_on_entry(entry_data, match)
  return entry_data.excluded_fields[match] if entry_data.excluded_fields.key?(match)
  return entry_data.bootstrap_fields[match] if entry_data.bootstrap_fields.key?(match)

  raise "Template could not be resolved, #{match} not found."
end

#run(entry_data, index) ⇒ Object



12
13
14
15
16
# File 'lib/contentful/database_importer/id_generator/base.rb', line 12

def run(entry_data, index)
  options.fetch(:template, '').gsub(/\{\{([\w|\.]+)\}\}/) do |match|
    find(entry_data, index, match.gsub('{{', '').gsub('}}', '').to_sym)
  end
end