module Eco module API module UseCases class UseGroup def initialize() @usecases = {} @cache_init = false @cases_by_name = {} end def add(usecase) raise "Expected UseCase object. Given: #{usecase}" if !usecase || !usecase.is_a?(UseCase) name = usecase.name type = usecase.type puts "Warning: overriding '#{type.to_s}' case #{name}" if self.defined?(name, type: type) @cache_init = false @usecases[key(name, type)] = usecase usecase end def define(name, type:, &block) add(UseCase.new(name, type: type, root:self, &block)) end def defined?(name, type: nil) return @usecases.key?(key(name,type)) if type name?(name) end def name?(name) !!by_name[name] end def types(name) return nil if !name?(name) by_name[name].map { |usecase| usecase.type } end def case(name, type: nil) return @usecases[key(name, type)] if type return nil unless cases = by_name[name] msg = "There are cases of different types (#{types(name).map(&:to_s).join(", ")}) named '#{name}'. You should specify a correct type" raise msg if cases.length > 1 cases.first end def names by_name.keys end # merges cases overriding self for exisint parsers def merge(cases) return self if !cases raise "Expected a UseGroup object. Given #{cases}" if !cases.is_a?(UseGroup) cases_hash = cases.hash @usecases.merge!(cases_hash) cases_hash.transform_values do |usecase| usecase.root = self end @cache_init = false self end protected def hash @usecases end private def by_name init_caches @by_name end def init_caches return true if @cache_init @cache_init = true @by_name = @usecases.values.group_by { |usecase| usecase.name } end def key(name, type) name + type.to_s end def name(key) key&.split(":").first end def type key&.split(":").last&.to_sym end end end end end