Sha256: 39437ce014089cf4a742fc31e2ffb4735671040e285c77bc404cf97d8244be66

Contents?: true

Size: 1.76 KB

Versions: 27

Compression:

Stored size: 1.76 KB

Contents

module Pageflow
  class PageTypes
    include Enumerable

    def initialize
      clear
    end

    def clear
      @page_types = []
      @page_types_by_name = {}
    end

    def register(page_type)
      @page_types << page_type
      @page_types_by_name[page_type.name] = page_type

      ensure_export_version_implemented(page_type)
    end

    def find_by_name!(name)
      @page_types_by_name.fetch(name) do
        raise "Unknown page type with name #{name}."
      end
    end

    def names
      map(&:name)
    end

    def each(&block)
      @page_types.each(&block)
    end

    def setup(config)
      config.help_entries.register('pageflow.help_entries.page_types', priority: 10)
      each do |page_type|
        register_help_entries(config, page_type)
        register_revision_components(config, page_type)
        register_file_types(config, page_type)
      end
    end

    private

    def ensure_export_version_implemented(page_type)
      page_type.export_version
    end

    def register_help_entries(config, page_type)
      config.help_entries.register(page_type.help_entry_translation_key,
                                   parent: 'pageflow.help_entries.page_types')
    end

    def register_revision_components(config, page_type)
      page_type.revision_components.each do |component|
        config.revision_components.register(component)
      end
    end

    def register_file_types(config, page_type)
      # After a deprecation period against initializing FileType with
      # a model reference instead of a model name string, we could
      # rewrite this to register the page type's file types one by one
      # right here instead of lazily in FileTypes#each.
      config.file_types.register(
        -> { page_type.file_types }
      )
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
pageflow-17.0.4 lib/pageflow/page_types.rb
pageflow-17.0.3 lib/pageflow/page_types.rb
pageflow-17.0.2 lib/pageflow/page_types.rb
pageflow-17.0.1 lib/pageflow/page_types.rb
pageflow-17.0.0 lib/pageflow/page_types.rb
pageflow-16.2.0 lib/pageflow/page_types.rb
pageflow-16.1.0 lib/pageflow/page_types.rb
pageflow-16.0.0 lib/pageflow/page_types.rb
pageflow-15.8.0 lib/pageflow/page_types.rb
pageflow-15.7.1 lib/pageflow/page_types.rb
pageflow-15.7.0 lib/pageflow/page_types.rb
pageflow-15.6.1 lib/pageflow/page_types.rb
pageflow-15.6.0 lib/pageflow/page_types.rb
pageflow-15.5.0 lib/pageflow/page_types.rb
pageflow-15.4.0 lib/pageflow/page_types.rb
pageflow-15.3.0 lib/pageflow/page_types.rb
pageflow-15.2.2 lib/pageflow/page_types.rb
pageflow-15.2.1 lib/pageflow/page_types.rb
pageflow-15.2.0 lib/pageflow/page_types.rb
pageflow-15.1.2 lib/pageflow/page_types.rb