all files / src/ element.ts

100% Statements 21/21
100% Branches 6/6
100% Functions 6/6
100% Lines 21/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50    14× 14× 14×         14×                                              
import { Response, RequestOptionsArgs } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { RestangularPath } from './path';
 
export class RestangularElement<T> {
  [key: string]: any
  __path__: RestangularPath;
  __original__: any;
 
  constructor(
    object: any,
    path?: RestangularPath
  ) {
    this.__original__ = object;
    this.__path__ = path;
 
    Object.assign(this, {}, object);
  }
 
  clone(): RestangularElement<T> {
    return Object.assign(Object.create(this), this);
  }
 
  put(body?: any, options?: RequestOptionsArgs): Observable<Response> {
    return this.__path__.put(Object.assign({}, this.plain(), body), options);
  }
 
  remove(options?: RequestOptionsArgs): Observable<Response> {
    return this.__path__.remove(options);
  }
 
  plain(entity?: { new(): T ;}): T {
    let obj: any = {};
 
    for(let key in this) {
      if (this.hasOwnProperty(key) && !emptyElement.hasOwnProperty(key)) {
        obj[key] = this[key];
      }
    }
 
    if (entity) {
      return Object.assign(new entity(), obj);
    }
 
    return Object.assign(Object.create(this.__original__), obj);
  }
}
 
const emptyElement: RestangularElement<any> = new RestangularElement<any>(null, null);