Mila

Abstraction for model.

import 'package:mila/mila.dart';

class Person extends Model {
  @field String name;
  @field int age;
  @field String get hello => "hello my name is $name and I'm $age years old";
  @field List<Achievement> achievements;
}

class Achievement extends Model {
  @field String inCompetition;
  @field int rank;
  @field String description;
}

void main() {
  var person = new Person()
        ..name = 'foobar'
        ..age = 30,
      firstAchievement = new Achievement()
        ..inCompetition = 'fishing'
        ..rank = 3,
        ..description = 'my first trophy',
      secondAchievement = new Achievement()
        ..inCompetition = 'dota'
        ..rank = 1
        ..description = 'my second achievement',
      achievements = []..adAll([firstAchievement, secondAchievement]);

  person.achievements = achievements;

  var mapRepresentation = person.toMap();

  print(mapRepresentation);
  // {'name' : 'foobar',
      'age' : 30,
      'hello' : "hello my name is foobar and I'm 30 years old",
      'achievements' : [
        {'inCompetition' : 'fishing',
         'rank' : 3,
         'description' : 'my first throphy'},
        {'inCompetition' : 'dota',
         'rank' : 1,
         'description' : 'my second achievement'}
      ]}

  // useful if you want to write to db

A field is marked with

  @field

Only variable and getter can be marked.

With the same model we can also do

void main() {
  var factory = new ModelFactory(Achievement),
      achievementMap = {'inCompetition' : 'piano',
                        'rank' : 2,
                        'description' : 'I play clair de lune when I perform'},
      achievement = factory.create(achievementMap);

      print(achievement.inCompetition); // piano
      print(achievement.rank); // 2
      print(achievement.description); // I play clair de lune when I perform

  var personFactory = new ModelFactory(Person),
      personMap = {'name' : 'foobar',
                    'age' : 30,
                    'hello' : "hello my name is foobar and I'm 30 years old",
                    'achievements' : [
                      {'inCompetition' : 'fishing',
                       'rank' : 3,
                       'description' : 'my first throphy'},
                      {'inCompetition' : 'dota',
                       'rank' : 1,
                       'description' : 'my second achievement'}
                    ]},
      person = personFactory.create(personMap);

      print(person.achievements.first); // instance of Achievement
      print(person.achievements.first.rank); // 3
}

can also handle complex fields

class Complex extends Model {
  List<List<Map<String, List<Map<String, Person>>>>> complicated;
}

void main() {
  var data = {'complicated' :
    // complicated value which follows [Complex.complicated] generics},
      complex = new ModelFactory(Complex).create(data);
}

fields with generic will be type-checked to ensure the value given is in sync with the generics e.g. you cannot give List<int> as a value to a field with List<Map<String, dynamic>> as its declaration, but its's ok to give it a null. This is true when you creating a model through new ModelFactory().create() or when you call validate() from model.

What is this validate() ? It is a simple rule to validate your field.

  class ValueCannotFooBar extends Rule {
    const ValueCannotFooBar();

    validate(value) {
      if (value == 'foobar') throw 'value cannot equal to foobar';
    }
  }

  class Foooo extends Model {
    @field
    @ValueCannotFooBar()
    String name;
  }

  void main() {
    var foooo = new Foooo()..name = 'foobar';

    foooo.validate().catchError(handleError);

    foooo.name = '12321321321';

    foooo.validate().then(willPass);
  }

API

Model

model.validate()
model.toMap()

ModelFactory

factory.create(attributes);

TODO

  1. Better error information

License

(The MIT License)

Copyright (c) 2013 Faris Nasution <nasution.faris@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Libraries

mila