Class: Snack::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/snack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Application

Returns a new instance of Application.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/snack.rb', line 7

def initialize(options = {})
  @settings = options
  @settings[:output_dir] ||= '../'

  app = self
  @builder = Rack::Builder.new do
    use Rack::CommonLogger
    use Rack::ShowStatus      # Nice looking 404s and other messages
    use Rack::ShowExceptions  # Nice looking errors
    run Snack::Server.new app
  end
end

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



5
6
7
# File 'lib/snack.rb', line 5

def builder
  @builder
end

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/snack.rb', line 5

def settings
  @settings
end

Instance Method Details

#buildObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/snack.rb', line 24

def build
  FileUtils.cd @settings[:root] do
    # collect all files that don't start with '_'
    files = Dir[File.join('**', '*')].reject { |f| f =~ /(^_|\/_)/ }

    files.each do |file|
      path = File.join @settings[:output_dir], file

      if Tilt[path]
        body = View.new(file).render
        File.open(path.chomp(File.extname(path)), 'w') { |f| f.write body }
      elsif Dir.exists? file
        FileUtils.mkpath path
      else
        FileUtils.cp file, path
      end
    end
  end
end

#serveObject



20
21
22
# File 'lib/snack.rb', line 20

def serve
  Rack::Handler::Thin.run @builder, Port: 9393
end