Example #1
0
        public override void Run(Context context)
        {
            if (Content == null)
            {
                context.Error("Content required for Add");
                context.Current = null;
                return;
            }

            var key = context.Current.ToString();

            // does it exist?
            if (!context.Data.ContainsKey(key))
                context.Data[key] = new List<object>();

            var list = context.Data[key] as IList<object>;

            if (list == null)
                throw new NotSupportedException("Target must be a list to add to");

            var sb = new StringBuilder();
            var newContext = new Context(context.Data, s => sb.Append(s));
            Content.Run(newContext);
            list.Add(sb.ToString());
            context.Current = null;
        }
Example #2
0
        public override bool Test(Context context)
        {
            if (context.Current is bool)
                return ((bool)context.Current);

            context.Error("Current passed to True wasn't boolean");
            context.Current = null;
            return false;
        }
Example #3
0
        public override void Run(Context context)
        {
            if (Params != null && Params.Count != 0)
            {
                context.Error("Params given to constant");
                context.Current = null;
                return;
            }

            context.Current = Value;
        }
Example #4
0
        public override bool Test(Context context)
        {
            var type = context.Current.GetType();

            if (type.IsImplementationOf(typeof(IEnumerable<>)))
                return ReflectUtil.GetListCount(context.Current) == 0;

            context.Error("Current passed to Empty wasn't Enumerable");
            context.Current = null;
            return false;
        }
Example #5
0
        public override void Run(Context context)
        {
            if (Params == null)
                return;

            foreach (var action in Params)
            {
                context.Current = context.Data;
                action.Run(context);
            }
            context.Current = context.Data;
        }
Example #6
0
        public override void Run(Context context)
        {
            if (Params == null)
                return;

            foreach (var action in Params)
            {
                // bail out if finished early
                if (context.Current == null)
                    return;
                action.Run(context);
            }
        }
Example #7
0
        public override void Run(Context context)
        {
            var type = context.Current.GetType();

            if (!type.IsImplementationOf(typeof(IEnumerable<>)))
            {
                context.Error("Current passed to First wasn't Enumerable");
                context.Current = null;
                return;
            }

            context.Current = ReflectUtil.GetListLast(context.Current);
        }
Example #8
0
        public override void Run(Context context)
        {
            if (Content == null)
            {
                context.Error("Content required for Define");
                context.Current = null;
                return;
            }

            var key = context.Current.ToString();

            context.Data[key] = Content;
            context.Current = null;
        }
Example #9
0
        public override bool Test(Context context)
        {
            if (Params.Count != 1)
            {
                context.Error("Equal requires a single parameter");
                context.Current = null;
                return false;
            }

            var newContext = new Context(context);
            Params.First().Run(newContext);

            return context.Current.Equals(newContext.Current);
        }
Example #10
0
        public override void Run(Context context)
        {
            if (Content == null)
            {
                context.Error("Content required for Assign");
                context.Current = null;
                return;
            }

            var key = context.Current.ToString();

            var sb = new StringBuilder();
            var newContext = new Context(context.Data, s => sb.Append(s));
            Content.Run(newContext);
            context.Data[key] = sb.ToString();
            context.Current = null;
        }
Example #11
0
        public override void Run(Context context)
        {
            var areEqual = Test(context);

            if (context.Current == null)
                return;

            if (areEqual)
            {
                if (Content != null)
                    Content.Run(context);
            }
            else
            {
                if (AltContent != null)
                    AltContent.Run(context);
            }
            context.Current = null;
        }
Example #12
0
        public override bool Test(Context context)
        {
            if (Params.Count != 1)
            {
                context.Error("StartsWith requires a single parameter");
                context.Current = null;
                return false;
            }

            var newContext = new Context(context);
            Params.First().Run(newContext);

            if (context.Current is string && newContext.Current is string)
                return (context.Current as string).StartsWith(newContext.Current as string);

            context.Error("StartsWith compares two strings");
            context.Current = null;
            return false;
        }
Example #13
0
        public override void Run(Context context)
        {
            if (Params.Count != 1)
            {
                context.Error("Each requires a single parameter");
                context.Current = null;
                return;
            }
            if (Content == null)
            {
                context.Error("Content required for Each");
                context.Current = null;
                return;
            }

            // key is optional
            var keyContext = new Context(context);
            Params.First().Run(keyContext);
            var key = keyContext.Current.ToString();

            //todo: how to get IEnumerable where we don't care what it contains?
            if (context.Current is IEnumerable)
            {
                var list = (context.Current as IEnumerable);
                var index = 0;
                foreach (var value in list)
                {
                    var newContext = new Context(context);
                    newContext.Data[key] = value;
                    newContext.Data[key + "_index"] = index;
                    Content.Run(newContext);
                    index++;
                }

                context.Current = null;
                return;
            }

            context.Error("Unable to enumerate " + context.Current.ToString());
            context.Current = null;
        }
Example #14
0
        public override bool Test(Context context)
        {
            if (Params.Count != 1)
            {
                context.Error("Equal requires a single parameter");
                context.Current = null;
                return false;
            }

            var newContext = new Context(context);
            Params.First().Run(newContext);

            var type = context.Current.GetType();

            if (type.IsImplementationOf(typeof(IEnumerable<>)))
                return ReflectUtil.ListContains(context.Current, newContext.Current);

            context.Error("Current passed to Contains wasn't Enumerable");
            context.Current = null;
            return false;
        }
