Module: Gopher::DSL

Defined in:
lib/gopher2000/dsl.rb

Overview

DSL that can be used to specify gopher apps with a very simple format.

See Also:

  • examples/ directory for working scripts

Instance Method Summary collapse

Instance Method Details

#applicationGopher::Application

initialize an instance of an Application if we haven't already, otherwise, return

the current app

Returns:



16
17
18
19
20
21
# File 'lib/gopher2000/dsl.rb', line 16

def application
  return @application unless @application.nil?

  @application = Gopher::Application.new
  @application.reset!
end

#default_route(&block) ⇒ Object

specify a default route



38
39
40
# File 'lib/gopher2000/dsl.rb', line 38

def default_route(&block)
  application.default_route(&block)
end

#helpers { ... } ⇒ Object

specify some helpers for your app

Yields:

  • block which defines the helper methods



79
80
81
# File 'lib/gopher2000/dsl.rb', line 79

def helpers(&block)
  application.helpers(&block)
end

specify a menu template

Parameters:

  • name (Symbol)

    of the template

Yields:

  • block which renders the template



62
63
64
# File 'lib/gopher2000/dsl.rb', line 62

def menu(name, &block)
  application.menu(name, &block)
end

#mount(path, opts = {}) ⇒ Object

mount a folder for browsing



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gopher2000/dsl.rb', line 43

def mount(path, opts = {})
  route, folder = path.first

  #
  # if path has more than the one option (:route => :folder),
  # then incorporate the rest of the hash into our opts
  #
  if path.size > 1
    other_opts = path.dup
    other_opts.delete(route)
    opts = opts.merge(other_opts)
  end

  application.mount(route, opts.merge({:path => folder}))
end

#route(path) { ... } ⇒ Object

specify a route

Parameters:

  • path (String)

    path of the route

Yields:

  • block that will respond to the request



33
34
35
# File 'lib/gopher2000/dsl.rb', line 33

def route(path, &block)
  application.route(path, &block)
end

#run(script, opts = {}) ⇒ Object

run a script with the specified options applied to the config. This is

called by bin/gopher2000

Parameters:

  • script (String)

    path to script to run

  • opts (Hash) (defaults to: {})

    options to pass to script. these will override any config options specified in the script, so you can use this to run on a different host/port, etc.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gopher2000/dsl.rb', line 97

def run(script, opts = {})

  load script

  #
  # apply options after loading the script so that anything specified on the command-line
  # will take precedence over defaults specified in the script
  #
  opts.each { |k, v|
    set k, v
  }

  if application.config[:debug] == true
    puts "watching #{script} for changes"
    watch script
  end

end

#set(key, value = nil) ⇒ Object

set a config value

Parameters:

  • key (Symbol)

    key to add to config

  • value (String) (defaults to: nil)

    value to set



26
27
28
# File 'lib/gopher2000/dsl.rb', line 26

def set(key, value = nil)
  application.config[key] = value
end

#text(name) { ... } ⇒ Object

specify a text template

Parameters:

  • name (Symbol)

    of the template

Yields:

  • block which renders the template



69
70
71
# File 'lib/gopher2000/dsl.rb', line 69

def text(name, &block)
  application.text(name, &block)
end

#watch(f) ⇒ Object

watch the specified script for changes

Parameters:

  • f (String)

    script to watch



85
86
87
# File 'lib/gopher2000/dsl.rb', line 85

def watch(f)
  application.scripts << f
end