Skip to content
Samuel edited this page May 27, 2014 · 3 revisions

Configuration

class ComputedValue {
  final _computation;
  bool _evaluated;
  dynamic _value;
  ComputedValue(dynamic _computation(Map config));

  /** Lazy compute the value */
  getValue(config);
}

$(dynamic computation(ConfigMap config)) => new ComputedValue(computation);

class ConfigMap {
  ConfigMap root;

  /**
   * Recursively converts [other] to [ConfigMap].
   */
  ConfigMap({ConfigMap root: null, Map other: const {}});

  ConfigMap.config(ConfigMap root, Map data);
  operator[](key) => _data[key] is ComputedValue ? _data[key].getValue(root) : _data[key];
}

class Configuration {

  /**
   * Register configuration under [name].
   */
  void add(String name, Map configuration, parent: String);
  
  /** 
   * Get configuration stored under [name].
   * 
   * Name of the configuration is stored under key "__name__".
   * Throws [ArgumentError] if configuration with [name] does not exists.
   */
  Map get(name);
}

Example

var config = new Configuration();

config.add("master", {
  "page": { // you can use nested values
    "title": "My new homepage",
    "url": $((c) => c['__name__]), // and reference other values in config
  },
});

// Find configuration by registered name.
config.get("master");

Clone this wiki locally