Beispiel #1
0
        /// <summary>
        /// Fetches an object starting at the local scope and then moving up
        /// the hierarchy
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private object FindVariable(string key)
        {
            Hash   scope    = Scopes.FirstOrDefault(s => s.ContainsKey(key));
            object variable = null;

            if (scope == null)
            {
                foreach (Hash e in Environments)
                {
                    if ((variable = LookupAndEvaluate(e, key)) != null)
                    {
                        scope = e;
                        break;
                    }
                }
            }
            scope    = scope ?? Environments.LastOrDefault() ?? Scopes.Last();
            variable = variable ?? LookupAndEvaluate(scope, key);

            variable = Liquidize(variable);
            if (variable is IContextAware)
            {
                ((IContextAware)variable).Context = this;
            }
            return(variable);
        }
Beispiel #2
0
        private void SquashInstanceAssignsWithEnvironments()
        {
            Dictionary <string, object> tempAssigns = new Dictionary <string, object>(Template.NamingConvention.StringComparer);

            Hash lastScope = Scopes.Last();

            foreach (string k in lastScope.Keys)
            {
                foreach (Hash env in Environments)
                {
                    if (env.ContainsKey(k))
                    {
                        tempAssigns[k] = LookupAndEvaluate(env, k);
                        break;
                    }
                }
            }

            foreach (string k in tempAssigns.Keys)
            {
                lastScope[k] = tempAssigns[k];
            }
        }
Beispiel #3
0
        /// <summary>
        /// Fetches an object starting at the local scope and then moving up
        /// the hierarchy
        /// </summary>
        /// <param name="key"></param>
        /// <param name="variable"></param>
        /// <returns></returns>
        private bool TryFindVariable(string key, out object variable)
        {
            bool   foundVariable = false;
            object foundValue    = null;
            Hash   scope         = Scopes.FirstOrDefault(s => s.ContainsKey(key));

            if (scope == null)
            {
                foreach (Hash environment in Environments)
                {
                    foundVariable = TryEvaluateHashOrArrayLikeObject(environment, key, out foundValue);
                    if (foundVariable)
                    {
                        scope = environment;
                        break;
                    }
                }

                if (scope == null)
                {
                    scope         = Environments.LastOrDefault() ?? Scopes.Last();
                    foundVariable = TryEvaluateHashOrArrayLikeObject(scope, key, out foundValue);
                }
            }
            else
            {
                foundVariable = TryEvaluateHashOrArrayLikeObject(scope, key, out foundValue);
            }

            variable = Liquidize(foundValue);
            if (variable is IContextAware contextAwareVariable)
            {
                contextAwareVariable.Context = this;
            }
            return(foundVariable);
        }
Beispiel #4
0
 public void Set(TK key, TV value, bool local) => (local ? scopes.Peek() : scopes.Last())[key] = value;