Permalink
Browse files

Using dynamic for item and layout meta instead of dictionary.

  • Loading branch information...
1 parent ab5b44d commit 4c79dfd03dc3fbc20867a99caa5a989684c9ba79 @dragan committed Sep 21, 2012
@@ -30,7 +30,7 @@ Route("/stylesheet/", (context) => {
Route("*", (context) => {
if (context.Item.IsBinary) {
// Write item with identifier /foo/ to /foo.ext
- return context.Item.Identifier.Chop() + context.Item.Meta["extension"];
+ return context.Item.Identifier.Chop() + context.Item.Meta.Extension;
} else {
// Write item with identifier /foo/ to /foo/index.html
return context.Item.Identifier + "index.html";
@@ -93,11 +93,12 @@ void ExecuteFilters(StaticFile staticFile, Site site)
string source = staticFile.GetLastSnapShot();
- dynamic model = new ExpandoObject();
- model.Configuration = site.Configuration;
- model.Item = staticFile.Item.Meta;
+ var filterContext = new FilterContext {
+ Configuration = site.Configuration,
+ Item = staticFile.Item.Meta
+ };
- string result = filter.Execute(source, model);
+ string result = filter.Execute(source, filterContext);
staticFile.CreateSnapShot(result);
}
@@ -110,15 +111,15 @@ void ExecuteLayoutFilter(StaticFile staticFile, Site site)
IFilter filter = filterFactory.CreateFilter(layoutRule.FilterName);
+ var filterContext = new FilterContext {
+ Configuration = site.Configuration,
+ Layout = staticFile.Layout.Meta,
+ Item = staticFile.Item.Meta,
+ Content = staticFile.GetLastSnapShot()
+ };
- dynamic model = new ExpandoObject();
- model.Configuration = site.Configuration;
- model.Layout = staticFile.Layout.Meta;
- model.Item = staticFile.Item.Meta;
- model.Content = staticFile.GetLastSnapShot();
-
- string result = filter.Execute(staticFile.Layout.Content, model);
-
+ string result = filter.Execute(staticFile.Layout.Content, filterContext);
+
staticFile.CreateSnapShot(result);
}
}
@@ -0,0 +1,14 @@
+using System;
+
+using Mulder.Base.Domain;
+
+namespace Mulder.Base.Compilation
+{
+ public class FilterContext
+ {
+ public IConfiguration Configuration { get; set; }
+ public dynamic Layout { get; set; }
+ public dynamic Item { get; set; }
+ public string Content { get; set; }
+ }
+}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
@@ -153,13 +154,12 @@ List<FileObject> GetFileObjects(string path)
bool isBinary = IsBinary(contentFileName);
ParsedFileData parsedFileData = !isBinary
- ? ParseFiles(contentFileName, metaFileName) : new ParsedFileData { Content = "", Meta = new Dictionary<string, object>() };
+ ? ParseFiles(contentFileName, metaFileName) : new ParsedFileData { Content = "", Meta = new ExpandoObject() };
- IDictionary<string, object> meta = new Dictionary<string, object>(parsedFileData.Meta) {
- { "filename", contentFileName },
- { "meta_filename", metaFileName},
- { "extension", contentFileExtension }
- };
+ dynamic meta = parsedFileData.Meta;
+ meta.Filename = contentFileName;
+ meta.MetaFilename = metaFileName;
+ meta.Extension = contentFileExtension;
string contentFileNameWithoutPath = contentFileName.RemovePathFromFileName(path);
string identifier = GetIdentifierForFileName(contentFileNameWithoutPath);
@@ -234,17 +234,18 @@ bool IsBinary(string fileName)
ParsedFileData ParseFiles(string rawContentFileName, string metaFileName)
{
- IDictionary<string, object> meta = null;
+ dynamic meta = null;
string fileContent = string.Empty;
// If we have a stand alone meta file
if (!string.IsNullOrEmpty(metaFileName)) {
fileContent = !string.IsNullOrEmpty(rawContentFileName) ? fileSystem.ReadStringFromFile(rawContentFileName) : string.Empty;
- meta = fileSystem.ReadStringFromFile(metaFileName).DeserializeYaml()[0] as IDictionary<string, object>;
+ var yaml = fileSystem.ReadStringFromFile(metaFileName).DeserializeYaml()[0] as IDictionary<string, object>;
+ meta = yaml.ToExpando();
}
else {
string rawFileContent = !string.IsNullOrEmpty(rawContentFileName) ? fileSystem.ReadStringFromFile(rawContentFileName) : string.Empty;
- meta = rawFileContent.DeserializeYamlHeader();
+ meta = rawFileContent.DeserializeYamlHeader().ToExpando();
fileContent = rawFileContent.ExcludeYamlHeader();
}
@@ -301,14 +302,14 @@ class FileObject
public string Identifier { get; set; }
public bool IsBinary { get; set; }
public string Content { get; set; }
- public IDictionary<string, object> Meta { get; set; }
+ public dynamic Meta { get; set; }
public DateTime ModificationTime { get; set; }
}
class ParsedFileData
{
public string Content { get; set; }
- public IDictionary<string, object> Meta { get; set; }
+ public dynamic Meta { get; set; }
}
}
}
@@ -7,7 +7,7 @@ public interface ISourceFile
{
string Identifier { get; }
string Content { get; }
- IDictionary<string, object> Meta { get; }
+ dynamic Meta { get; }
DateTime ModificationTime { get; }
}
}
@@ -8,21 +8,21 @@ public class Item : ISourceFile, IEquatable<Item>
readonly string identifier;
readonly bool isBinary;
readonly string content;
- readonly IDictionary<string, object> meta;
+ readonly dynamic meta;
readonly DateTime modificationTime;
readonly List<Item> children;
readonly List<StaticFile> staticFiles;
public string Identifier { get { return identifier; } }
public bool IsBinary { get { return isBinary; } }
public string Content { get { return content; } }
- public IDictionary<string, object> Meta { get { return meta; } }
+ public dynamic Meta { get { return meta; } }
public DateTime ModificationTime { get { return modificationTime; } }
public Item Parent { get; set; }
public IEnumerable<Item> Children { get { return children; } }
public IEnumerable<StaticFile> StaticFiles { get { return staticFiles; } }
- public Item(string identifier, bool isBinary, string content, IDictionary<string, object> meta, DateTime modificationTime)
+ public Item(string identifier, bool isBinary, string content, dynamic meta, DateTime modificationTime)
{
this.identifier = identifier;
this.isBinary = isBinary;
@@ -53,7 +53,7 @@ public override string ToString()
stringBuilder.AppendLine("IsBinary: \"" + isBinary + "\"");
stringBuilder.AppendLine("Content: \"" + content + "\"");
stringBuilder.AppendLine("Meta:");
- foreach (var kvp in meta) {
+ foreach (var kvp in meta as IDictionary<string, object>) {
stringBuilder.AppendLine(" " + kvp.Key + ": \"" + kvp.Value.ToString() + "\"");
}
@@ -88,9 +88,11 @@ public bool Equals(Item other)
return false;
bool metasAreEqual = true;
- if (meta.Count == other.meta.Count) {
- foreach (var kvp in meta) {
- if (other.meta[kvp.Key].ToString() == kvp.Value.ToString())
+ var metaDictionary = meta as IDictionary<string, object>;
+ var otherMetaDictionary = other.meta as IDictionary<string, object>;
+ if (metaDictionary.Count == otherMetaDictionary.Count) {
+ foreach (var kvp in metaDictionary) {
+ if (otherMetaDictionary[kvp.Key].ToString() == kvp.Value.ToString())
continue;
metasAreEqual = false;
@@ -7,15 +7,15 @@ public class Layout : ISourceFile, IEquatable<Layout>
{
readonly string identifier;
readonly string content;
- readonly IDictionary<string, object> meta;
+ readonly dynamic meta;
readonly DateTime modificationTime;
public string Identifier { get { return identifier; } }
public string Content { get { return content; } }
- public IDictionary<string, object> Meta { get { return meta; } }
+ public dynamic Meta { get { return meta; } }
public DateTime ModificationTime { get { return modificationTime; } }
- public Layout(string identifier, string content, IDictionary<string, object> meta, DateTime modificationTime)
+ public Layout(string identifier, string content, dynamic meta, DateTime modificationTime)
{
this.identifier = identifier;
this.content = content;
@@ -31,7 +31,7 @@ public override string ToString()
stringBuilder.AppendLine("Identifier: \"" + identifier + "\"");
stringBuilder.AppendLine("Content: \"" + content + "\"");
stringBuilder.AppendLine("Meta:");
- foreach (var kvp in meta) {
+ foreach (var kvp in meta as IDictionary<string, object>) {
stringBuilder.AppendLine(" " + kvp.Key + ": \"" + kvp.Value.ToString() + "\"");
}
@@ -64,9 +64,11 @@ public bool Equals(Layout other)
return false;
bool metasAreEqual = true;
- if (meta.Count == other.meta.Count) {
- foreach (var kvp in meta) {
- if (other.meta[kvp.Key].ToString() == kvp.Value.ToString())
+ var metaDictionary = meta as IDictionary<string, object>;
+ var otherMetaDictionary = other.meta as IDictionary<string, object>;
+ if (metaDictionary.Count == otherMetaDictionary.Count) {
+ foreach (var kvp in metaDictionary) {
+ if (otherMetaDictionary[kvp.Key].ToString() == kvp.Value.ToString())
continue;
metasAreEqual = false;
@@ -0,0 +1,42 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Dynamic;
+
+namespace Mulder.Base.Extensions
+{
+ public static class DictionaryExtensions
+ {
+ public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
+ {
+ var expando = new ExpandoObject();
+ var expandoDictionary = (IDictionary<string, object>)expando;
+
+ foreach (var kvp in dictionary) {
+ if (kvp.Value is IDictionary<string, object>) {
+ var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
+ expandoDictionary.Add(kvp.Key, expandoValue);
+ }
+ else if (kvp.Value is ICollection) {
+ var itemList = new List<object>();
+ foreach (var item in (ICollection)kvp.Value) {
+ if (item is IDictionary<string, object>) {
+ var expandoItem = ((IDictionary<string, object>) item).ToExpando();
+ itemList.Add(expandoItem);
+ }
+ else {
+ itemList.Add(item);
+ }
+ }
+
+ expandoDictionary.Add(kvp.Key, itemList);
+ }
+ else {
+ expandoDictionary.Add(kvp);
+ }
+ }
+
+ return expando;
+ }
+ }
+}
@@ -1,9 +1,11 @@
using System;
+using Mulder.Base.Compilation;
+
namespace Mulder.Base.Filters
{
public interface IFilter
{
- string Execute(string source, dynamic model);
+ string Execute(string source, FilterContext filterContext);
}
}
@@ -8,6 +8,7 @@
using dotless.Core.Parser;
using dotless.Core.Stylizers;
+using Mulder.Base.Compilation;
using Mulder.Base.IO;
namespace Mulder.Base.Filters
@@ -21,9 +22,9 @@ public LessFilter(IFileSystem fileSystem)
this.fileSystem = fileSystem;
}
- public string Execute(string source, dynamic model)
+ public string Execute(string source, FilterContext filterContext)
{
- var item = model.Item as IDictionary<string, object>;
+ var item = filterContext.Item as IDictionary<string, object>;
string path = Path.GetDirectoryName(item["filename"].ToString());
string result = Transform(source, path);
Oops, something went wrong.

0 comments on commit 4c79dfd

Please sign in to comment.