Class: Contentful::Converter::ContentTypesStructureCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/converters/content_types_structure_creator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ContentTypesStructureCreator

Returns a new instance of ContentTypesStructureCreator.



6
7
8
9
# File 'lib/converters/content_types_structure_creator.rb', line 6

def initialize(config)
  @config = config
  @logger = Logger.new(STDOUT)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/converters/content_types_structure_creator.rb', line 4

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/converters/content_types_structure_creator.rb', line 4

def logger
  @logger
end

Instance Method Details

#create_content_type_json_file(content_type_name, values) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/converters/content_types_structure_creator.rb', line 11

def create_content_type_json_file(content_type_name, values)
  collection = {
    id: values[:id],
    name: values[:name],
    description: values[:description],
    displayField: values[:displayField],
    fields: create_fields(values[:fields])
  }
  write_json_to_file("#{config.collections_dir}/#{content_type_name}.json", collection)
  logger.info "Saving #{content_type_name}.json to #{config.collections_dir}"
end

#create_field(field, value) ⇒ Object



35
36
37
# File 'lib/converters/content_types_structure_creator.rb', line 35

def create_field(field, value)
  value.is_a?(Hash) ? value[:id] : field
end

#create_fields(fields) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/converters/content_types_structure_creator.rb', line 23

def create_fields(fields)
  fields.each_with_object([]) do |(field, value), results|
    results << {
      name: create_field(field, value).capitalize,
      id: create_field(field, value),
      type: create_type_field(value),
      link_type: create_link_type_field(value),
      link: create_link_field(value)
    }.compact
  end
end


47
48
49
# File 'lib/converters/content_types_structure_creator.rb', line 47

def create_link_field(value)
  value.is_a?(Hash) ? value[:link] : nil
end


39
40
41
# File 'lib/converters/content_types_structure_creator.rb', line 39

def create_link_type_field(value)
  value.is_a?(Hash) ? value[:link_type] : nil
end

#create_type_field(value) ⇒ Object



43
44
45
# File 'lib/converters/content_types_structure_creator.rb', line 43

def create_type_field(value)
  value.is_a?(Hash) ? value[:type] : value
end

#write_json_to_file(path, data) ⇒ Object



51
52
53
54
55
# File 'lib/converters/content_types_structure_creator.rb', line 51

def write_json_to_file(path, data)
  File.open(path, 'w') do |file|
    file.write(JSON.pretty_generate(data))
  end
end