Exemple #1
0
        /// <summary>
        /// Attach default methods to the delegates. If there isn't specified concrete methods the default
        /// are executed. There is no difference in the parameter order.
        /// </summary>
        /// <param name="delegates">
        /// A <see cref="System.Delegate"/>
        /// Methods to handle the cache system functionality.
        /// </param>
        public CacheManager(params Delegate[] delegates)
        {
            this.storage = new Dictionary <TKey, CacheItem>();

            foreach (Delegate d in delegates)
            {
                if (d is InitDelegate)
                {
                    this.initDelegate = (InitDelegate)d;
                }
                else if (d is TouchDelegate)
                {
                    this.touchDelegate = (TouchDelegate)d;
                }
                else if (d is ValidateDelegate)
                {
                    this.validateDelegate = (ValidateDelegate)d;
                }
                else if (d is CalculateDelegate)
                {
                    this.calculateDelegate = (CalculateDelegate)d;
                }
                else if (d is UpdateDelegate)
                {
                    this.updateDelegate = (UpdateDelegate)d;
                }
                else if (d is DeleteDelegate)
                {
                    this.deleteDelegate = (DeleteDelegate)d;
                }
            }

            if (this.initDelegate == null)
            {
                this.initDelegate = DefaultInitDelegate;
            }
            if (this.touchDelegate == null)
            {
                this.touchDelegate = DefaultTouchDelegate;
            }
            if (this.validateDelegate == null)
            {
                this.validateDelegate = DefaultValidateDelegate;
            }
            if (this.calculateDelegate == null)
            {
                this.calculateDelegate = DefaultCalculateDelegate;
            }
            if (this.updateDelegate == null)
            {
                this.updateDelegate = DefaultUpdateDelegate;
            }
            if (this.deleteDelegate == null)
            {
                this.deleteDelegate = DefaultDeleteDelegate;
            }
        }
Exemple #2
0
            private T ValidateItem <T>(GameObject go, ValidateDelegate <T> validate)
            {
                T inst = validate();

                if (inst == null)
                {
                    RebuildItem(go);
                    inst = validate();
                }

                return(inst);
            }
        private void Validate(Control target, Label label, ValidateDelegate validator)
        {
            bool prevValid = IsDataValid(target);
            bool isEmpty;
            bool isValid = validator(out isEmpty);

            target.Tag = isValid;           // save 'valid' state in the tag field

            target.ForeColor             = !isValid && !isEmpty ? Color.Red : Color.Black;
            label.ForeColor              = !isValid ? Color.Red : Color.Black;
            InvalidControls             += (prevValid == isValid) ? 0 : (!isValid ? 1 : -1);
            InvalidFieldsMessage.Visible = (InvalidControls > 0);
        }
        View InitializeCouchbaseView()
        {
            var view = Database.GetView(DefaultViewName);

            var mapBlock = new MapDelegate((doc, emit) =>
            {
                object date;
                doc.TryGetValue(CreationDatePropertyName, out date);

                object deleted;
                doc.TryGetValue(DeletedKey, out deleted);

                if (date != null && deleted == null)
                {
                    emit(date, doc);
                }
            });

            view.SetMap(mapBlock, "1.1");

            var validationBlock = new ValidateDelegate((revision, context) =>
            {
                if (revision.IsDeletion)
                {
                    return(true);
                }

                object date;
                revision.Properties.TryGetValue(CreationDatePropertyName, out date);
                return(date != null);
            });

            Database.SetValidation(CreationDatePropertyName, validationBlock);

            return(view);
        }
        /// <summary>
        /// Sets the validation delegate for the given name. If delegate is null, 
        /// the validation with the given name is deleted. Before any change 
        /// to the <see cref="Couchbase.Lite.Database"/> is committed, including incoming changes from a pull 
        /// <see cref="Couchbase.Lite.Replication"/>, all of its validation delegates are called and given 
        /// a chance to reject it.
        /// </summary>
        /// <param name="name">The name of the validation delegate to set.</param>
        /// <param name="validationDelegate">The validation delegate to set.</param>
        public void SetValidation(string name, ValidateDelegate validationDelegate)
        {
            if (!IsOpen) {
                Log.W(TAG, "SetValidation called on closed database");
                return;
            }

            Shared.SetValue("validation", name, Name, validationDelegate);
        }
