mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
added a blocks example
This commit is contained in:
parent
decaea0f5f
commit
47bc1d5fda
1 changed files with 57 additions and 0 deletions
57
examples/blocks.coffee
Normal file
57
examples/blocks.coffee
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
# 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 "Location does not exist" unless path.exists()
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
return io unless block
|
||||||
|
|
||||||
|
try
|
||||||
|
block io
|
||||||
|
finally
|
||||||
|
try
|
||||||
|
io.close() unless io.closed()
|
||||||
|
catch error
|
||||||
|
# nothing, just swallow them.
|
||||||
|
|
||||||
|
|
||||||
|
# Write.
|
||||||
|
write: (location, data) ->
|
||||||
|
path = new Pathname location
|
||||||
|
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'
|
||||||
|
|
||||||
|
respond_to (format) ->
|
||||||
|
format.html()
|
||||||
|
format.xml -> render { xml: people.xml() }
|
||||||
|
|
||||||
|
|
||||||
|
# Synchronization.
|
||||||
|
synchronize: (block) ->
|
||||||
|
lock()
|
||||||
|
try block() finally unlock()
|
Loading…
Add table
Reference in a new issue