Class: Flatrack

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/flatrack.rb,
lib/flatrack/cli.rb,
lib/flatrack/sass.rb,
lib/flatrack/site.rb,
lib/flatrack/view.rb,
lib/flatrack/request.rb,
lib/flatrack/version.rb,
lib/flatrack/response.rb,
lib/flatrack/rewriter.rb,
lib/flatrack/template.rb,
lib/flatrack/middleware.rb,
lib/flatrack/redirector.rb,
lib/flatrack/template/rb.rb,
lib/flatrack/domain_parser.rb,
lib/flatrack/sass/importer.rb,
lib/flatrack/template/html.rb,
lib/flatrack/sass/functions.rb,
lib/flatrack/template/erubis.rb,
lib/flatrack/view/tag_helper.rb,
lib/flatrack/asset_extensions.rb,
lib/flatrack/view/link_helper.rb,
lib/flatrack/sass/sass_template.rb,
lib/flatrack/sass/scss_template.rb,
lib/flatrack/view/output_buffer.rb,
lib/flatrack/view/render_helper.rb,
lib/flatrack/view/capture_helper.rb,
lib/flatrack/view/request_helper.rb,
lib/flatrack/template/erubis/handler.rb

Overview

Version

Defined Under Namespace

Modules: AssetExtensions, Sass, Site Classes: CLI, DomainParser, Middleware, Redirector, Request, Response, Rewriter, Template, View

Constant Summary collapse

TemplateNotFound =
Class.new StandardError
FileNotFound =
Class.new StandardError
FORMATS =
{}
MAPPING =
{ '/assets' => :assets, '/' => :site }
VERSION =
'1.4.1'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.gem_rootString (readonly)

The root of the flatrack gem

Returns:

  • (String)


45
46
47
# File 'lib/flatrack.rb', line 45

def gem_root
  File.expand_path File.join __FILE__, '..'
end

.site_rootString (readonly)

The site root

Returns:

  • (String)


52
53
54
# File 'lib/flatrack.rb', line 52

def site_root
  Dir.pwd
end

Instance Attribute Details

#assetsSprockets::Environment (readonly)

The flatrack sprockets environment

Returns:

  • (Sprockets::Environment)


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/flatrack.rb', line 92

def assets
  @assets ||= begin
    Sass.load_paths << File.join(site_root, 'assets/stylesheets')
    Sprockets::Environment.new.tap do |environment|
      environment.register_engine '.sass', Sprockets::Sass::SassTemplate
      environment.register_engine '.scss', Sprockets::Sass::ScssTemplate
      environment.append_path File.join site_root, 'assets/images'
      environment.append_path File.join site_root, 'assets/javascripts'
      environment.append_path File.join site_root, 'assets/stylesheets'
      environment.context_class.class_eval { include AssetExtensions }
    end
  end
end

#middlewareHash (readonly)

The middleware stack for flatrack

Returns:

  • (Hash)


116
117
118
# File 'lib/flatrack.rb', line 116

def middleware
  @middleware ||= []
end

#raise_errorsObject

Returns the value of attribute raise_errors.



39
40
41
# File 'lib/flatrack.rb', line 39

def raise_errors
  @raise_errors
end

#redirectsHash (readonly)

The redirects

Returns:

  • (Hash)


158
159
160
# File 'lib/flatrack.rb', line 158

def redirects
  @redirects ||= {}
end

#rewritesHash (readonly)

The rewrites

Returns:

  • (Hash)


151
152
153
# File 'lib/flatrack.rb', line 151

def rewrites
  @rewrites ||= {}
end

#site_rootString (readonly)

The site root

Returns:

  • (String)


178
179
180
# File 'lib/flatrack.rb', line 178

def site_root
  @site_root || (self.site_root = self.class.site_root)
end

Class Method Details

.reset!Object

Reset the state of flatrack and its configuration For testing



59
60
61
# File 'lib/flatrack.rb', line 59

def reset!
  @delegate_instance = nil
end

.site=(instance) ⇒ Object



63
64
65
# File 'lib/flatrack.rb', line 63

def site=(instance)
  @delegate_instance = instance
end

Instance Method Details

#config {|Flatrack| ... } ⇒ Flatrack

Configure the flatrack instance

Yields:

  • (Flatrack)

    configuration for the flatrack instance

Returns:



81
82
83
84
85
86
87
# File 'lib/flatrack.rb', line 81

def config
  yield self if block_given?
  OpenStruct.new(
    site_root:    site_root,
    raise_errors: raise_errors
  ).freeze
end

#mock_env_for(url, opts = {}) ⇒ Object

This is for testing



122
123
124
125
# File 'lib/flatrack.rb', line 122

def mock_env_for(url, opts={})
  env = Rack::MockRequest.env_for url, opts
  env.merge! env_extensions
end

#redirect(source, to: nil, type: :permanent) ⇒ Object

redirect a path

Parameters:

  • source (String)
  • to (String) (defaults to: nil)
  • type (Symbol) (defaults to: :permanent)


141
142
143
144
145
146
# File 'lib/flatrack.rb', line 141

def redirect(source, to: nil, type: :permanent)
  unless [source, to].all? { |path| path.is_a? String }
    raise ArgumentError, 'mappings must be strings'
  end
  redirects.merge! source => Redirector::Redirect.new(to, type)
end

#Redirector(source, opts = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/flatrack/redirector.rb', line 2

def Redirector(source, opts={})
  to    = opts.delete(:to)
  type  = opts.delete(:type) || :permanent
  klass = Class.new(Redirector)
  klass.send(:define_method, :initialize) do |app, mw_opts|
    mapping = { source => Redirector::Redirect.new(to, type) }
    super app, mapping, mw_opts
  end
  klass
end

#register_format(ext, mime) ⇒ Object

register a format extension by its mime type

Parameters:

  • ext (String)

    the extension

  • mime (String)

    the mime type



109
110
111
# File 'lib/flatrack.rb', line 109

def register_format(ext, mime)
  FORMATS[ext.to_s] = mime
end

#rewrite(source, to: nil) ⇒ Object

Rewrite a path

Parameters:

  • source (String)
  • to (String) (defaults to: nil)


130
131
132
133
134
135
# File 'lib/flatrack.rb', line 130

def rewrite(source, to: nil)
  unless [source, to].all? { |path| path.is_a? String }
    raise ArgumentError, 'mappings must be strings'
  end
  rewrites.merge! source => to
end

#Rewriter(source, opts = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/flatrack/rewriter.rb', line 2

def Rewriter(source, opts={})
  to    = opts.delete(:to)
  klass = Class.new(Rewriter)
  klass.send(:define_method, :initialize) do |app, mw_opts|
    mapping = { source => to }
    super app, mapping, mw_opts
  end
  klass
end

#siteProc

Returns the site lambda

Returns:

  • (Proc)


184
185
186
187
188
189
# File 'lib/flatrack.rb', line 184

def site
  lambda { |env|
    env.merge! env_extensions
    Request.new(env).response
  }
end

#site_root=(path) ⇒ Object (readonly)



169
170
171
172
173
# File 'lib/flatrack.rb', line 169

def site_root=(path)
  path = File.expand_path path
  Template.register_path path
  @site_root = path
end

#use(middleware, options = nil) ⇒ Object

Insert a rack middleware at the end of the stack

Parameters:

  • middleware (Class)

    the middleware class

  • options (Hash) (defaults to: nil)

    the options for the middleware



165
166
167
# File 'lib/flatrack.rb', line 165

def use(middleware, options = nil)
  self.middleware << [middleware, options].compact
end