Module: Contentful::DatabaseImporter::Resource

Includes:
ResourceBootstrapMethods, ResourceCoercions, ResourceRelationships
Defined in:
lib/contentful/database_importer/resource.rb

Overview

Resource for mapping Database Tables to Contentful Content Types and Entries

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceBootstrapMethods

#to_bootstrap, #to_link

Methods included from ResourceRelationships

#fetch_many, #fetch_one, #fetch_relations, #fetch_through, #fetch_through_table_rows, #resolve_through_relationship

Methods included from ResourceCoercions

#coerce, #coerce_array, #coerce_array_location, #coerce_asset, #coerce_boolean, #coerce_date, #coerce_hash_location, #coerce_integer, #coerce_location, #coerce_number, #coerce_object, #coerce_symbol, #create_associated_asset

Instance Attribute Details

#associated_assetsObject (readonly)

Returns the value of attribute associated_assets.



25
26
27
# File 'lib/contentful/database_importer/resource.rb', line 25

def associated_assets
  @associated_assets
end

#bootstrap_fieldsObject (readonly)

Returns the value of attribute bootstrap_fields.



25
26
27
# File 'lib/contentful/database_importer/resource.rb', line 25

def bootstrap_fields
  @bootstrap_fields
end

#excluded_fieldsObject (readonly)

Returns the value of attribute excluded_fields.



25
26
27
# File 'lib/contentful/database_importer/resource.rb', line 25

def excluded_fields
  @excluded_fields
end

#indexObject (readonly)

Returns the value of attribute index.



25
26
27
# File 'lib/contentful/database_importer/resource.rb', line 25

def index
  @index
end

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend(ResourceClassMethods)
  base.extend(ResourceFieldClassMethods)
  base.extend(ResourceBootstrapClassMethods)
end

Instance Method Details

#idObject



81
82
83
84
85
86
87
88
89
# File 'lib/contentful/database_importer/resource.rb', line 81

def id
  if self.class.id_generator.nil?
    self.class.id_generator = IdGenerator::Base.new(
      self.class.default_generator_options
    )
  end

  self.class.id_generator.run(self, index)
end

#initialize(row, index = 0) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/contentful/database_importer/resource.rb', line 30

def initialize(row, index = 0)
  @index = index
  @bootstrap_fields = {}
  @excluded_fields = {}
  @raw = row
  @associated_assets = []

  row.each do |db_name, value|
    process_row_field(db_name, value)
  end

  process_relationships
end

#pre_process(field_definition, value) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/contentful/database_importer/resource.rb', line 66

def pre_process(field_definition, value)
  return value unless field_definition[:pre_process]

  transformation = field_definition[:pre_process]

  return send(transformation, value) if transformation.is_a? ::Symbol
  return transformation.call(value) if transformation.respond_to?(:call)

  raise
rescue
  error = 'Pre Process could not be done for '
  error += "#{field_definition[:maps_to]} - #{transformation}"
  raise error
end

#process_relationshipsObject



59
60
61
62
63
64
# File 'lib/contentful/database_importer/resource.rb', line 59

def process_relationships
  self.class.relationship_fields.each do |relationship_field_definition|
    relations = fetch_relations(relationship_field_definition)
    @bootstrap_fields[relationship_field_definition[:maps_to]] = relations
  end
end

#process_row_field(db_name, value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/contentful/database_importer/resource.rb', line 44

def process_row_field(db_name, value)
  field_definition = self.class.fields.find { |f| f[:db_name] == db_name }

  return unless field_definition

  value = pre_process(field_definition, value)
  value = coerce(field_definition, value)

  if field_definition[:exclude_from_output]
    @excluded_fields[field_definition[:maps_to]] = value
  else
    @bootstrap_fields[field_definition[:maps_to]] = value
  end
end