Exemple #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);
        }
Exemple #2
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);
        }