Skip to content

Commit

Permalink
Refactors and inmutable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarobock committed Apr 28, 2017
1 parent d7140c1 commit fbb53b0
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 143 deletions.
69 changes: 48 additions & 21 deletions src/app/domain/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,6 @@ export enum HopForm {
Pellet, WholeLeaf, Plug
}

export class EditableRecipe {

constructor(
public recipe: Recipe,
private calcService: CalcService,
private changeService: ChangeService
) {}

change(field: string) {
console.log('change', field);
this.changeService.change(field, this);
}
}

const defaultRecipe = {
STYLE: {}
};
Expand All @@ -86,35 +72,43 @@ export class Bom1Recipe implements Recipe {
private _hops: Hop[];
private _vital: Vital;

constructor(private obj: any = defaultRecipe, private calcService: CalcService) {
this._fermentables = this.obj.FERMENTABLES.FERMENTABLE.map(f => new Bom1Fermentable(f, this.calcService));
this._hops = this.obj.HOPS.HOP.map(h => new Bom1Hop(h, this.calcService));
this._vital = new Bom1Vital(this.obj, this.calcService);
constructor(
private obj: any = defaultRecipe,
private calcService: CalcService,
private changeService: ChangeService
) {
this._fermentables = this.obj.FERMENTABLES.FERMENTABLE.map(f => new Bom1Fermentable(f, this.calcService, this.changeService, this));
this._hops = this.obj.HOPS.HOP.map(h => new Bom1Hop(h, this.calcService, this.changeService, this));
this._vital = new Bom1Vital(this.obj, this.calcService, this.changeService, this);
}

get name(): string {
return this.obj.NAME;
}
set name(value: string) {
this.obj.NAME = value;
this.changeService.change('name', this);
}
get style(): string {
return this.obj.STYLE.NAME;
}
set style(value: string) {
this.obj.STYLE.NAME = value;
this.changeService.change('style', this);
}
get vital(): Vital {
return this._vital;
}
set amountFermentables(value: number) {
this.obj.totalAmount = value;
this.changeService.change('amountFermentables', this);
}
get amountFermentables(): number {
return this.obj.totalAmount;
}
set amountHops(value: number) {
this.obj.totalHop = value;
this.changeService.change('amountHops', this);
};
get amountHops(): number {
return this.obj.totalHop;
Expand All @@ -128,37 +122,47 @@ export class Bom1Recipe implements Recipe {
}

export class Bom1Vital implements Vital {
constructor(private obj: any, private calcService: CalcService) {}
constructor(
private obj: any,
private calcService: CalcService,
private changeService: ChangeService,
private recipe: Recipe
) { }

get batchSize(): number {
return this.obj.BATCH_SIZE;
}
set batchSize(value: number) {
this.obj.BATCH_SIZE = value;
this.changeService.change('vital.batchSize', this.recipe);
}
get og(): number {
return this.obj.OG;
}
set og(value: number) {
this.obj.OG = value;
this.changeService.change('vital.og', this.recipe);
}
get fg(): number {
return this.obj.FG;
}
set fg(value: number) {
this.obj.FG = value;
this.changeService.change('vital.fg', this.recipe);
}
get abv(): number {
return this.obj.ABV;
}
set abv(value: number) {
this.obj.ABV = value;
this.changeService.change('vital.abv', this.recipe);
}
get ibu(): number {
return this.obj.CALCIBU;
}
set ibu(value: number) {
this.obj.CALCIBU = value;
this.changeService.change('vital.ibu', this.recipe);
}
get bugu(): number {
return this.calcService.balance(this.obj.CALCIBU, this.obj.OG);
Expand All @@ -168,30 +172,39 @@ export class Bom1Vital implements Vital {
}
set efficiency(value: number) {
this.obj.EFFICIENCY = value;
this.changeService.change('vital.efficiency', this.recipe);
}
get bv(): number {
return this.obj.BV;
}
set bv(value: number) {
this.obj.BV = value;
this.changeService.change('vital.bv', this.recipe);
}
}

export class Bom1Fermentable implements Fermentable {

constructor(private obj: any, private calcService: CalcService) {}
constructor(
private obj: any,
private calcService: CalcService,
private changeService: ChangeService,
private recipe: Recipe
) { }

get name(): string {
return this.obj.NAME;
}
set name(value: string) {
this.obj.NAME = value;
this.changeService.change('fermentable.time', this.recipe);
}
get amount(): number {
return this.obj.AMOUNT;
}
set amount(value: number) {
this.obj.AMOUNT = value;
this.changeService.change('fermentable.amount', this.recipe);
}
get type(): FermentableType {
return this.obj.NAME.toLowerCase().indexOf('sugar') !== -1 ? FermentableType.Sugar : FermentableType.Grain;
Expand All @@ -201,36 +214,46 @@ export class Bom1Fermentable implements Fermentable {
}
set srm(value: number) {
this.obj.COLOR = value;
this.changeService.change('fermentable.srm', this.recipe);
}
get potential(): number {
return this.obj.POTENTIAL;
}
set potential(value: number) {
this.obj.POTENTIAL = value;
this.changeService.change('fermentable.potential', this.recipe);
}
get use(): FermentableUse {
return this.obj.USE;
}
set use(value: FermentableUse) {
this.obj.USE = value;
this.changeService.change('fermentable.use', this.recipe);
}
}

export class Bom1Hop implements Hop {

constructor(private obj: any, private calcService: CalcService) {}
constructor(
private obj: any,
private calcService: CalcService,
private changeService: ChangeService,
private recipe: Recipe
) { }

get name(): string {
return this.obj.NAME;
}
set name(value: string) {
this.obj.NAME = value;
this.changeService.change('hop.name', this.recipe);
}
get alpha(): number {
return this.obj.ALPHA;
}
set alpha(value: number) {
this.obj.ALPHA = value;
this.changeService.change('hop.alpha', this.recipe);
}
get beta(): number {
return null;
Expand All @@ -243,12 +266,14 @@ export class Bom1Hop implements Hop {
}
set amount(value: number) {
this.obj.AMOUNT = value;
this.changeService.change('hop.amount', this.recipe);
}
get use(): HopUse {
return this.obj.USE;
}
set use(value: HopUse) {
this.obj.USE = value;
this.changeService.change('hop.use', this.recipe);
}
get temperature(): number {
return null;
Expand All @@ -258,11 +283,13 @@ export class Bom1Hop implements Hop {
}
set time(value: number) {
this.obj.TIME = value;
this.changeService.change('hop.time', this.recipe);
}
get form(): HopForm {
return this.obj.FORM;
}
set form(value: HopForm) {
this.obj.FORM = value;
this.changeService.change('hop.form', this.recipe);
}
}
61 changes: 26 additions & 35 deletions src/app/services/change.service.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,72 @@
import { CalcService } from './calc.service';
import { EditableRecipe } from '../domain/recipe';
import { Recipe } from '../domain/recipe';
import { Injectable } from '@angular/core';
import * as _ from 'lodash';

/**
* @todo #INMUTABLE: convert to inmutable,
* extract constructor to config or similar class
* ver en el blog como maneja la DI
*/
@Injectable()
export class ChangeService {

map = new Map<string, ((recipe: EditableRecipe) => any)[]>();
map = new Map<string, ((recipe: Recipe) => any)[]>();

constructor(private calcService: CalcService) {

// this.add('vital.bugu', ['vital.og', 'vital.ibu'], (editable: EditableRecipe) => {
// editable.recipe.vital.bugu = this.calcService.balance(editable.recipe.vital.ibu, editable.recipe.vital.og);
// });

this.add('vital.bv', ['vital.og', 'vital.fg', 'vital.ibu'], (editable: EditableRecipe) => {
editable.recipe.vital.bv = this.calcService.bv(editable.recipe.vital.og, editable.recipe.vital.fg, editable.recipe.vital.ibu);
this.add('vital.bv', ['vital.og', 'vital.fg', 'vital.ibu'], (recipe: Recipe) => {
recipe.vital.bv = this.calcService.bv(recipe.vital.og, recipe.vital.fg, recipe.vital.ibu);
});

this.add('vital.abv', ['vital.og', 'vital.fg'], (editable: EditableRecipe) => {
editable.recipe.vital.abv = this.calcService.abv(editable.recipe.vital.og, editable.recipe.vital.fg);
this.add('vital.abv', ['vital.og', 'vital.fg'], (recipe: Recipe) => {
recipe.vital.abv = this.calcService.abv(recipe.vital.og, recipe.vital.fg);
});

this.add(
'vital.og',
['vital.batchSize', 'vital.efficiency', 'fermentable.potential', 'fermentable.amount'],
(editable: EditableRecipe) => {
editable.recipe.vital.og = this.calcService.og(
editable.recipe.vital.batchSize,
editable.recipe.vital.efficiency,
editable.recipe.fermentables
(recipe: Recipe) => {
recipe.vital.og = this.calcService.og(
recipe.vital.batchSize,
recipe.vital.efficiency,
recipe.fermentables
);
}
);

this.add(
'vital.ibu',
['vital.og', 'vital.batchSize', 'hop.amount', 'hop.alpha', 'hop.use', 'hop.time', 'hop.form'],
(editable: EditableRecipe) => {
(recipe: Recipe) => {
const ogNoSugar = this.calcService.og(
editable.recipe.vital.batchSize,
editable.recipe.vital.efficiency,
editable.recipe.fermentables,
recipe.vital.batchSize,
recipe.vital.efficiency,
recipe.fermentables,
true
);
editable.recipe.vital.ibu = this.calcService.ibu(editable.recipe.hops, ogNoSugar, editable.recipe.vital.batchSize);
recipe.vital.ibu = this.calcService.ibu(recipe.hops, ogNoSugar, recipe.vital.batchSize);
}
);

this.add('amoutFermentables', ['fermentable.amount'], (editable: EditableRecipe) => {
editable.recipe.amountFermentables = _.sumBy(editable.recipe.fermentables, f => f.amount);
this.add('amoutFermentables', ['fermentable.amount'], (recipe: Recipe) => {
recipe.amountFermentables = _.sumBy(recipe.fermentables, f => f.amount);
});

this.add('amoutHops', ['hop.amount'], (editable: EditableRecipe) => {
editable.recipe.amountHops = _.sumBy(editable.recipe.hops, h => h.amount);
this.add('amoutHops', ['hop.amount'], (recipe: Recipe) => {
recipe.amountHops = _.sumBy(recipe.hops, h => h.amount);
});
}

change(field: string, editable: EditableRecipe) {
change(field: string, recipe: Recipe) {
if (this.map.has(field)) {
this.map.get(field).forEach(calc => calc(editable));
this.map.get(field).forEach(calc => calc(recipe));
}
}

add(fieldName: string, depends: string[], calc: (recipe: EditableRecipe) => any) {
add(fieldName: string, depends: string[], calc: (recipe: Recipe) => any) {
depends.forEach(field => {
if ( !this.map.has(field) ) {
this.map.set(field, []);
}
this.map.get(field).push((editable: EditableRecipe) => {
calc(editable);
this.change(fieldName, editable);
this.map.get(field).push((recipe: Recipe) => {
calc(recipe);
this.change(fieldName, recipe);
});
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/services/services.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Util } from './util.service';
import { RecipesService } from './recipes.service';
import { CalcService, Srm2Rgb, Gravity2Ppg } from './calc.service';
import { NgModule } from '@angular/core';
Expand All @@ -10,7 +9,7 @@ import { ChangeService } from './change.service';
CommonModule
],
exports: [Srm2Rgb, Gravity2Ppg],
providers: [RecipesService, CalcService, ChangeService, Util],
providers: [RecipesService, CalcService, ChangeService],
declarations: [Srm2Rgb, Gravity2Ppg]
})
export class ServicesModule { }
15 changes: 0 additions & 15 deletions src/app/services/util.service.spec.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/app/services/util.service.ts

This file was deleted.

Loading

2 comments on commit fbb53b0

@0pdd
Copy link

@0pdd 0pdd commented on fbb53b0 Apr 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle INMUTABLE-27729359 disappeared, that's why I closed as #10

@0pdd
Copy link

@0pdd 0pdd commented on fbb53b0 Apr 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle refact-d8591d4a discovered and submitted as #11

Please sign in to comment.