Skip to content

Commit

Permalink
Config is now a reactive object
Browse files Browse the repository at this point in the history
  • Loading branch information
pirhoo committed Jan 30, 2020
1 parent 112843a commit 2b5b6ef
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 52 deletions.
123 changes: 71 additions & 52 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,80 @@
import get from 'lodash/get'
import each from 'lodash/each'
import Vue from 'vue'
import defaultValues from './config.default'

const _VALUES = typeof Symbol === 'function' ? Symbol('values') : '_VALUES'
const _SCOPES = typeof Symbol === 'function' ? Symbol('scopes') : '_SCOPES'
const _VALUES = '_VALUES'
const _SCOPES = '_SCOPES'

export class Config {
constructor (values = {}) {
this[_VALUES] = {}
this.merge(values)
return this;
}
merge (values = {}) {
return each(values, (value, key) => {
this.set(key, value)
})
}
set (key, value) {
const levels = key.split('.')
if (levels.length > 1) {
this[_VALUES][key] = this.scope(levels.shift()).set(levels.join('.'), value)
} else {
this[_VALUES][key] = value
export const Config = Vue.extend({
props: {
defaultValues: {
type: Object,
default: () => ({})
}
return value
}
get (key, defaultValue) {
return get(this[_VALUES], key, defaultValue)
}
is (key) {
const value = this.get(key, null)
switch(value) {
case 1: return true
case true: return true
case '1': return true
case 'true': return true
case 0: return false
case false: return false
case '0': return false
case 'false': return false
default: return !!value
},
data () {
return {
[_VALUES]: null,
[_SCOPES]: null
}
},
created () {
this[_VALUES] = Vue.observable({})
this[_SCOPES] = Vue.observable({})
this.merge(this.defaultValues)
},
methods: {
merge (values = {}) {
return each(values, (value, key) => {
this.set(key, value)
})
},
set (key, value) {
const levels = key.split('.')
if (levels.length > 1) {
this.$set(this[_VALUES], key, this.scope(levels.shift()).set(levels.join('.'), value))
} else {
this.$set(this[_VALUES], key, value)
}
return value
},
get (key, defaultValue) {
return get(this[_VALUES], key, defaultValue)
},
is (key) {
const value = this.get(key, null)
switch(value) {
case 1: return true
case true: return true
case '1': return true
case 'true': return true
case 0: return false
case false: return false
case '0': return false
case 'false': return false
default: return !!value
}
},
isnt (key) {
return !this.is(key)
},
scope (name) {
this.$set(this[_SCOPES], name, get(this.scopes, name, new Config()))
return this[_SCOPES][name]
},
},
computed: {
values () {
return this[_VALUES]
},
scopes () {
this[_SCOPES] = get(this, _SCOPES, {})
return this[_SCOPES]
}
}
isnt (key) {
return !this.is(key)
}
scope (name) {
this.scopes[name] = get(this.scopes, name, new Config())
return this.scopes[name]
}
get values () {
return this[_VALUES]
}
get scopes () {
this[_SCOPES] = get(this, _SCOPES, {})
return this[_SCOPES]
}
}
})

export default new Config(defaultValues)
export default new Config({
propsData: { defaultValues }
})
4 changes: 4 additions & 0 deletions tests/unit/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ describe('config.js', () => {
config.set('activated', false)
expect(config.isnt('activated')).toBeTruthy()
})

it('should create an observable object', () => {

})
})

0 comments on commit 2b5b6ef

Please sign in to comment.