Sha256: db4263035baebd0db9ffa2a776e5db29ac22d2fdfaa0fd40156e0980f17305de

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 KB

Contents

# 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()

  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()

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
haml-more-0.5.1 vendor/coffee-script/examples/blocks.coffee
haml-more-0.5.0 vendor/coffee-script/examples/blocks.coffee
haml-more-0.4.0 vendor/coffee-script/examples/blocks.coffee
haml-more-0.4.0.d vendor/coffee-script/examples/blocks.coffee
haml-more-0.4.0.c vendor/coffee-script/examples/blocks.coffee
haml-more-0.4.0.b vendor/coffee-script/examples/blocks.coffee
haml-more-0.4.0.a vendor/coffee-script/examples/blocks.coffee