Module: WCC::Contentful::Helpers

Extended by:
Helpers
Included in:
ContentTypeIndexer, Helpers, ModelBuilder
Defined in:
lib/wcc/contentful/helpers.rb

Instance Method Summary collapse

Instance Method Details

#constant_from_content_type(content_type) ⇒ Object



17
18
19
# File 'lib/wcc/contentful/helpers.rb', line 17

def constant_from_content_type(content_type)
  content_type.gsub(/[^_a-zA-Z0-9]/, '_').camelize
end

#content_type_from_constant(const) ⇒ Object



28
29
30
31
32
33
# File 'lib/wcc/contentful/helpers.rb', line 28

def content_type_from_constant(const)
  return const.content_type if const.respond_to?(:content_type)

  name = const.try(:name) || const.to_s
  name.demodulize.camelize(:lower)
end

#content_type_from_raw(value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/wcc/contentful/helpers.rb', line 6

def content_type_from_raw(value)
  case value.dig('sys', 'type')
  when 'Entry', 'DeletedEntry'
    value.dig('sys', 'contentType', 'sys', 'id')
  when 'Asset', 'DeletedAsset'
    'Asset'
  else
    raise ArgumentError, "Unknown content type '#{value.dig('sys', 'type') || 'null'}'"
  end
end

#shared_prefix(string_array) ⇒ Object



21
22
23
24
25
26
# File 'lib/wcc/contentful/helpers.rb', line 21

def shared_prefix(string_array)
  string_array.reduce do |l, s|
    l = l.chop while l != s[0...l.length]
    l
  end
end