Created
October 7, 2009 07:09
-
-
Save davetheninja/203836 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using NinjaTools.Builders; | |
using NinjaTools.Helpers; | |
using NinjaTools.NHDynamicTable.Core.Model; | |
using NinjaTools.NHDynamicTable.Core.Model.Enums; | |
namespace NinjaTools.NHDynamicTable.Core.Builders | |
{ | |
public class DynamicTableBuilder : AbstractBuilder<DynamicTable, DynamicTableBuilder> | |
{ | |
protected override void SetDefaults() {} | |
protected override DynamicTable CreateItem() | |
{ | |
var table = new DynamicTable(); | |
table.SetDefinitions(GetValueFromCache<IList<DynamicColumnDefinition>>(x => x.ColumnDefinitions)); | |
table.SetRows(GetValueFromCache<IList<DynamicRow>>(x => x.Rows)); | |
table.SetName(GetValueFromCache<string>(x => x.Name)); | |
return table; | |
} | |
public DynamicTableBuilder WithDefinitions(IEnumerable<string> keys, Func<string, string> labelFormat) | |
{ | |
var definitions = new List<DynamicColumnDefinition>(); | |
foreach (var key in keys) | |
{ | |
definitions | |
.Add(new ColumnDefinitionBuilder() | |
.As(key) | |
.WithType(FieldType.TextBox) | |
.WithLabel(labelFormat.Invoke(key)) | |
.Build()); | |
} | |
AddValueToCache(x => x.ColumnDefinitions, definitions); | |
return this; | |
} | |
public DynamicTableBuilder WithRows(IEnumerable<DynamicRowBuilder> rowBuilders) | |
{ | |
var definitions = GetValueFromCache<IList<DynamicColumnDefinition>>(x => x.ColumnDefinitions); | |
if (definitions == null || definitions.Count == 0) | |
throw new Exception("You must have column definitions set before setting rows"); | |
var rows = new List<DynamicRow>(); | |
rowBuilders.Each(row => rows.Add(row.Build(definitions))); | |
AddValueToCache(x => x.Rows, rows); | |
return this; | |
} | |
public DynamicTableBuilder As(string tableName) | |
{ | |
AddValueToCache(x => x.Name, tableName); | |
return this; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment