Class: Contentful::Bootstrap::Templates::JsonTemplate

Inherits:
Base
  • Object
show all
Defined in:
lib/contentful/bootstrap/templates/json_template.rb

Constant Summary collapse

CONTENT_TYPES_KEY =
'contentTypes'
ENTRIES_KEY =
'entries'
ASSETS_KEY =
'assets'
BOOTSTRAP_PROCCESSED_KEY =
'bootstrapProcessed'
DISPLAY_FIELD_KEY =
'displayField'
ALTERNATE_DISPLAY_FIELD_KEY =
'display_field'
SYS_KEY =
'sys'

Instance Attribute Summary collapse

Attributes inherited from Base

#skip_content_types, #space

Instance Method Summary collapse

Methods inherited from Base

#run

Constructor Details

#initialize(space, file, mark_processed = false, all = true, skip_content_types = false) ⇒ JsonTemplate

Returns a new instance of JsonTemplate.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/contentful/bootstrap/templates/json_template.rb', line 19

def initialize(space, file, mark_processed = false, all = true, skip_content_types = false)
  super(space, skip_content_types)
  @file = file
  @all = all
  @mark_processed = mark_processed

  json

  @assets = process_assets
  @entries = process_entries
  @content_types = process_content_types

  check_version
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



17
18
19
# File 'lib/contentful/bootstrap/templates/json_template.rb', line 17

def assets
  @assets
end

#content_typesObject (readonly)

Returns the value of attribute content_types.



17
18
19
# File 'lib/contentful/bootstrap/templates/json_template.rb', line 17

def content_types
  @content_types
end

#entriesObject (readonly)

Returns the value of attribute entries.



17
18
19
# File 'lib/contentful/bootstrap/templates/json_template.rb', line 17

def entries
  @entries
end

Instance Method Details

#after_runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/contentful/bootstrap/templates/json_template.rb', line 34

def after_run
  return unless mark_processed?

  @json.fetch(CONTENT_TYPES_KEY, []).each do |content_type|
    content_type[BOOTSTRAP_PROCCESSED_KEY] = true
  end

  @json.fetch(ASSETS_KEY, []).each do |asset|
    asset[BOOTSTRAP_PROCCESSED_KEY] = true
  end

  @json.fetch(ENTRIES_KEY, {}).each do |_content_type_name, entry_list|
    entry_list.each do |entry|
      if entry.key?(SYS_KEY)
        entry[SYS_KEY][BOOTSTRAP_PROCCESSED_KEY] = true
      else
        entry[SYS_KEY] = { BOOTSTRAP_PROCCESSED_KEY => true }
      end
    end
  end

  ::File.write(@file, JSON.pretty_generate(@json))
end