Exemple #6
0
 public Rule(ValidateDelegate valDel, ParseDelegate parseDel)
 {
     Validate = valDel;
     Parse    = parseDel;
 }
    View InitializeCouchbaseView ()
    {
        var view = Database.GetView (DefaultViewName);

        var mapBlock = new MapDelegate ((doc, emit) => 
        {
            object date;
            doc.TryGetValue (CreationDatePropertyName, out date);

            object deleted;
            doc.TryGetValue (DeletedKey, out deleted);

            if (date != null && deleted == null)
                emit (date, doc);
        });

        view.SetMap (mapBlock, "1.1");

        var validationBlock = new ValidateDelegate ((revision, context) =>
                {
                    if (revision.IsDeletion)
                        return true;

                    object date;
                    revision.Properties.TryGetValue (CreationDatePropertyName, out date);
                    return (date != null);
                });

        Database.SetValidation(CreationDatePropertyName, validationBlock);

        return view;
    }
        public void TestValidations()
        {
            ValidateDelegate validator = (newRevision, context) =>
            {
                Assert.IsNotNull(newRevision);
                Assert.IsNotNull(context);
                Assert.IsTrue(newRevision.Properties != null || newRevision.IsDeletion);

                validationCalled = true;

                bool hoopy = newRevision.IsDeletion || (newRevision.Properties.Get("towel") != null);
                Log.V(ValidationsTest.Tag, string.Format("--- Validating {0} --> {1}", newRevision.Properties, hoopy));
                if (!hoopy)
                {
                    context.Reject("Where's your towel?");
                }
                return(hoopy);
            };

            database.SetValidation("hoopy", validator);

            // POST a valid new document:
            IDictionary <string, object> props = new Dictionary <string, object>();

            props["name"]  = "Zaphod Beeblebrox";
            props["towel"] = "velvet";
            RevisionInternal rev    = new RevisionInternal(props, database);
            Status           status = new Status();

            validationCalled = false;
            rev = database.PutRevision(rev, null, false, status);
            Assert.IsTrue(validationCalled);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            // PUT a valid update:
            props["head_count"] = 3;
            rev.SetProperties(props);
            validationCalled = false;
            rev = database.PutRevision(rev, rev.GetRevId(), false, status);
            Assert.IsTrue(validationCalled);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            // PUT an invalid update:
            Sharpen.Collections.Remove(props, "towel");
            rev.SetProperties(props);
            validationCalled = false;
            bool gotExpectedError = false;

            try
            {
                rev = database.PutRevision(rev, rev.GetRevId(), false, status);
            }
            catch (CouchbaseLiteException e)
            {
                gotExpectedError = (e.GetCBLStatus().GetCode() == StatusCode.Forbidden);
            }
            Assert.IsTrue(validationCalled);
            Assert.IsTrue(gotExpectedError);

            // POST an invalid new document:
            props            = new Dictionary <string, object>();
            props["name"]    = "Vogon";
            props["poetry"]  = true;
            rev              = new RevisionInternal(props, database);
            validationCalled = false;
            gotExpectedError = false;
            try
            {
                rev = database.PutRevision(rev, null, false, status);
            }
            catch (CouchbaseLiteException e)
            {
                gotExpectedError = (e.GetCBLStatus().GetCode() == StatusCode.Forbidden);
            }
            Assert.IsTrue(validationCalled);
            Assert.IsTrue(gotExpectedError);

            // PUT a valid new document with an ID:
            props            = new Dictionary <string, object>();
            props["_id"]     = "ford";
            props["name"]    = "Ford Prefect";
            props["towel"]   = "terrycloth";
            rev              = new RevisionInternal(props, database);
            validationCalled = false;
            rev              = database.PutRevision(rev, null, false, status);
            Assert.IsTrue(validationCalled);
            Assert.AreEqual("ford", rev.GetDocId());

            // DELETE a document:
            rev = new RevisionInternal(rev.GetDocId(), rev.GetRevId(), true, database);
            Assert.IsTrue(rev.IsDeleted());
            validationCalled = false;
            rev = database.PutRevision(rev, rev.GetRevId(), false, status);
            Assert.IsTrue(validationCalled);

            // PUT an invalid new document:
            props            = new Dictionary <string, object>();
            props["_id"]     = "petunias";
            props["name"]    = "Pot of Petunias";
            rev              = new RevisionInternal(props, database);
            validationCalled = false;
            gotExpectedError = false;
            try
            {
                rev = database.PutRevision(rev, null, false, status);
            }
            catch (CouchbaseLiteException e)
            {
                gotExpectedError = (e.GetCBLStatus().GetCode() == StatusCode.Forbidden);
            }
            Assert.IsTrue(validationCalled);
            Assert.IsTrue(gotExpectedError);
        }
