Skip to content

Commit

Permalink
Refactor this to use a simple YAML file. Now to figure out why Cocoa …
Browse files Browse the repository at this point in the history
…is choking on a dictionary key read...right now Railcar is broken :(
  • Loading branch information
jm committed Apr 24, 2012
1 parent 2adb7a0 commit 0fd1ca6
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions Railcar/RCApplicationManager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,41 @@
#

class RCApplicationManager
def appsFile
File.join((ENV['RAILCAR_PATH'] || NSBundle.mainBundle.bundlePath), "application.yml")
end

def applications
NSUserDefaults.standardUserDefaults['railcar.linkedApplications'] ||= {}
NSUserDefaults.standardUserDefaults['railcar.linkedApplications']
if File.exist?(appsFile)
YAML.load_file(appsFile)
else
saveApplicationsList({})
{}
end
end

def saveApplicationsList(data)
File.open(appsFile, "w") do |f|
f.write(YAML.dump(data))
end
end

# We do the persistentDomainForName song and dance so the CLI can use this same code
def addApplicationToList(data)
defaults = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")
newAppData = {}
newAppData[uniqueId(data)] = data

hsh = defaults.dup
hsh['railcar.linkedApplications'][uniqueId(data)] = data

NSUserDefaults.standardUserDefaults.setPersistentDomain(hsh, forName: "com.arcturo.railcar")
saveApplicationsList(applications.merge(newAppData))
end

def appDataForPath(path)
appList = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")['railcar.linkedApplications']

appList.values.select do |data|
applications.values.select do |data|
data[:path] == path
end.first
end

def appDataForName(name)
appList = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")['railcar.linkedApplications']

appList.values.select do |data|
applications.values.select do |data|
data[:name] == name
end.first
end
Expand All @@ -42,17 +50,16 @@ def uniqueId(data)
"#{Time.now.to_i}-#{data[:name].gsub(/\W/, '')}-#{data[:path].gsub(/\W/, '')}"
end

# We do the persistentDomainForName song and dance so the CLI can use this same code
def add(path, data = {})
if isRailsApp?(path)
defaults = NSUserDefaults.standardUserDefaults.persistentDomainForName("com.arcturo.railcar")

newAppPort = (applications.empty? ? (applications.length + 3001) : 3000)
addApplicationToList({
:name => discernAppName(path),
:path => path,
:rubyVersion => DEFAULT_RUBY_VERSION,
:environment => "development",
:port => (defaults['railcar.linkedApplications'].length + 3001).to_s
:port => ().to_s
}.merge(data))

true
Expand Down

0 comments on commit 0fd1ca6

Please sign in to comment.