Sha256: 84e74bf6a30821ea47ca92eb714d906d97f78b334e3b25d7a22ae0c07d93c100

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'seory/runtime'

module Seory
  class DuplicateDefault < Seory::Error; end

  class Repository
    class << self
      def extract_label_from_trace(trace)
        trace.first.split(':').first
      end
    end

    attr_accessor :helper

    def initialize
      @page_groups = []
    end

    def <<(page_group)
      remove_old_group!(page_group.name)

      @page_groups << page_group

      clear_page_order_pre_calculation!
    end

    def lookup(view_context)
      page = pre_orderd_pages.detect {|pg| pg.match?(view_context.controller) } || default

      Seory::Runtime.new(page, view_context, default).tap do |runtime|
        runtime.extend helper if helper
      end
    end

    def default
      @default ||=
        @page_groups.map(&:default).compact.tap {|defaults|
          raise DuplicateDefault if defaults.size > 1
        }.first
    end

    private

    def pre_orderd_pages
      @pre_orderd_pages ||= @page_groups.sort_by(&:name).flat_map(&:pages)
    end

    def remove_old_group!(page_group_name)
      @page_groups.reject! {|pg| pg.name == page_group_name }
    end

    def clear_page_order_pre_calculation!
      @pre_orderd_pages = nil
      @default = nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seory-0.2.0 lib/seory/repository.rb