Skip to content

Commit

Permalink
Couple of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vladtsf committed Jan 30, 2013
1 parent 8c1df57 commit 022c683
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/adapters/json.coffee
Expand Up @@ -23,6 +23,6 @@ class JSONAdapter
# @return [String] result string
#
@stringify: ( data ) ->
JSON.stringify data
JSON.stringify data, null, " "

module.exports = JSONAdapter
25 changes: 19 additions & 6 deletions lib/config-parser.coffee
Expand Up @@ -15,6 +15,7 @@ class ConfigParser
#
constructor: ( @path ) ->
@format = null
@parsed = {}

# Pick config format.
#
Expand Down Expand Up @@ -48,20 +49,32 @@ class ConfigParser
adapter = require "./adapters/#{ format }"

if @rawData is ""
@_parsed = {}
@parsed = {}
else if adapter.detect @rawData
@_parsed = adapter.parse @rawData
@parsed = adapter.parse @rawData
else
throw new Error()

@

# Parse config.
# Stringify and save config.
#
# @return [String] stringified value
# @param [String] format config format
# @param [Function] callback
# @return [ConfigParser]
#
stringify: ( format ) ->
require( "./adapters/#{ format ? @format }" ).stringify @_parsed
stringify: ( format, callback ) ->
if typeof format is "function"
callback = format
format = "json"

try
fs.writeFile @path, ( require( "./adapters/#{ format ? @format }" ).stringify @parsed ), "utf8", ( err ) ->
callback err
catch e
callback e

@

# Supported formats.
@formats: [ "json", "plist", "ini", "yaml" ]
Expand Down
45 changes: 43 additions & 2 deletions lib/runtime-configuration.coffee
Expand Up @@ -43,7 +43,7 @@ class RuntimeConfiguration

iteration = ( item, done ) ->
new rc.ConfigParser( item.file ).pick ->
done null, chain.push @_parsed
done null, chain.push @parsed

# load configs
async.forEach ( { file, idx } for own file, idx in @lookup() ), iteration, ( err ) =>
Expand All @@ -63,7 +63,48 @@ class RuntimeConfiguration
# new rc.Adapter( path ).pick()
@

save: ->
# Save configuration.
#
# @param [String] format config format
# @param [Function] callback
# @return [RuntimeConfiguration] rc instance
#
save: ( format, callback = -> ) ->
if typeof format is "function"
callback = format
format = "json"

config = new rc.ConfigParser( path.join process.env.HOME, ".#{ @appName }rc" )
config.parsed = @config

config.stringify format, callback

@

# Property getter.
#
# @param [String] key property name
# @return [Object] value
#
get: ( key ) ->
if key? then @config[ key ] else extend( {}, @config )

# Property setter.
#
# @param [String|Object] key
# @param [Object] value
# @return [RuntimeConfiguration] rc instance
#
set: ( key, value ) ->
if ( not value? ) and typeof key is "object"
obj = key
else
obj = {}
obj[ key ] = value

extend on, @config, obj

@

# Load env vars
#
Expand Down
3 changes: 3 additions & 0 deletions test/helpers.coffee
Expand Up @@ -5,6 +5,9 @@ global.glob = require "glob"
global.wrench = require( "wrench" )
global.sinon = require "sinon"
global.shld = require "should"
global.plist = require "plist"
global.yaml = require "yamljs"
global.ini = require "ini"

global.__testsRootDir = __dirname
global.__tmpDir = path.join __dirname, ".tmp"
Expand Down
1 change: 0 additions & 1 deletion test/mocha.opts
Expand Up @@ -4,7 +4,6 @@
--compilers coffee:coffee-script
--colors
--reporter spec
--ignore-leaks
--require should
--require coffee-script
--require test/helpers.coffee
Expand Down
40 changes: 37 additions & 3 deletions test/specs/config-parser.coffee
Expand Up @@ -5,6 +5,7 @@ describe "ConfigParser", ->
@yaml = new rc.ConfigParser path.join __homepaths.yaml, ".apprc"
@ini = new rc.ConfigParser path.join __homepaths.ini, ".apprc"
@plist = new rc.ConfigParser path.join __homepaths.plist, ".apprc"
@writeable = new rc.ConfigParser path.join __tmpDir, ".apprc"

