This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const user = { | |
| id: 1, | |
| name: 'foo', | |
| company: { | |
| id: 12, | |
| name: 'bar', | |
| address: { | |
| street: 'randomstreet', | |
| } | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // When a process has side effects that need to be executed only in certain situations, | |
| // this functionality can be layered onto an existing operation using Decorators. | |
| // A Decorator takes an object and wraps auxiliary functionality around it, | |
| // letting you add on what you need when you need it and keeping the core procedure untouched. | |
| // Let’s imagine a Service Object that generates report cards for each student in a class. | |
| // A teacher can generate these report cards at any point, but there are some situations where those report cards should be mailed to the students’ parent. | |
| // One way to address this would be to have two separate Service Objects, | |
| // GenerateReportCards and GenerateReportCardsAndEmailParent, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Helmkamp describes the potential overlap in concept between a Policy Object and a Query Object or a Service Object. | |
| // He writes, “Policy Objects are similar to Service Objects, but I use the term ‘Service Object’ for write operations and 'Policy Object’ for reads. | |
| // They are also similar to Query Objects, but Query Objects focus on executing SQL to return a result set, whereas Policy Objects operate on domain models already loaded into memory.” | |
| // Let’s imagine a collection of students that comprise the entire freshman student body. | |
| // We want to filter out all students that are eligible to register for sports, which we define with these rules: | |
| // Not suspended | |
| // Not expelled | |
| // Are passing all classes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Query objects provide a nice tool for extracting query logic and associated operations into a contained module, | |
| // pulling the logic out into a more maintainable and readable structure, | |
| // while also providing a very readable API where the query object is used. | |
| // Let’s imagine an API endpoint that returns a JSON representation of all students that are currently passing. | |
| // Without using Query Objects, we might have a function in an API controller method or a Service Object that could look like this | |
| // (note the user of DetermineStudentPassingStatus, the example from the Service Objects post): | |
| var MongoClient = require('mongodb').MongoClient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // A Form Object can encapsulate all associated logic into a single object, keeping it focused, isolated, and easy to test. | |
| // You may have a sign up form that creates an account, so an associated Form Object could handle the following logic: | |
| // Make sure all fields that are required are present | |
| // Make sure all values are valid | |
| // Persist the data to the database | |
| // Provide success or error feedback to the user | |
| // Let’s imagine a teacher is registering new students for the school year. | |
| // The application can hand the form data off to the Form Object for handling all aspects of its processing flow: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Value Objects as “…simple objects whose equality is dependent on their value rather than an identity.” | |
| // Because JavaScript adheres to the “pass-by-reference” principle for all JavaScript objects, | |
| // there are no native examples of this in ECMAScript 5 or even Harmony. | |
| var _ = require('underscore'); | |
| var Grade = function(percentage) { | |
| this.percentage = percentage; | |
| this.grade = this.grade(percentage); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Service Objects can be an extremely valuable tool in cleaning up and refactoring your code. | |
| // Isolating actions keeps logic clean, orderly, easy to test, and ultimately leads to a more maintainable code base. | |
| var _ = require('underscore'); | |
| /** | |
| * For a given student, determine whether or not they are passing | |
| * @constructor | |
| * @param {Object} student - The student that being evaluated. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Composition Example | |
| // http://codepen.io/ericelliott/pen/XXzadQ?editors=001 | |
| // https://gist.github.com/ericelliott/fed0fd7a0d3388b06402 | |
| const distortion = { distortion: 1 }; | |
| const volume = { volume: 1 }; | |
| const cabinet = { cabinet: 'maple' }; | |
| const lowCut = { lowCut: 1 }; | |
| const inputLevel = { inputLevel: 1 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const machine = { | |
| init(context) { | |
| this.context = context; | |
| this._stateTransitions = {}; | |
| this._stateTransitionsAny = {}; | |
| this._defaultTransition = null; | |
| this._initialState = null; | |
| this._currentState = null; | |
| return this; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* basic factorys */ | |
| const makeAnimal = state => ({ | |
| eat: () => `${state.name} eat`, | |
| breathe: () => `${state.name} breathe` | |
| }); | |
| const makeMachine = state => ({ | |
| reload: () => `${state.name} reload`, | |
| turnOn: () => `${state.name} turned on`, |