Created
April 14, 2011 20:36
-
-
Save emiaj/920467 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using FubuCore; | |
using FubuMVC.Core.Registration; | |
using FubuMVC.Spark.Tokenization.Model; | |
namespace FubuMVC.Spark.Tokenization | |
{ | |
public interface IViewTokenizer | |
{ | |
IEnumerable<SparkViewToken> Tokenize(TypePool types, BehaviorGraph graph); | |
} | |
public class ViewTokenizer : IViewTokenizer | |
{ | |
private readonly ISparkFilesProvider _sparkFilesProvider; | |
public ViewTokenizer(ISparkFilesProvider sparkFilesProvider) | |
{ | |
_sparkFilesProvider = sparkFilesProvider; | |
} | |
public IEnumerable<SparkViewToken> Tokenize(TypePool types, BehaviorGraph graph) | |
{ | |
var sparkFiles = _sparkFilesProvider.GetFiles(types); | |
var validActions = graph.Actions().Where(x => x.HasOutput).ToLookup(x => x.OutputType()); | |
foreach (var sparkFile in sparkFiles) | |
{ | |
foreach (var action in validActions[sparkFile.ViewModelType]) | |
{ | |
yield return new SparkViewToken(sparkFile, action); | |
} | |
} | |
} | |
} | |
public interface ISparkFilesProvider | |
{ | |
IEnumerable<SparkFile> GetFiles(TypePool typePool); | |
} | |
public class SparkFileProvider : ISparkFilesProvider | |
{ | |
private readonly ISparkFileSource _source; | |
private readonly IFileSystem _fileSystem; | |
private readonly IEnumerable<ISparkFileEnricher> _enrichers; | |
public SparkFileProvider(ISparkFileSource source, IFileSystem fileSystem, IEnumerable<ISparkFileEnricher> enrichers) | |
{ | |
_source = source; | |
_fileSystem = fileSystem; | |
_enrichers = enrichers; | |
} | |
public IEnumerable<SparkFile> GetFiles(TypePool typePool) | |
{ | |
var sparkFiles = new SparkFiles(); | |
sparkFiles.AddRange(_source.GetFiles()); | |
foreach (var sparkFile in sparkFiles) | |
{ | |
applyEnrichers(sparkFiles, typePool, sparkFile); | |
} | |
return sparkFiles.Where(x => x.HasViewModel()); | |
} | |
private void applyEnrichers(SparkFiles sparkFiles, TypePool typePool, SparkFile sparkFile) | |
{ | |
foreach (var enricher in _enrichers) | |
{ | |
var context = getContext(sparkFiles, typePool, sparkFile); | |
enricher.Enrich(sparkFile, context); | |
} | |
} | |
private EnrichmentContext getContext(SparkFiles sparkFiles, TypePool typePool, SparkFile sparkFile) | |
{ | |
return new EnrichmentContext | |
{ | |
SparkFiles = sparkFiles, | |
TypePool = typePool, | |
FileContent = _fileSystem.ReadStringFromFile(sparkFile.Path) | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment