From 90995ba0b2d37ddfba0765b8d7b7ffbc2fa33619 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 23 Nov 2011 14:42:38 -0500 Subject: [PATCH] Settings grab data from ~/.atomocity/settings.coffee --- src/atom/resource.coffee | 4 ++++ src/stdlib/settings.coffee | 45 +++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/atom/resource.coffee b/src/atom/resource.coffee index 806b17bac..8e6e51e11 100644 --- a/src/atom/resource.coffee +++ b/src/atom/resource.coffee @@ -6,9 +6,13 @@ Pane = require 'pane' module.exports = class Resource extends Pane position: "main" + settings: {} url: null # Can be used to delegate key events to another object, such as a pane. + constructor: -> + atom.settings.applyTo this + responder: -> this diff --git a/src/stdlib/settings.coffee b/src/stdlib/settings.coffee index 4435afe5a..869ed4813 100644 --- a/src/stdlib/settings.coffee +++ b/src/stdlib/settings.coffee @@ -1,8 +1,47 @@ fs = require 'fs' +{CoffeeScript} = require 'coffee-script' module.exports = class Settings + settings: {} + constructor: -> - atom.on 'window:load', -> - if fs.isFile "~/.atomicity/settings.coffee" - require "~/.atomicity/settings.coffee" + @load "~/.atomicity/settings.coffee" + console.log @settings + + load: (path) -> + path = require.resolve path + if not fs.isFile path + console.warn "Could not find settings file '#{path}'" + return + + try + json = CoffeeScript.eval "return " + (fs.read path) + + for className, value of json + @settings[@simplifyClassName className] = value + catch error + console.error "Can't evaluate settings at `#{path}`." + console.error error + + applyTo: (object) -> + if not object.settings + console.warning "#{object.constructor.name}: Does not have `settings` varible" + return + + classHierarchy = [] + + # ICK: Using internal var __super to get the build heirarchy + walker = object + while walker + classHierarchy.unshift @simplifyClassName walker.constructor.name + walker = walker.constructor.__super__ + + for className in classHierarchy + for setting, value of @settings[className] ? {} + object.settings[setting] = value + + # I don't care if you use camelCase, underscores or dashes. It should all + # point to the same place + simplifyClassName: (className) -> + className.toLowerCase().replace /\W/, ''