1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/examples/blocks.coffee

55 lines
1 KiB
CoffeeScript
Raw Normal View History

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.
append = (location, data) ->
path = new Pathname location
throw new Error("Location does not exist") unless path.exists()
2010-02-07 11:33:29 -05:00
File.open path, 'a', (file) ->
file.puts YAML.dump data
data
# Rubinius' File.open implementation.
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
io.close() unless io.closed()
2010-02-07 11:33:29 -05:00
# Write.
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()
file.puts YAML.dump data
true
# Rails' respond_to.
index = ->
people = Person.find 'all'
2010-02-07 11:33:29 -05:00
respond_to (format) ->
format.html()
format.xml -> render xml: people.xml()
2010-02-07 11:33:29 -05:00
# Synchronization.
synchronize = (block) ->
2010-02-07 11:33:29 -05:00
lock()
try block() finally unlock()