Exemple #9
0
 /// <summary>
 /// Sets the validation delegate for the given name. If delegate is null,
 /// the validation with the given name is deleted. Before any change
 /// to the <see cref="Couchbase.Lite.Database"/> is committed, including incoming changes from a pull
 /// <see cref="Couchbase.Lite.Replication"/>, all of its validation delegates are called and given
 /// a chance to reject it.
 /// </summary>
 /// <param name="name">The name of the validation delegate to set.</param>
 /// <param name="validationDelegate">The validation delegate to set.</param>
 public void SetValidation(string name, ValidateDelegate validationDelegate)
 {
     Base.SetValidation(name, validationDelegate);
 }
        /// <summary>
        /// Sets the validation delegate for the given name. If delegate is null, 
        /// the validation with the given name is deleted. Before any change 
        /// to the <see cref="Couchbase.Lite.Database"/> is committed, including incoming changes from a pull 
        /// <see cref="Couchbase.Lite.Replication"/>, all of its validation delegates are called and given 
        /// a chance to reject it.
        /// </summary>
        /// <param name="name">The name of the validation delegate to set.</param>
        /// <param name="validationDelegate">The validation delegate to set.</param>
        public void SetValidation(String name, ValidateDelegate validationDelegate)
        {
            if (_validations == null)
                _validations = new Dictionary<string, ValidateDelegate>();

            if (validationDelegate != null)
                _validations[name] = validationDelegate;
            else
                _validations.Remove(name);
        }
Exemple #11
0
        public void TestValidations()
        {
            ValidateDelegate validator = (newRevision, context) =>
            {
                Assert.IsNotNull(newRevision);
                Assert.IsNotNull(context);
                Assert.IsTrue(newRevision.Properties != null || newRevision.IsDeletion);

                validationCalled = true;

                bool hoopy = newRevision.IsDeletion || (newRevision.Properties.Get("towel") != null);
                Console.WriteLine("--- Validating {0} --> {1}", newRevision.Properties, hoopy);
                if (!hoopy)
                {
                    context.Reject("Where's your towel?");
                }

                return(hoopy);
            };

            database.SetValidation("hoopy", validator);

            // POST a valid new document:
            IDictionary <string, object> props = new Dictionary <string, object>();

            props["name"]  = "Zaphod Beeblebrox";
            props["towel"] = "velvet";
            RevisionInternal rev = new RevisionInternal(props);

            validationCalled = false;
            rev = database.PutRevision(rev, null, false);
            Assert.IsTrue(validationCalled);

            // PUT a valid update:
            props["head_count"] = 3;
            rev.SetProperties(props);
            validationCalled = false;
            rev = database.PutRevision(rev, rev.RevID, false);
            Assert.IsTrue(validationCalled);

            // PUT an invalid update:
            props.Remove("towel");
            rev.SetProperties(props);
            validationCalled = false;
            Assert.Throws <CouchbaseLiteException>(() => rev = database.PutRevision(rev, rev.RevID, false));
            Assert.IsTrue(validationCalled);

            // POST an invalid new document:
            props            = new Dictionary <string, object>();
            props["name"]    = "Vogon";
            props["poetry"]  = true;
            rev              = new RevisionInternal(props);
            validationCalled = false;
            Assert.Throws <CouchbaseLiteException>(() => database.PutRevision(rev, null, false));
            Assert.IsTrue(validationCalled);

            // PUT a valid new document with an ID:
            props            = new Dictionary <string, object>();
            props["_id"]     = "ford";
            props["name"]    = "Ford Prefect";
            props["towel"]   = "terrycloth";
            rev              = new RevisionInternal(props);
            validationCalled = false;
            rev              = database.PutRevision(rev, null, false);
            Assert.IsTrue(validationCalled);
            Assert.AreEqual("ford", rev.DocID);

            // DELETE a document:
            rev = new RevisionInternal(rev.DocID, rev.RevID, true);
            Assert.IsTrue(rev.Deleted);
            validationCalled = false;
            rev = database.PutRevision(rev, rev.RevID, false);
            Assert.IsTrue(validationCalled);

            // PUT an invalid new document:
            props            = new Dictionary <string, object>();
            props["_id"]     = "petunias";
            props["name"]    = "Pot of Petunias";
            rev              = new RevisionInternal(props);
            validationCalled = false;
            Assert.Throws <CouchbaseLiteException>(() => rev = database.PutRevision(rev, null, false));
            Assert.IsTrue(validationCalled);

            // Cancel the validation
            database.SetValidation("hoopy", null);
            validationCalled = false;
            Assert.DoesNotThrow(() => rev = database.PutRevision(rev, null, false));
            Assert.IsFalse(validationCalled);
        }
