Skip to content

Commit

Permalink
Merge c6b5b77 into b05ffc8
Browse files Browse the repository at this point in the history
  • Loading branch information
zspecza committed Apr 9, 2014
2 parents b05ffc8 + c6b5b77 commit 4d12911
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
.DS_Store
_figson.coffee

# Logs
logs
Expand Down
84 changes: 84 additions & 0 deletions lib/_figson.coffee
@@ -0,0 +1,84 @@
props = require 'pathval'
fs = require 'fs'
path = require 'path'
_ = require 'lodash'

class Figson

constructor: (@configFile) ->
unless @configFile
throw new Error "Figson: configuration file expected, but not received."
@compress = false
@indentor = 2
parse.call(@)

###*
* sets the value of a key
* @param {string} key - the property that is being set
* @param {(string|number|array|object|null)} val - the value to assign
###
set: (key, val) ->
props.set(@data, key, val)
save.call(@)

###*
* fetches the value of a key
* @param {string} key - the property who's value is being fetched
* @return {(string|number|array|object|null)} - the fetched value
###
get: (key) ->
props.get(@data, key)

###*
* updates a key - first gets, checks for existence, then either
* sets the key or throws an error if the key does not exist
* @param {string} key - the property to update
* @param {(string|number|array|object|null)} val - the value to update with
###
update: (key, val) ->
unless @get(key)
throw new Error "Figson: cannot update non-existent property '#{key}'"
else
@set(key, val)

###*
* deletes a property of an object and saves to disk
* @param {string} key - the property to delete
###
destroy: (key) ->
@set(key, undefined)
save.call(@)

###*
* converts JSON to javascript object
* @api private
* @return {object} - object heirarchy of parsed JSON data
###
parse = ->
@data = JSON.parse readFile.call(@)

###*
* reads in a file
* @api private
* @return {string} file contents
###
readFile = ->
fs.readFileSync(@configFile)

###*
* saves the current object data to the config file
* @api private
###
save = ->
fs.writeFileSync(@configFile, stringify.call(@))

###*
* stringify converts javascript object to JSON
* @api private
* @return {json} json representation of data object
###
stringify = ->
if @compress
JSON.stringify(@data)
else
JSON.stringify(@data, null, @indentor)
26 changes: 26 additions & 0 deletions lib/config.coffee
@@ -0,0 +1,26 @@
props = require 'pathval'
fs = require 'fs'

class Config

constructor: (@file, @data) ->

set: (key, val) ->
props.set(@data, key, val)

get: (key, val) ->
props.get(@data, key)

update: (key, val) ->
unless @get(key)
throw new Error "Figson: cannot update non-existent property '#{key}'"
else
@set(key, val)

destroy: (key) ->
@set(key, undefined)

save: (callback) ->
fs.writeFile(@file, JSON.stringify(@data, null, 2), callback)

module.exports = Config
84 changes: 84 additions & 0 deletions lib/figson.coffee
@@ -0,0 +1,84 @@
props = require 'pathval'
fs = require 'fs'
path = require 'path'
_ = require 'lodash'

class Figson

constructor: (@configFile) ->
unless @configFile
throw new Error "Figson: configuration file expected, but not received."
@compress = false
@indentor = 2
parse.call(@)

###*
* sets the value of a key
* @param {string} key - the property that is being set
* @param {(string|number|array|object|null)} val - the value to assign
###
set: (key, val) ->
props.set(@data, key, val)
save.call(@)

###*
* fetches the value of a key
* @param {string} key - the property who's value is being fetched
* @return {(string|number|array|object|null)} - the fetched value
###
get: (key) ->
props.get(@data, key)

###*
* updates a key - first gets, checks for existence, then either
* sets the key or throws an error if the key does not exist
* @param {string} key - the property to update
* @param {(string|number|array|object|null)} val - the value to update with
###
update: (key, val) ->
unless @get(key)
throw new Error "Figson: cannot update non-existent property '#{key}'"
else
@set(key, val)

###*
* deletes a property of an object and saves to disk
* @param {string} key - the property to delete
###
destroy: (key) ->
@set(key, undefined)
save.call(@)

###*
* converts JSON to javascript object
* @api private
* @return {object} - object heirarchy of parsed JSON data
###
parse = ->
@data = JSON.parse readFile.call(@)

###*
* reads in a file
* @api private
* @return {string} file contents
###
readFile = ->
fs.readFileSync(@configFile)

###*
* saves the current object data to the config file
* @api private
###
save = ->
fs.writeFileSync(@configFile, stringify.call(@))

###*
* stringify converts javascript object to JSON
* @api private
* @return {json} json representation of data object
###
stringify = ->
if @compress
JSON.stringify(@data)
else
JSON.stringify(@data, null, @indentor)
88 changes: 2 additions & 86 deletions lib/index.coffee
@@ -1,86 +1,2 @@
props = require 'pathval'
fs = require 'fs'
path = require 'path'
_ = require 'lodash'

class Figson

constructor: (@configFile) ->
unless @configFile
throw new Error "Figson: configuration file expected, but not received."
@compress = false
@indentor = 2
parse.call(@)

###*
* sets the value of a key
* @param {string} key - the property that is being set
* @param {(string|number|array|object|null)} val - the value to assign
###
set: (key, val) ->
props.set(@data, key, val)
save.call(@)

###*
* fetches the value of a key
* @param {string} key - the property who's value is being fetched
* @return {(string|number|array|object|null)} - the fetched value
###
get: (key) ->
props.get(@data, key)

###*
* updates a key - first gets, checks for existence, then either
* sets the key or throws an error if the key does not exist
* @param {string} key - the property to update
* @param {(string|number|array|object|null)} val - the value to update with
###
update: (key, val) ->
unless @get(key)
throw new Error "Figson: cannot update non-existent property '#{key}'"
else
@set(key, val)

###*
* deletes a property of an object and saves to disk
* @param {string} key - the property to delete
###
destroy: (key) ->
@set(key, undefined)
save.call(@)

###*
* converts JSON to javascript object
* @api private
* @return {object} - object heirarchy of parsed JSON data
###
parse = ->
@data = JSON.parse readFile.call(@)

###*
* reads in a file
* @api private
* @return {string} file contents
###
readFile = ->
fs.readFileSync(@configFile)

###*
* saves the current object data to the config file
* @api private
###
save = ->
fs.writeFileSync(@configFile, stringify.call(@))

###*
* stringify converts javascript object to JSON
* @api private
* @return {json} json representation of data object
###
stringify = ->
if @compress
JSON.stringify(@data)
else
JSON.stringify(@data, null, @indentor)

module.exports = (f) -> new Figson(f)
Parser = require './parser'
module.exports = new Parser()
13 changes: 13 additions & 0 deletions lib/parser.coffee
@@ -0,0 +1,13 @@
fs = require 'fs'
Config = require './config'
nodefn = require 'when/node'

class Parser

parse: (file, callback) ->
nodefn.call fs.readFile, file, endoding: 'utf8'
.then JSON.parse
.then (data) -> callback(null, new Config(file, data))
.done null, callback.bind(callback)

module.exports = Parser
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"lodash": "^2.4.1",
"pathval": "^0.1.1"
"pathval": "^0.1.1",
"when": "^3.1.0"
}
}
11 changes: 10 additions & 1 deletion test/fixtures/config.json
@@ -1,3 +1,12 @@
{
"foo": "bar"
"foo": "bar",
"long": {
"deeply": {
"nested": {
"property": [
"support"
]
}
}
}
}

0 comments on commit 4d12911

Please sign in to comment.