Created
April 29, 2011 20:57
-
-
Save emiaj/949032 to your computer and use it in GitHub Desktop.
ISharedItemLocator.cs
This file contains hidden or 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
| public interface ISharedItemLocator | |
| { | |
| SparkItem LocateItem(string sparkName, SparkItem fromItem, IEnumerable<SparkItem> items); | |
| } | |
| // REVIEW: This class is too busy. Let's see if we can take parts out of it into classes. | |
| public class SharedItemLocator : ISharedItemLocator | |
| { | |
| private readonly SharedDirectoryProvider _sharedDirectoryProvider; | |
| public SharedItemLocator(IEnumerable<string> sharedFolderNames) | |
| { | |
| _sharedDirectoryProvider = new SharedDirectoryProvider(sharedFolderNames); | |
| } | |
| public SparkItem LocateItem(string sparkName, SparkItem fromItem, IEnumerable<SparkItem> items) | |
| { | |
| var reachables = _sharedDirectoryProvider.GetDirectories(fromItem, items); | |
| var item = items.ByName(sparkName).InDirectories(reachables).FirstOrDefault(); | |
| return item; | |
| } | |
| } | |
| public class SharedFolderFinder | |
| { | |
| private readonly IEnumerable<string> _sharedFolderNames; | |
| public SharedFolderFinder(IEnumerable<string> sharedFolderNames) | |
| { | |
| _sharedFolderNames = sharedFolderNames; | |
| } | |
| public IEnumerable<string> Find(string root, string path) | |
| { | |
| return reachableLocations(root, path); | |
| } | |
| private IEnumerable<string> reachableLocations(string root, string path) | |
| { | |
| do | |
| { | |
| path = Path.GetDirectoryName(path); | |
| if (path == null) break; | |
| foreach (var sharedFolder in _sharedFolderNames) | |
| { | |
| yield return Path.Combine(path, sharedFolder); | |
| } | |
| } while (path.IsNotEmpty() && path.PathRelativeTo(root).IsNotEmpty()); | |
| } | |
| } | |
| public class SharedDirectoryProvider | |
| { | |
| private readonly SharedFolderFinder _sharedFolderFinder; | |
| public SharedDirectoryProvider(IEnumerable<string> sharedFolderNames) | |
| { | |
| _sharedFolderFinder = new SharedFolderFinder(sharedFolderNames); | |
| } | |
| public IEnumerable<string> GetDirectories(SparkItem item, IEnumerable<SparkItem> items) | |
| { | |
| var candidates = getCandidateDirectories(item, items); | |
| return candidates; | |
| } | |
| private IEnumerable<string> getCandidateDirectories(SparkItem item, IEnumerable<SparkItem> items) | |
| { | |
| foreach (var directory in _sharedFolderFinder.Find(item.RootPath, item.FilePath)) | |
| { | |
| yield return directory; | |
| } | |
| if (item.Origin == FubuSparkConstants.HostOrigin) | |
| { | |
| yield break; | |
| } | |
| var hostRoot = items.ByOrigin(FubuSparkConstants.HostOrigin).FirstValue(x => x.RootPath); | |
| if (hostRoot.IsEmpty()) | |
| { | |
| yield break; | |
| } | |
| foreach (var directory in _sharedFolderFinder.Find(hostRoot, hostRoot)) | |
| { | |
| yield return directory; | |
| } | |
| } | |
| } | |
| public static class SparkItemEnumerableExtensions | |
| { | |
| public static IEnumerable<SparkItem> ByName(this IEnumerable<SparkItem> items, string name) | |
| { | |
| return items.Where(x => x.Name() == name); | |
| } | |
| public static IEnumerable<SparkItem> ByOrigin(this IEnumerable<SparkItem> items, string origin) | |
| { | |
| return items.Where(x => x.Origin == origin); | |
| } | |
| public static IEnumerable<SparkItem> InDirectories(this IEnumerable<SparkItem> items,IEnumerable<string> directories) | |
| { | |
| var predicate = new CompositePredicate<SparkItem>(); | |
| predicate = directories.Aggregate(predicate, (current, local) => current + (x => x.DirectoryPath() == local)); | |
| return items.Where(predicate.MatchesAny); | |
| } | |
| public static SparkItem FirstByName(this IEnumerable<SparkItem> items, string name) | |
| { | |
| return items.ByName(name).FirstOrDefault(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment