File: blocks.coffee

package info (click to toggle)
coffeescript 1.10.0~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,292 kB
  • sloc: makefile: 62
file content (54 lines) | stat: -rw-r--r-- 1,068 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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 fs.existsSync(location)

  File.open path, 'a', (file) ->
    file.console.log 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
    io.close() unless io.closed()


# Write.
write = (location, data) ->
  path = new Pathname location
  throw new Error "Location does not exist" unless fs.existsSync location

  File.open path, 'w', (file) ->
    return false if Digest.MD5.hexdigest(file.read()) is data.hash()
    file.console.log 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()