Sha256: aec32c4d109c0d22530a747c58938939c2535685ad26879c6647e2b71471fd20

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'singleton'

require_relative 'layer'

module AxR
  class App
    include Singleton

    LayerConflictError = Class.new(StandardError)

    attr_reader :layers

    def initialize
      @layers = []
    end

    def define(&block)
      instance_eval(&block)
    end

    def layer(layer_name, options = {})
      check_name_conflict!(layer_name)

      layers << AxR::Layer.new(layer_name, layers.size, options)
    end

    def layer_names
      layers.map(&:name).map(&:to_s)
    end

    # rubocop:disable Metrics/AbcSize
    def legal?(context, dependncy)
      ctx = layers.find { |l| l.name.to_s == context.to_s }
      dep = layers.find { |l| l.name.to_s == dependncy.to_s }

      return false unless ctx && dep
      return false if ctx.isolated?
      return true if ctx.familiar_with.map(&:to_s).include?(dependncy.to_s)

      ctx.level < dep.level
    end
    # rubocop:enable Metrics/AbcSize

    private

    def check_name_conflict!(layer_name)
      return unless layers.map(&:name).include?(layer_name)

      raise LayerConflictError, "Layer #{layer_name} is already defined in the app"
    end

    # Use only for testing purpose!
    def reset
      @layers = []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axr-0.5.0 lib/axr/app.rb