2010-02-07 11:33:29 -05:00
|
|
|
# After wycats' http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/
|
|
|
|
|
|
|
|
# Sinatra.
|
|
|
|
get '/hello', ->
|
|
|
|
'Hello World'
|
|
|
|
|
|
|
|
|
|
|
|
# Append.
|
2010-07-29 00:03:42 -04:00
|
|
|
append = (location, data) ->
|
|
|
|
path = new Pathname location
|
2010-02-23 21:59:29 -05:00
|
|
|
throw new Error("Location does not exist") unless path.exists()
|
2010-02-07 11:33:29 -05:00
|
|
|
|
|
|
|
File.open path, 'a', (file) ->
|
2010-10-24 12:48:42 -04:00
|
|
|
file.console.log YAML.dump data
|
2010-02-07 11:33:29 -05:00
|
|
|
|
|
|
|
data
|
|
|
|
|
|
|
|
|
|
|
|
# Rubinius' File.open implementation.
|
2010-07-29 00:03:42 -04:00
|
|
|
File.open = (path, mode, block) ->
|
|
|
|
io = new File path, mode
|
2010-02-07 11:33:29 -05:00
|
|
|
|
|
|
|
return io unless block
|
|
|
|
|
|
|
|
try
|
|
|
|
block io
|
|
|
|
finally
|
2010-07-29 00:03:42 -04:00
|
|
|
io.close() unless io.closed()
|
2010-02-07 11:33:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Write.
|
2010-07-29 00:03:42 -04:00
|
|
|
write = (location, data) ->
|
|
|
|
path = new Pathname location
|
2010-02-07 11:33:29 -05:00
|
|
|
raise "Location does not exist" unless path.exists()
|
|
|
|
|
|
|
|
File.open path, 'w', (file) ->
|
|
|
|
return false if Digest.MD5.hexdigest(file.read()) is data.hash()
|
2010-10-24 12:48:42 -04:00
|
|
|
file.console.log YAML.dump data
|
2010-02-07 11:33:29 -05:00
|
|
|
true
|
|
|
|
|
|
|
|
|
|
|
|
# Rails' respond_to.
|
2010-07-29 00:03:42 -04:00
|
|
|
index = ->
|
|
|
|
people = Person.find 'all'
|
2010-02-07 11:33:29 -05:00
|
|
|
|
|
|
|
respond_to (format) ->
|
|
|
|
format.html()
|
2010-07-29 00:03:42 -04:00
|
|
|
format.xml -> render xml: people.xml()
|
2010-02-07 11:33:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Synchronization.
|
2010-07-29 00:03:42 -04:00
|
|
|
synchronize = (block) ->
|
2010-02-07 11:33:29 -05:00
|
|
|
lock()
|
|
|
|
try block() finally unlock()
|