Skip to content

Commit

Permalink
re-factoring to add more sentence types
Browse files Browse the repository at this point in the history
  • Loading branch information
James Thomas committed May 30, 2017
1 parent f52d75b commit c03e58b
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 220 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Sentence = require('./src/sentence')
const Sentences = require('./src/sentences')

module.exports = {
Sentence
Sentences
}
37 changes: 37 additions & 0 deletions src/concept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

class Concept {
constructor (property, value) {
this.concept = { property, value }
this.clauses = []
}

is (parentConcept) {
this.clauses.push(`is a ${parentConcept}`)
return this
}

value (letter, property) {
this.clauses.push(`has the value ${letter} as ~ ${property} ~`)
return this
}

property (relationship, name, letter) {
this.clauses.push(`~ ${relationship} ~ the ${name} ${letter}`)
return this
}

toString () {
let conceptString = `conceptualise a ~ ${this.concept.property} ~ ${this.concept.value}`

if (this.clauses.length) {
const clausesString = this.clauses.join(' and ')

conceptString = `${conceptString} that ${clausesString}`
}

return `${conceptString}.`
}
}

module.exports = Concept
11 changes: 11 additions & 0 deletions src/extend_instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const Instance = require('./instance')

class ExtendInstance extends Instance {
prefix () {
return `the ${this.instance.concept} ${this.instance.name}`
}
}

module.exports = ExtendInstance
41 changes: 41 additions & 0 deletions src/instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

class Instance {
constructor (concept, name) {
this.instance = { concept, name}
this.clauses = []
}

is (name, concept, value) {
this.clauses.push(`is the ${name} of the ${concept} ${value}`)
return this
}

has (property, concept) {
this.clauses.push(`has \'${property}\' as ${concept}`)
return this
}

has_clauses () {
return !!this.clauses.length
}

prefix () {
let prefix = `there is a ${this.instance.concept} named ${this.instance.name}`
return this.has_clauses() ? `${prefix} that` : prefix
}

toString () {
let conceptString = this.prefix()

if (this.has_clauses()) {
const clausesString = this.clauses.join(' and ')

conceptString = `${conceptString} ${clausesString}`
}

return `${conceptString}.`
}
}

module.exports = Instance
138 changes: 0 additions & 138 deletions src/sentence.js

This file was deleted.

38 changes: 38 additions & 0 deletions src/sentences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const sentencesSymbol = Symbol('sentences')

const Concept = require('./concept')
const Instance = require('./instance')
const ExtendInstance = require('./extend_instance')

class Sentences {
constructor () {
this[sentencesSymbol] = []
}

toString () {
return this[sentencesSymbol]
.map(sentence => sentence.toString())
.join(' ')
}

concept (property, value) {
return this.sentence(new Concept(property, value))
}

there_is_a (concept, instance) {
return this.sentence(new Instance(concept, instance))
}

the (concept, instance) {
return this.sentence(new ExtendInstance(concept, instance))
}

sentence (klass) {
this[sentencesSymbol].push(klass)
return klass
}
}

module.exports = Sentences
Loading

0 comments on commit c03e58b

Please sign in to comment.