Example #15
0
        public override void Run(Context context)
        {
            if (context.Current is IAction)
            {
                var action = context.Current as IAction;
                var newContext = new Context(context);
                action.Run(newContext);
                context.Current = null;
            }

            if (context.Current is string)
            {
                //todo: boot up another parser and execute this string?
                context.Error("Executing arbitary string not supported");
                context.Current = null;
                return;
            }

            context.Error("Execute doesn't understand " + context.Current.ToString());
            context.Current = null;
        }
Example #16
0
        public override bool Test(Context context)
        {
            if (Params.Count != 1)
            {
                context.Error("GreaterThanOrEqual requires a single parameter");
                context.Current = null;
                return false;
            }

            var newContext = new Context(context);
            Params.First().Run(newContext);

            var decimal1 = context.Current.ToString().ToDecimal();
            var decimal2 = newContext.Current.ToString().ToDecimal();

            if (decimal1.HasValue && decimal2.HasValue)
                return decimal1.Value >= decimal2.Value;

            context.Error("GreaterThanOrEqual compares two numbers");
            context.Current = null;
            return false;
        }
Example #17
0
 public abstract bool Test(Context context);
Example #18
0
 public IDictionary<string, object> Run(IDictionary<string, object> data)
 {
     var context = new Context(data);
     Action.Run(context);
     return context.Data;
 }
Example #19
0
 public string Render(IDictionary<string, object> data)
 {
     var context = new Context(data);
     Action.Run(context);
     return context.Data["Content"].ToString();
 }
Example #20
0
 public string Render()
 {
     var context = new Context();
     Action.Run(context);
     return context.Data["Content"].ToString();
 }
Example #21
0
File: Get.cs Project: tcoats/Voodoo
        public override void Run(Context context)
        {
            if (Params.Count != 1)
            {
                context.Error("Get requires a single parameter");
                context.Current = null;
                return;
            }

            var newContext = new Context(context);

            Params.First().Run(newContext);

            var value = newContext.Current;
            if (value == null)
            {
                context.Error("Param executed by Get ended up null " + Params.First());
                context.Current = null;
                return;
            }

            var type = context.Current.GetType();

            if (type.IsImplementationOf(typeof(IDictionary<,>)))
            {
                try
                {
                    context.Current = ReflectUtil.GetDictionaryValue(context.Current, value);
                    return;
                }
                catch (Exception e)
                {
                    context.Error(string.Format("Get could not index {0} using {1}. Exception: {2}",
                        context.Current,
                        value,
                        e.Message));
                    context.Current = null;
                    return;
                }
            }

            if (type.IsImplementationOf(typeof(IEnumerable<>)))
            {
                try
                {
                    context.Current = ReflectUtil.GetListIndex(context.Current, value.ToString().ToInt().Value);
                    return;
                }
                catch (Exception)
                {
                    // continue, perhaps we are really looking for a property or method
                }
            }

            var propertyInfo = type.GetProperty(value.ToString());
            if (propertyInfo != null)
            {
                try
                {
                    context.Current = propertyInfo.GetValue(context.Current, null);
                    return;
                }
                catch (Exception e)
                {
                    context.Error(string.Format("Get could not access {0} with property {1}. Exception {2}",
                        context.Current,
                        propertyInfo.Name,
                        e.Message));
                    context.Current = null;
                    return;
                }
            }

            var fieldInfo = type.GetField(value.ToString());
            if (fieldInfo != null)
            {
                try
                {
                    context.Current = fieldInfo.GetValue(context.Current);
                    return;
                }
                catch (Exception e)
                {
                    context.Error(string.Format("Get could not access {0} with field {1}. Exception {2}",
                        context.Current,
                        fieldInfo.Name,
                        e.Message));
                    context.Current = null;
                    return;
                }
            }

            var methodInfo = type.GetMethod(
                    value.ToString(),
                    new Type[] {});
            if (methodInfo != null)
            {
                try
                {
                    context.Current = methodInfo.Invoke(context.Current, null);
                    return;
                }
                catch (Exception e)
                {
                    context.Error(string.Format("Get could not access {0} with method {1}. Exception {2}",
                        context.Current,
                        methodInfo.Name,
                        e.Message));
                    context.Current = null;
                    return;
                }
            }

            context.Error(string.Format("Could not 'Get' {0} from {1}",
                value,
                context.Current));
            context.Current = null;
        }
Example #22
0
        public void Test()
        {
            var actions = Actions;

            var data = new Dictionary<string, object>()
            {
                {"posts", Get()},
                {"configuration", new Dictionary<string, string>()
                    {
                        {"title", "A joint blog"},
                        {"year", "2010"}
                    }
                }
            };

            var context = new Context(data);
            actions.Run(context);
        }
Example #23
0
 public Context(Context context)
     : this(context.Data, context.Render, context.Trace, context.Error)
 {
 }
Example #24
0
 public override void Run(Context context)
 {
     context.Current = context.Current.ToString().ToUpper();
 }
Example #25
0
 public override void Run(Context context)
 {
     throw new NotImplementedException();
 }
Example #26
0
 public override void Run(Context context)
 {
     var result = context.Current.ToString();
     context.Render(result);
     context.Current = null;
 }
Example #27
0
 public abstract void Run(Context context);
Example #28
0
 public override bool Test(Context context)
 {
     return !base.Test(context);
 }
Example #29
0
 public override void Run(Context context)
 {
     context.Current = context.Current.ToString().ToJavascriptString();
 }