More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / IgnoreNull.cs
blobb4d6168219d26ab77aa7ba5912ac9ca51c5bac40
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Views.Brail
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
21 using Boo.Lang;
23 public class IgnoreNull : IQuackFu
25 private readonly object target;
27 public IgnoreNull(object target)
29 this.target = target;
32 #region IQuackFu Members
34 public object QuackGet(string name, object[] parameters)
36 if (name == "_IsIgnoreNullReferencingNotNullObject_")
38 return target != null;
41 if (target == null)
43 return this;
45 object value;
46 if (IsNullOrEmpty(parameters))
48 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetProperty(target, name);
50 else
52 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetSlice(target, name, parameters);
54 return new IgnoreNull(value);
57 public object QuackSet(string name, object[] parameters, object obj)
59 if (target == null)
61 return this;
63 if (IsNullOrEmpty(parameters))
65 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetProperty(target, name, obj);
67 else
69 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetSlice(target, name,
70 GetParameterArray(parameters, obj));
72 return this;
75 public object QuackInvoke(string name, object[] args)
77 if (target == null)
79 return this;
81 object value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.Invoke(target, name, args);
82 return new IgnoreNull(value);
85 #endregion
87 private static bool IsNullOrEmpty(object[] parameters)
89 return parameters == null || parameters.Length == 0;
92 private static object[] GetParameterArray(object[] parameters, object obj)
94 List<object> args = new List<object>(parameters);
95 args.Add(obj);
96 return args.ToArray();
99 public override string ToString()
101 if (target == null)
103 return string.Empty;
105 return target.ToString();
108 public static bool AreEqual(object left, object right)
110 IgnoreNull temp = left as IgnoreNull;
111 if (temp != null)
113 left = temp.target;
115 temp = right as IgnoreNull;
116 if (temp != null)
118 right = temp.target;
120 return Equals(left, right);
123 /// <summary>
124 /// Gets the underlying target object of the IgnorNull
125 /// </summary>
126 /// <param name="ignored"></param>
127 /// <returns>target</returns>
128 public static Object ExtractTarget(IgnoreNull ignored)
130 return ignored.target;
133 /// <summary>
134 /// Returns a new array copied from args that has replaced IgnoreNull instances
135 /// with the underlying target object.
136 /// </summary>
137 /// <param name="args">source array</param>
138 /// <returns>new array</returns>
139 public static Object[] ReplaceIgnoreNullsWithTargets(Object[] args)
141 Object[] replaced = new object[args.Length];
142 for (int i = 0; i < args.Length; i++)
144 if (args[i] is IgnoreNull)
146 replaced[i] = ExtractTarget((IgnoreNull) args[i]);
148 else
150 replaced[i] = args[i];
153 return replaced;
156 /// <summary>
157 /// Returns a new Hashtable that has copied the entries from dict
158 /// and replaced values of IgnoreNull with the underlying target object.
159 /// </summary>
160 /// <param name="dict"></param>
161 /// <returns>Hashtable</returns>
162 public static IDictionary ReplaceIgnoreNullsWithTargets(IDictionary dict)
164 Hashtable hash = new Hashtable(dict.Count);
166 foreach (DictionaryEntry de in dict)
168 if (de.Value is IgnoreNull)
170 hash[de.Key] = ExtractTarget((IgnoreNull) de.Value);
172 else
174 hash[de.Key] = de.Value;
178 return hash;