Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
283 additions
and
138 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.DS_Store | ||
_figson.coffee | ||
|
||
# Logs | ||
logs | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
}, | ||
"dependencies": { | ||
"lodash": "^2.4.1", | ||
"pathval": "^0.1.1" | ||
"pathval": "^0.1.1", | ||
"when": "^3.1.0" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
{ | ||
"foo": "bar" | ||
"foo": "bar", | ||
"long": { | ||
"deeply": { | ||
"nested": { | ||
"property": [ | ||
"support" | ||
] | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.