Class: Flatrack
- Inherits:
-
Object
- Object
- Flatrack
- 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
-
.gem_root ⇒ String
readonly
The root of the flatrack gem.
-
.site_root ⇒ String
readonly
The site root.
Instance Attribute Summary collapse
-
#assets ⇒ Sprockets::Environment
readonly
The flatrack sprockets environment.
-
#middleware ⇒ Hash
readonly
The middleware stack for flatrack.
-
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
-
#redirects ⇒ Hash
readonly
The redirects.
-
#rewrites ⇒ Hash
readonly
The rewrites.
-
#site_root ⇒ String
readonly
The site root.
Class Method Summary collapse
-
.reset! ⇒ Object
Reset the state of flatrack and its configuration For testing.
- .site=(instance) ⇒ Object
Instance Method Summary collapse
-
#config {|Flatrack| ... } ⇒ Flatrack
Configure the flatrack instance.
-
#mock_env_for(url, opts = {}) ⇒ Object
This is for testing.
-
#redirect(source, to: nil, type: :permanent) ⇒ Object
redirect a path.
- #Redirector(source, opts = {}) ⇒ Object
-
#register_format(ext, mime) ⇒ Object
register a format extension by its mime type.
-
#rewrite(source, to: nil) ⇒ Object
Rewrite a path.
- #Rewriter(source, opts = {}) ⇒ Object
-
#site ⇒ Proc
Returns the site lambda.
- #site_root(path) ⇒ Object readonly
-
#use(middleware, options = nil) ⇒ Object
Insert a rack middleware at the end of the stack.
Class Attribute Details
.gem_root ⇒ String (readonly)
The root of the flatrack gem
45 46 47 |
# File 'lib/flatrack.rb', line 45 def gem_root File. File.join __FILE__, '..' end |
.site_root ⇒ String (readonly)
The site root
52 53 54 |
# File 'lib/flatrack.rb', line 52 def site_root Dir.pwd end |
Instance Attribute Details
#assets ⇒ Sprockets::Environment (readonly)
The flatrack 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 |
#middleware ⇒ Hash (readonly)
The middleware stack for flatrack
116 117 118 |
# File 'lib/flatrack.rb', line 116 def middleware @middleware ||= [] end |
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
39 40 41 |
# File 'lib/flatrack.rb', line 39 def raise_errors @raise_errors end |
#redirects ⇒ Hash (readonly)
The redirects
158 159 160 |
# File 'lib/flatrack.rb', line 158 def redirects @redirects ||= {} end |
#rewrites ⇒ Hash (readonly)
The rewrites
151 152 153 |
# File 'lib/flatrack.rb', line 151 def rewrites @rewrites ||= {} end |
#site_root ⇒ String (readonly)
The site root
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
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
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
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
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 |
#site ⇒ Proc
Returns the site lambda
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. path Template.register_path path @site_root = path end |
#use(middleware, options = nil) ⇒ Object
Insert a rack middleware at the end of the stack
165 166 167 |
# File 'lib/flatrack.rb', line 165 def use(middleware, = nil) self.middleware << [middleware, ].compact end |