Exemple #12
0
        public IFormBuilder <T> Field(string name, ConditionalDelegate <T> condition = null, ValidateDelegate <T> validate = null)
        {
            var field = (condition == null ? new FieldReflector <T>(name) : new Conditional <T>(name, condition));

            if (validate != null)
            {
                field.SetValidation(validate);
            }
            return(AddField(field));
        }
        private void Validate(Control target, Label label, ValidateDelegate validator)
        {
            bool prevValid = IsDataValid(target);
            bool isEmpty;
            bool isValid = validator(out isEmpty);

            target.Tag = isValid;           // save 'valid' state in the tag field

            target.ForeColor = !isValid && !isEmpty ? Color.Red : Color.Black;
            label.ForeColor = !isValid ? Color.Red : Color.Black;
            InvalidControls += (prevValid == isValid) ? 0 : (!isValid ? 1 : -1);
            InvalidFieldsMessage.Visible = (InvalidControls > 0);
        }
Exemple #14
0
 /// <summary>   Set the field validation. </summary>
 /// <param name="validate"> The validator. </param>
 /// <returns>   An IField&lt;T&gt; </returns>
 public IField <T> SetValidation(ValidateDelegate <T> validate)
 {
     UpdateAnnotations();
     _validate = validate;
     return(this);
 }
Exemple #15
0
 public Login(ValidateDelegate closeDelegate, UserDelegate _userDelegate)
 {
     InitializeComponent();
     userDelegate       = _userDelegate;
     this.closeDelegate = closeDelegate;
 }
Exemple #16
0
 internal TileDownloader(TileId tileId, Uri downloadingUri, Action <TileDownloader> assyncImageReader, ValidateDelegate validator)
     : this(tileId, assyncImageReader)
 {
     this.uri       = downloadingUri;
     this.validator = validator;
 }
Exemple #17
0
 public Rule(ValidateDelegate valDel, ParseDelegate parseDel)
 {
     this.Validate = valDel;
     this.Parse    = parseDel;
 }
 /// <summary>
 /// Sets the validation delegate for the given name. If delegate is null, 
 /// the validation with the given name is deleted. Before any change 
 /// to the <see cref="Couchbase.Lite.Database"/> is committed, including incoming changes from a pull 
 /// <see cref="Couchbase.Lite.Replication"/>, all of its validation delegates are called and given 
 /// a chance to reject it.
 /// </summary>
 /// <param name="name">The name of the validation delegate to set.</param>
 /// <param name="validationDelegate">The validation delegate to set.</param>
 public void SetValidation(String name, ValidateDelegate validationDelegate)
 {
     Shared.SetValue("validation", name, Name, validationDelegate);
 }
        /// <summary>
        /// Sets the validation delegate for the given name. If delegate is null, 
        /// the validation with the given name is deleted. Before any change 
        /// to the <see cref="Couchbase.Lite.Database"/> is committed, including incoming changes from a pull 
        /// <see cref="Couchbase.Lite.Replication"/>, all of its validation delegates are called and given 
        /// a chance to reject it.
        /// </summary>
        /// <param name="name">The name of the validation delegate to set.</param>
        /// <param name="validationDelegate">The validation delegate to set.</param>
        public void SetValidation(string name, ValidateDelegate validationDelegate)
        {
            if(!IsOpen) {
                Log.To.Database.W(TAG, "{0} SetValidation called on closed database, returning null...", this);
                return;
            }

            Shared.SetValue("validation", name, Name, validationDelegate);
        }