public static Container WithReadModelConvenienceFunctions(this FunctionBody functionBody, Type type) { var excludePropertiesFrom = typeof(IReadModel); var properties = type.GetProperties(); if (excludePropertiesFrom != null) properties = properties.Where(p => !excludePropertiesFrom.GetProperties().Select(pi => pi.Name).Contains(p.Name)).ToArray(); foreach (var property in properties) { var functionName = string.Format("matching{0}",property.Name.ToPascalCase()); var propertyName = property.Name.ToCamelCase(); var filter = new ObjectLiteral(); filter.Assign(propertyName).WithLiteral(propertyName); functionBody.Property(functionName,p => p.WithFunction(function => function .WithParameters(propertyName) .Body .Scope("self", scope=> scope.FunctionCall(f=>f.WithName("instanceMatching").WithParameters(new[] { filter }) ) ) ) ); } return functionBody; }
/// <summary> /// Assign a key within an <see cref="ObjectLiteral"/> /// </summary> /// <param name="objectLiteral"><see cref="ObjectLiteral"/> to assign to</param> /// <param name="name">Name of key</param> /// <returns><see cref="KeyAssignment"/> to build</returns> public static KeyAssignment Assign(this ObjectLiteral objectLiteral, string name) { var keyAssignment = new KeyAssignment(name); objectLiteral.AddChild(keyAssignment); return(keyAssignment); }
/// <summary> /// Assign an object literal /// </summary> /// <param name="assignment"><see cref="Assignment"/> to assign to</param> /// <param name="callback"><see cref="Action{ObjectLiteral}"/> that gets called to build the object literal</param> /// <returns>The <see cref="Assignment"/> to build on</returns> public static Assignment WithObjectLiteral(this Assignment assignment, Action <ObjectLiteral> callback = null) { var objectLiteral = new ObjectLiteral(); if (callback != null) { callback(objectLiteral); } assignment.Value = objectLiteral; return(assignment); }
static void AddObservablePropertiesFromType(Container parent, IEnumerable <PropertyInfo> properties, Action <Assignment> assignmentVisitor, ObservableVisitor observableVisitor) { foreach (var property in properties) { var propertyName = property.Name.ToCamelCase(); Assignment assignment; if (parent is FunctionBody) { assignment = new PropertyAssignment(propertyName); } else { assignment = new KeyAssignment(propertyName); } if (property.IsDictionary()) { assignment.WithObjectLiteral(); } else if (property.IsEnumerable()) { assignment.WithObservableArray(); } else if (property.IsObservable()) { assignment.WithObservable(observableVisitor); } else { var objectLiteral = new ObjectLiteral(); assignment.Value = objectLiteral; AddObservablePropertiesFromType(objectLiteral, property.PropertyType.GetProperties(), assignmentVisitor, observableVisitor); } if (assignmentVisitor != null) { assignmentVisitor(assignment); } parent.AddChild(assignment); } }
public static Container WithServiceMethodsFrom(this FunctionBody functionBody, Type type) { var objectMethods = typeof(object).GetMethods(); var methods = type.GetMethods(BindingFlags.Public|BindingFlags.Instance).Where(m => objectMethods.Any(om=>om.DeclaringType != m.DeclaringType)); foreach (var method in methods) { var functionName = method.Name.ToCamelCase(); var selfScopeCall = new Scope("self"); var parameters = method.GetParameters().Select(p => p.Name.ToCamelCase()).ToArray(); var objectLiteral = new ObjectLiteral(); foreach (var parameter in parameters) objectLiteral.Assign(parameter).WithLiteral(parameter); if (method.ReturnType == typeof(void)) selfScopeCall.FunctionCall(f => f.WithName("callWithoutReturnValue").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral)); else if (method.ReturnType.IsDictionary() || !method.ReturnType.IsEnumerable()) selfScopeCall.FunctionCall(f => f.WithName("callWithObjectAsReturn").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral)); else if (method.ReturnType.IsEnumerable()) selfScopeCall.FunctionCall(f => f.WithName("callWithArrayAsReturn").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral)); functionBody.Property(functionName, p => { p.WithFunction(function => function .WithParameters(parameters) .Body .Return(selfScopeCall) ); }); } return functionBody; }
/// <summary> /// Assign an object literal /// </summary> /// <param name="assignment"><see cref="Assignment"/> to assign to</param> /// <param name="callback"><see cref="Action{ObjectLiteral}"/> that gets called to build the object literal</param> /// <returns>The <see cref="Assignment"/> to build on</returns> public static Assignment WithObjectLiteral(this Assignment assignment, Action<ObjectLiteral> callback = null) { var objectLiteral = new ObjectLiteral(); if (callback != null) callback(objectLiteral); assignment.Value = objectLiteral; return assignment; }
/// <summary> /// Initializes a new instance of <see cref="Namespace"/> /// </summary> /// <param name="name">Name of the namespace</param> public Namespace(string name) { Name = name; Content = new ObjectLiteral(); Content.Parent = this; }
static void AddPropertiesFromType(Container parent, IEnumerable <PropertyInfo> properties, Action <Assignment> assignmentVisitor) { foreach (var property in properties) { var propertyName = property.Name.ToCamelCase(); Assignment assignment; if (parent is FunctionBody) { assignment = new PropertyAssignment(propertyName); } else { assignment = new KeyAssignment(propertyName); } if (property.IsDictionary()) { assignment.WithObjectLiteral(); } else if (property.IsEnumerable()) { assignment.WithEmptyArray(); } else if (property.PropertyType.IsConcept()) { assignment.WithDefaultValue(property.PropertyType.GetConceptValueType()); } else if (property.PropertyType.IsNullable()) { assignment.WithNullValue(); } else if (property.IsDateTime()) { assignment.WithDate(); } else if (property.IsBoolean()) { assignment.WithBoolean(); } else if (property.IsEnum()) { assignment.WithDefaultEnumValue(property.PropertyType); } else if (property.PropertyType.IsNumericType()) { assignment.WithDefaultNumericValue(property.PropertyType); } else if (property.HasPrimitiveDefaultValue()) { assignment.WithDefaultValue(property.PropertyType); } else { var objectLiteral = new ObjectLiteral(); assignment.Value = objectLiteral; AddPropertiesFromType(objectLiteral, property.PropertyType.GetProperties(), assignmentVisitor); } if (assignmentVisitor != null) { assignmentVisitor(assignment); } parent.AddChild(assignment); } }
static void AddPropertiesFromType(Container parent, IEnumerable<PropertyInfo> properties, Action<Assignment> assignmentVisitor) { foreach (var property in properties) { var propertyName = property.Name.ToCamelCase(); Assignment assignment; if (parent is FunctionBody) assignment = new PropertyAssignment(propertyName); else assignment = new KeyAssignment(propertyName); if (property.IsDictionary()) assignment.WithObjectLiteral(); else if (property.IsEnumerable()) assignment.WithEmptyArray(); else if (property.PropertyType.IsConcept()) assignment.WithDefaultValue(property.PropertyType.GetConceptValueType()); else if (property.PropertyType.IsNullable()) assignment.WithNullValue(); else if (property.IsDateTime()) assignment.WithDate(); else if (property.IsBoolean()) assignment.WithBoolean(); else if (property.IsEnum()) assignment.WithDefaultEnumValue(property.PropertyType); else if (property.PropertyType.IsNumericType()) assignment.WithDefaultNumericValue(property.PropertyType); else if (property.HasPrimitiveDefaultValue()) assignment.WithDefaultValue(property.PropertyType); else { var objectLiteral = new ObjectLiteral(); assignment.Value = objectLiteral; AddPropertiesFromType(objectLiteral, property.PropertyType.GetProperties(), assignmentVisitor); } if (assignmentVisitor != null) assignmentVisitor(assignment); parent.AddChild(assignment); } }
static void AddObservablePropertiesFromType(Container parent, IEnumerable<PropertyInfo> properties, Action<Assignment> assignmentVisitor, ObservableVisitor observableVisitor) { foreach (var property in properties) { var propertyName = property.Name.ToCamelCase(); Assignment assignment; if (parent is FunctionBody) assignment = new PropertyAssignment(propertyName); else assignment = new KeyAssignment(propertyName); if (property.IsDictionary()) assignment.WithObjectLiteral(); else if (property.IsEnumerable()) assignment.WithObservableArray(); else if (property.IsObservable()) assignment.WithObservable(observableVisitor); else { var objectLiteral = new ObjectLiteral(); assignment.Value = objectLiteral; AddObservablePropertiesFromType(objectLiteral, property.PropertyType.GetProperties(), assignmentVisitor, observableVisitor); } if (assignmentVisitor != null) assignmentVisitor(assignment); parent.AddChild(assignment); } }