Sha256: ab0e9500a7e9c039a14c418eaa105f51ffc552651b1632ebb92f67ac6b6ef66c

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Generators.Output;

namespace Generators
{
    public class ExerciseCollection : IEnumerable<Exercise>
    {
        private readonly string[] _exerciseNames;

        public ExerciseCollection() => _exerciseNames = null;
        public ExerciseCollection(string[] exerciseNames) => _exerciseNames = exerciseNames;
        
        public IEnumerator<Exercise> GetEnumerator() => GetDefinedGenerators().GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

        private IEnumerable<Exercise> GetDefinedGenerators() =>
            from type in Assembly.GetEntryAssembly().GetTypes()
            where IsConcreteExercise(type) && ShouldBeIncluded(type)
            select (Exercise)Activator.CreateInstance(type);

        private static bool IsConcreteExercise(Type type) => typeof(Exercise).IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract;

        private bool ShouldBeIncluded(Type type) 
            => _exerciseNames == null || 
               _exerciseNames.Contains(type.ToExerciseName(), StringComparer.OrdinalIgnoreCase) || 
               _exerciseNames.Contains(type.Name, StringComparer.OrdinalIgnoreCase);
    }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/csharp/generators/ExerciseCollection.cs
trackler-2.1.0.52 tracks/csharp/generators/ExerciseCollection.cs
trackler-2.1.0.51 tracks/csharp/generators/ExerciseCollection.cs
trackler-2.1.0.50 tracks/csharp/generators/ExerciseCollection.cs
trackler-2.1.0.49 tracks/csharp/generators/ExerciseCollection.cs