after ->
delete @suitable
Expand Down Expand Up @@ -48,11 +49,44 @@ describe "ConfigParser", ->

it "should try to parse config", ->
@json.parse( "json" )
@json._parsed.should.be.a "object"
@json.parsed.should.be.a "object"

it "should raise an error when can't parse", ->
( => @plist.parse( "json" ) ).should.throw()

describe "@stringify()", ->
it "should serialize object to appropriate format", ->
@json.stringify().should.be.a "string"
beforeEach ->
@write = sinon.spy fs, "writeFile"
@writeable.parsed.foo = "bar"

@stringify = ( format, done, callback ) =>
@writeable.stringify format, ( err ) =>
callback.apply @, @write.getCall( 0 ).args
done err


afterEach ->
@write.restore()

delete @write
delete @stringify

it "should write file", ( done ) ->
@stringify "json", done, ( file ) =>
file.should.equal path.join __tmpDir, ".apprc"

it "should serialize json properly", ( done ) ->
@stringify "json", done, ( file, serialized ) =>
JSON.parse( serialized ).should.have.property "foo", "bar"

it "should serialize ini properly", ( done ) ->
@stringify "ini", done, ( file, serialized ) =>
ini.parse( serialized ).should.have.property "foo", "bar"

it "should serialize yaml properly", ( done ) ->
@stringify "yaml", done, ( file, serialized ) =>
yaml.parse( serialized ).should.have.property "foo", "bar"

it "should serialize plist properly", ( done ) ->
@stringify "plist", done, ( file, serialized ) =>
plist.parseStringSync( serialized ).should.have.property "foo", "bar"
45 changes: 41 additions & 4 deletions test/specs/runtime-configuration.coffee
Expand Up @@ -101,6 +101,40 @@ describe "RuntimeConfiguration", ->
.be.a( "object" )
.and.have.property( "baz", "test2" )

describe "@set()", ->
beforeEach ( done ) ->
process.env.HOME = __tmpDir
@config = new rc.RuntimeConfiguration( "app" )
@config.load done

afterEach ->
delete @config

it "should set only one key", ->
@config.set "foo", "bar"
@config.config.should.have.property "foo", "bar"

it "should set many keys by object", ->
@config.set bar: "baz", baz: "foo"
@config.config.should.include bar: "baz", baz: "foo"

describe "@get()", ->
beforeEach ( done ) ->
process.env.HOME = __tmpDir
@config = new rc.RuntimeConfiguration( "app" )
@config.load ( err ) ->
@set "foo", "bar"
done err

afterEach ->
delete @config

it "should get value by key", ->
@config.get( "foo" ).should.equal "bar"

it "should return entire object if key not specified", ->
@config.get().should.be.a "object"

describe "@save()", ->
beforeEach ( done ) ->
process.env.HOME = __tmpDir
Expand All @@ -109,14 +143,17 @@ describe "RuntimeConfiguration", ->
@config = new rc.RuntimeConfiguration( "app" )

@config.load ( err, config ) ->
@config.config.foo = "bar"
@config.save done
@config.foo = "bar"
@save done

afterEach ->
@write.restore()

delete @config
delete @write

it "should write ~/.apprc file", ->
call = @write.getCall( 0 ).args
args = @write.getCall( 0 ).args

call[ 0 ].should.equal path.join __tmpDir, ".apprc"
args[ 0 ].should.equal path.join __tmpDir, ".apprc"
JSON.parse( args[ 1 ] ).should.have.property "foo", "bar"

0 comments on commit 022c683

Please sign in to comment.