flatrack 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8edfe0f35ac6cc52afc3bedd60f818c74e7e1d52
4
- data.tar.gz: 5b338685735c1feb6dece6a301b20d50d19cf5ce
3
+ metadata.gz: d0aec0d226a3b1fbf6614fdd772325b2d393a58a
4
+ data.tar.gz: c066f48a3047e11b32a7df82e5ad544bf8d7390f
5
5
  SHA512:
6
- metadata.gz: 09b78371dbfe2ec291364dfa6ba660b53dc3ab2f5d0e80e4afeacfeaaee91b8e71a2cf0f4ab030b813df6dec4ddf4b74c4c4c2d4a3355c3854a2830ff87a1af6
7
- data.tar.gz: 94b13b995d764115712e86573a76a5f7859d83ff5a290e9ac31ec9acc2d532b147dc96e9e135f08ef37b66b28d167c67feafae2ae5dca8cd56d39c7b94f96603
6
+ metadata.gz: 57a09c2d7aae4bfab7c95450a5a5a41b4ed1f34abf5a345d90a8f444d4e301b0e7fdb81c2de6c9e9cc3d26a9fa7b86f772cbefde2eb500755a3b277f0f21e603
7
+ data.tar.gz: 61804f644c5f4c5bfece7bf8974ebbdca4707a06c8196ae32b214cc18d758b0c5ea5dbdadd66b5b4fca6806a687665fceaf215f1ae257d884b551dcc2b0009bb
data/.gitignore CHANGED
@@ -4,3 +4,7 @@ pkg/*
4
4
  tmp
5
5
  coverage
6
6
  /.sass-cache/
7
+
8
+ /.yardoc/
9
+
10
+ /doc/
data/.rubocop.yml CHANGED
@@ -1,6 +1,3 @@
1
1
  AllCops:
2
2
  Excludes:
3
3
  - vendor/bundle/**
4
-
5
- Documentation:
6
- Enabled: false
data/flatrack.gemspec CHANGED
@@ -50,4 +50,5 @@ based routing.
50
50
  spec.add_development_dependency 'simplecov', '~> 0.8'
51
51
  spec.add_development_dependency 'rubocop', '~> 0.18'
52
52
  spec.add_development_dependency 'inch', '~> 0.3'
53
+ spec.add_development_dependency 'yard', '~> 0.3'
53
54
  end
@@ -1,6 +1,10 @@
1
1
  class Flatrack
2
+ # Provides asset helpers to various parts of a flatrack site.
2
3
  module AssetExtensions
3
- def asset_path(path, options = {})
4
+ # The path to a given asset
5
+ # @param path [String] the asset name or path
6
+ # @return [String]
7
+ def asset_path(path, _ = {})
4
8
  File.join('/assets', path.to_s)
5
9
  end
6
10
  end
data/lib/flatrack/cli.rb CHANGED
@@ -2,10 +2,12 @@ require 'thor'
2
2
  require 'flatrack'
3
3
 
4
4
  class Flatrack
5
+ # The command line interface for flatrack
5
6
  class CLI < Thor
6
7
  include FileUtils
7
8
  include Thor::Actions
8
9
 
10
+ # @private
9
11
  SRC_ROOT = File.join Flatrack.gem_root, 'flatrack/cli/templates'
10
12
  source_root SRC_ROOT
11
13
 
@@ -14,7 +16,8 @@ class Flatrack
14
16
 
15
17
  desc 'new NAME', 'create a new flatrack site with the given name'
16
18
 
17
- KEEP_DIRS = [
19
+ # @private
20
+ KEEP_DIRS = [
18
21
  'assets/stylesheets',
19
22
  'assets/javascripts',
20
23
  'assets/images',
@@ -23,7 +26,8 @@ class Flatrack
23
26
  'partials'
24
27
  ]
25
28
 
26
- FILES = {
29
+ # @private
30
+ FILES = {
27
31
  '.gitignore' => '.gitignore',
28
32
  'boot.rb' => 'boot.rb',
29
33
  'Rakefile' => 'Rakefile',
@@ -35,10 +39,13 @@ class Flatrack
35
39
  'javascript.js.coffee' => 'assets/javascripts/main.js.coffee'
36
40
  }
37
41
 
42
+ # @private
38
43
  BIN_COPY_FILES = {
39
44
  'logo.png' => 'assets/images/logo.png'
40
45
  }
41
46
 
47
+ # Create a new app
48
+ # @param path [String]
42
49
  def new(path)
43
50
  mkdir_p path
44
51
  full_path = File.expand_path path
@@ -52,8 +59,8 @@ class Flatrack
52
59
  method_option :verbose, type: :boolean, default: true, aliases: :v
53
60
  method_option :port, type: :numeric, default: 5959, aliases: :p
54
61
 
55
- desc 'start PORT', 'run the site on the given port'
56
-
62
+ desc 'start --port PORT', 'run the site on the given port'
63
+ # Start the app
57
64
  def start
58
65
  require './boot'
59
66
  run_opts = {}
@@ -64,6 +71,15 @@ class Flatrack
64
71
  Rack::Server.start run_opts
65
72
  end
66
73
 
74
+ method_option :version, type: :boolean, default: false, aliases: :v
75
+ desc '--version', 'flatrack version'
76
+ # Info about flatrack
77
+ def __default__
78
+ puts 'Flatrack ' + Flatrack::VERSION and return if options[:version]
79
+ end
80
+
81
+ default_task :__default__
82
+
67
83
  private
68
84
 
69
85
  def bundle!
@@ -1,26 +1,35 @@
1
1
  class Flatrack
2
+ # parses an incoming flatrack request and provides a method to render a
3
+ # response
2
4
  class Request
5
+ # @private
3
6
  DEFAULT_FORMAT = 'html'
4
7
 
5
8
  attr_reader :env, :rack_request
6
9
 
10
+ # Initializes a response
11
+ # @param env [Hash] the rack env
7
12
  def initialize(env)
8
13
  @rack_request = Rack::Request.new(env)
9
14
  @env = env
10
15
  end
11
16
 
17
+ # the path of the incoming request
12
18
  def path
13
19
  env['PATH_INFO']
14
20
  end
15
21
 
22
+ # the params on the incoming request
16
23
  def params
17
24
  rack_request.params.with_indifferent_access
18
25
  end
19
26
 
27
+ # the format on the incoming request
20
28
  def format
21
29
  (ext = File.extname path).empty? ? DEFAULT_FORMAT : ext.sub(/\./, '')
22
30
  end
23
31
 
32
+ # the processed response for an inbound request
24
33
  def response
25
34
  Response.new(self).render
26
35
  rescue TemplateNotFound
@@ -1,25 +1,23 @@
1
1
  class Flatrack
2
+ # Handles flatrack responses
2
3
  class Response
3
- DEFAULT_FILE = 'index'
4
- CONTENT_TYPES = {
5
- html: 'text/html',
6
- rb: 'text/html'
7
- }
4
+ # @private
5
+ DEFAULT_FILE = 'index'
8
6
 
9
7
  attr_reader :request
10
8
 
9
+ # Initializes a response
10
+ # @param request [Flatrack::Request]
11
11
  def initialize(request)
12
12
  @request = request
13
13
  end
14
14
 
15
- def headers
16
- @headers ||= {}
17
- end
18
-
19
- def body
20
- @body ||= []
21
- end
22
-
15
+ # Renders a response
16
+ # @param opts [Hash]
17
+ # @option opts [String] :file
18
+ # @option opts [Fixnum] :status
19
+ # @option opts [Symbol] :layout
20
+ # @return [Array] the rack response
23
21
  def render(file: file_for(request.path), status: 200, layout: :layout)
24
22
  page_content = proc { renderer_for_page(file).render(view) }
25
23
  set_content_type
@@ -31,11 +29,19 @@ class Flatrack
31
29
  [status, headers, body]
32
30
  end
33
31
 
34
- def set_content_type
35
- headers['Content-Type'] = CONTENT_TYPES[request.format.to_sym]
32
+ private
33
+
34
+ def body
35
+ @body ||= []
36
36
  end
37
37
 
38
- private
38
+ def headers
39
+ @headers ||= {}
40
+ end
41
+
42
+ def set_content_type
43
+ headers['Content-Type'] = FORMATS[request.format.to_sym]
44
+ end
39
45
 
40
46
  def file_for(path)
41
47
  if File.directory?(File.join 'pages', path)
data/lib/flatrack/site.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'rack/server'
2
2
 
3
3
  class Flatrack
4
+ # Provides a rake wrapper for encapsulating a flatrack site
4
5
  module Site
6
+ # Call the site with a rack env
7
+ # @param env [Hash] the rack env
5
8
  def self.call(env)
6
9
  builder.call(env)
7
10
  end
@@ -1,9 +1,12 @@
1
1
  require 'erubis'
2
2
 
3
3
  class Flatrack
4
- module Template
4
+ class Template
5
5
  class Erubis < Tilt::ErubisTemplate
6
+ # The default ERB handler for rendering erb files in flatrack
6
7
  class Handler < ::Erubis::Eruby
8
+ private
9
+
7
10
  def add_preamble(src)
8
11
  @newline_pending = 0
9
12
  end
@@ -24,31 +27,27 @@ class Flatrack
24
27
  # Erubis toggles <%= and <%== behavior when escaping is enabled.
25
28
  # We override to always treat <%== as escaped.
26
29
  def add_expr(src, code, indicator)
27
- case indicator
28
- when '=='
29
- add_expr_escaped(src, code)
30
- else
31
- super
32
- end
30
+ return super unless indicator == '=='
31
+ add_expr_escaped(src, code)
33
32
  end
34
33
 
35
34
  BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
36
35
 
37
36
  def add_expr_literal(src, code)
38
- flush_newline_if_pending(src)
39
- if code =~ BLOCK_EXPR
40
- src << '@output_buffer.append= ' << code
41
- else
42
- src << '@output_buffer.append=(' << code << ');'
43
- end
37
+ add_expr_with_type(src, code)
44
38
  end
45
39
 
46
40
  def add_expr_escaped(src, code)
41
+ add_expr_with_type(src, code, :safe)
42
+ end
43
+
44
+ def add_expr_with_type(src, code, type = nil)
45
+ setter = [type, :append].compact.join('_')
47
46
  flush_newline_if_pending(src)
48
47
  if code =~ BLOCK_EXPR
49
- src << '@output_buffer.safe_append= ' << code
48
+ src << "@output_buffer.#{setter}= " << code
50
49
  else
51
- src << '@output_buffer.safe_append=(' << code << ');'
50
+ src << "@output_buffer.#{setter}=(" << code << ');'
52
51
  end
53
52
  end
54
53
 
@@ -1,9 +1,12 @@
1
1
  class Flatrack
2
- module Template
2
+ class Template
3
+ # The tilt template for rendering ERB in flatrack
3
4
  class Erubis < Tilt::ErubisTemplate
4
5
  extend ActiveSupport::Autoload
5
6
  autoload :Handler
6
7
 
8
+ private
9
+
7
10
  def self.engine_initialized?
8
11
  defined? Handler
9
12
  end
@@ -1,6 +1,9 @@
1
1
  class Flatrack
2
- module Template
2
+ class Template
3
+ # The tilt template for rendering HTML in flatrack
3
4
  class Html < Tilt::PlainTemplate
5
+ private
6
+
4
7
  def evaluate(scope, locals, &block)
5
8
  super.html_safe
6
9
  end
@@ -1,8 +1,11 @@
1
1
  require 'erubis'
2
2
 
3
3
  class Flatrack
4
- module Template
4
+ class Template
5
+ # The tilt template for rendering Ruby in flatrack
5
6
  class Rb < Tilt::ErubisTemplate
7
+ private
8
+
6
9
  def data
7
10
  "<%= #{super} %>"
8
11
  end
@@ -4,9 +4,28 @@ require 'flatrack/template/rb'
4
4
  require 'flatrack/template/html'
5
5
 
6
6
  class Flatrack
7
- module Template
8
- def find(type, file)
9
- template = find_by_type type, file
7
+ # The default template parser/finder
8
+ class Template
9
+ attr_reader :type, :file
10
+
11
+ # Creates a new template instance and invokes find
12
+ # @param type [Symbol] the type of template
13
+ # @param file [String] the location of the file
14
+ def self.find(type, file)
15
+ new(type, file).find
16
+ end
17
+
18
+ # Creates a new template instance
19
+ # @param type [Symbol] the type of template
20
+ # @param file [String] the location of the file
21
+ def initialize(type, file)
22
+ @type, @file = type, file
23
+ end
24
+
25
+ # Finds a given template
26
+ # @return [Tilt::Template]
27
+ def find
28
+ template = find_by_type
10
29
  fail FileNotFound, "could not find #{file}" unless template
11
30
  Tilt.new template, options
12
31
  rescue RuntimeError
@@ -22,14 +41,12 @@ class Flatrack
22
41
  local_options
23
42
  end
24
43
 
25
- def find_by_type(type, file)
44
+ def find_by_type
26
45
  if File.exist?(file)
27
46
  file
28
47
  else
29
48
  Dir[File.join type.to_s.pluralize, "#{file}*"].first
30
49
  end
31
50
  end
32
-
33
- module_function :find, :find_by_type, :options
34
51
  end
35
52
  end
@@ -1,3 +1,5 @@
1
+ # Version
1
2
  class Flatrack
2
- VERSION = '1.2.0'
3
+ # @private
4
+ VERSION = '1.2.1'
3
5
  end
@@ -1,5 +1,6 @@
1
1
  class Flatrack
2
2
  class View
3
+ # Block capture support
3
4
  module CaptureHelper
4
5
  include ERB::Util
5
6
 
@@ -1,25 +1,54 @@
1
1
  class Flatrack
2
2
  class View
3
+ # View helpers to render link tags
3
4
  module LinkHelper
4
5
  include TagHelper
5
6
 
6
- def link_to(name = nil, options = nil, html_options = nil, &block)
7
+ # Creates an HTML link tag
8
+ #
9
+ # @overload link_to(href, options={})
10
+ # Creates an html link tag to URL, using the URL as the content
11
+ # of the tag.
12
+ #
13
+ # @param href [String] the link
14
+ # @param options [Hash] html options for the tag
15
+ # @return [String]
16
+ #
17
+ # @overload link_to(content, href, options={})
18
+ # Creates an html link tag to URL, using the provided content as the
19
+ # content of the tag.
20
+ #
21
+ # @param content [String] the content to be displayed for the link tag
22
+ # @param href [String] the link
23
+ # @param options [Hash] html options for the tag
24
+ # @return [String]
25
+ #
26
+ # @overload link_to(href, options={}, &block)
27
+ # Creates an html link tag to URL, using the provided return value of
28
+ # the block as the content of the tag.
29
+ #
30
+ # @param href [String] the link
31
+ # @yield the content of the tag
32
+ # @return [String]
33
+ def link_to(*args, &block)
34
+ href, options, block = link_to_options(*args, &block)
35
+ html_tag :a, link_to_tag_options(href, options || {}), &block
36
+ end
37
+
38
+ private
39
+
40
+ def link_to_options(content_or_href = nil, href_or_options = nil,
41
+ options = nil, &block)
7
42
  if block_given?
8
- href, options, block = name, options, block
9
- elsif options.is_a?(Hash) || options.blank?
10
- name, href, options, block = name, name, options, block
43
+ [content_or_href, href_or_options, block]
44
+ elsif href_or_options.is_a?(Hash) || href_or_options.blank?
45
+ [content_or_href, href_or_options, proc { content_or_href }]
11
46
  else
12
- name, href, options, block = name, options, html_options, block
47
+ [href_or_options, options, proc { content_or_href }]
13
48
  end
14
-
15
- block ||= proc { name }
16
-
17
- html_tag :a, link_to_options(href, options || {}), &block
18
49
  end
19
50
 
20
- private
21
-
22
- def link_to_options(link, opts = {})
51
+ def link_to_tag_options(link, opts = {})
23
52
  link << '?' + opts.delete(:params).to_param if opts[:params].present?
24
53
  { href: link }.merge(opts)
25
54
  end
@@ -1,11 +1,16 @@
1
1
  class Flatrack
2
2
  class View
3
+ # A modified output buffer for block evaluation
3
4
  class OutputBuffer < ActiveSupport::SafeBuffer #:nodoc:
5
+ # Initializes and encodes the buffer
4
6
  def initialize(*)
5
7
  super
6
8
  encode!
7
9
  end
8
10
 
11
+ # appends text to the buffer
12
+ # @param value [String] the value to be appended
13
+ # @return [OutputBuffer]
9
14
  def <<(value)
10
15
  return self if value.nil?
11
16
  super(value.to_s)
@@ -13,6 +18,9 @@ class Flatrack
13
18
 
14
19
  alias_method :append=, :<<
15
20
 
21
+ # safely concatenates value to the buffer
22
+ # @param value [String] the value to be concatenated
23
+ # @return [OutputBuffer]
16
24
  def safe_concat(value)
17
25
  return self if value.nil?
18
26
  super(value.to_s)
@@ -1,6 +1,10 @@
1
1
  class Flatrack
2
2
  class View
3
+ # View helpers to render partials
3
4
  module RenderHelper
5
+ # Renders a partial
6
+ # @param file [Symbol, String]
7
+ # @return [String]
4
8
  def render(file)
5
9
  Template.find(:partial, file.to_s).render(self)
6
10
  end
@@ -1,10 +1,15 @@
1
1
  class Flatrack
2
2
  class View
3
+ # View helpers to access the params and path
3
4
  module RequestHelper
5
+ # Returns the query parameters
6
+ # @return [Hash]
4
7
  def params
5
8
  @response.request.params
6
9
  end
7
10
 
11
+ # Returns the path
12
+ # @return [String]
8
13
  def path
9
14
  @response.request.path
10
15
  end
@@ -1,13 +1,16 @@
1
1
  class Flatrack
2
2
  class View
3
+ # View helpers for rendering various html tags
3
4
  module TagHelper
4
5
  include CaptureHelper
5
6
  include ERB::Util
6
7
 
8
+ # @private
7
9
  PRE_CONTENT_STRINGS = {
8
10
  textarea: "\n"
9
11
  }
10
12
 
13
+ # @private
11
14
  BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked autobuffer
12
15
  autoplay controls loop selected hidden scoped
13
16
  async defer reversed ismap seamless muted
@@ -15,6 +18,25 @@ class Flatrack
15
18
  pubdate itemscope allowfullscreen default inert
16
19
  sortable truespeed typemustmatch).to_set
17
20
 
21
+ # Creates an HTML tag
22
+ #
23
+ # @overload html_tag(name, content, options={})
24
+ # Creates an html tag using the provided content as the content of the
25
+ # tag.
26
+ #
27
+ # @param name [String] the name of the tag (i.e. a, img, style)
28
+ # @param content [String] the content of the tag
29
+ # @param options [Hash] the html options for the tag
30
+ # @return [String]
31
+ #
32
+ # @overload html_tag(name, options={}, &block)
33
+ # Creates an html tag using the provided content as the content of the
34
+ # tag.
35
+ #
36
+ # @param name [String] the name of the tag (i.e. a, img, style)
37
+ # @param options [Hash] the html options for the tag
38
+ # @yield the tag content
39
+ # @return [String]
18
40
  def html_tag(name, content_or_options_with_block = nil, options = nil,
19
41
  escape = true, &block)
20
42
  if block_given?
@@ -27,17 +49,27 @@ class Flatrack
27
49
  end
28
50
  end
29
51
 
52
+ # Returns an HTML image tag
53
+ # @param uri [String] location of the image
54
+ # @param options [Hash] the html options for the tag
55
+ # @return [String]
30
56
  def image_tag(uri, options = {})
31
57
  uri = asset_path(uri) unless uri =~ %r{^(http)?(s)?:?\/\/}
32
58
  options.merge! src: uri
33
59
  html_tag(:img, nil, options)
34
60
  end
35
61
 
62
+ # Returns an HTML script tag for javascript
63
+ # @param uri [String] location of the javascript file
64
+ # @return [String]
36
65
  def javascript_tag(uri)
37
66
  uri = asset_path(uri) + '.js' if uri.is_a? Symbol
38
67
  html_tag(:script, '', src: uri, type: 'application/javascript')
39
68
  end
40
69
 
70
+ # Returns an HTML link tag for css
71
+ # @param uri [String] location of the css file
72
+ # @return [String]
41
73
  def stylesheet_tag(uri)
42
74
  uri = asset_path(uri) + '.css' if uri.is_a? Symbol
43
75
  html_tag(:link, nil, rel: 'stylesheet', type: 'text/css', href: uri)
data/lib/flatrack/view.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class Flatrack
2
+ # The flatrack view
2
3
  class View
3
4
  extend ActiveSupport::Autoload
4
5
 
@@ -19,8 +20,10 @@ class Flatrack
19
20
 
20
21
  attr_accessor :output_buffer
21
22
 
23
+ # initializes a flatrack view
24
+ # @param response [Flatrack::Response]
22
25
  def initialize(response)
23
- @response = response
26
+ @response = response
24
27
  @output_buffer = OutputBuffer.new
25
28
  super(response)
26
29
  rescue ArgumentError
data/lib/flatrack.rb CHANGED
@@ -6,6 +6,7 @@ require 'coffee-script'
6
6
  require 'sass'
7
7
  require 'rack'
8
8
 
9
+ # A static site generator with a little sprinkle of ruby magic
9
10
  class Flatrack
10
11
  extend ActiveSupport::Autoload
11
12
 
@@ -17,41 +18,57 @@ class Flatrack
17
18
  autoload :AssetExtensions
18
19
  autoload :CLI
19
20
 
21
+ # @private
20
22
  TemplateNotFound = Class.new StandardError
23
+ # @private
21
24
  FileNotFound = Class.new StandardError
22
-
25
+ # @private
23
26
  FORMATS = {}
24
27
 
25
28
  delegate :gem_root, :site_root, to: self
26
29
 
27
- def self.gem_root
28
- File.expand_path File.join __FILE__, '..'
29
- end
30
-
31
- def self.site_root
32
- File.expand_path Dir.pwd
33
- end
34
-
35
30
  class << self
36
- def delegate_instance
37
- @delegate_instance ||= new
31
+ # The root of the flatrack gem
32
+ # @!attribute [r] gem_root
33
+ # @return [String]
34
+ def gem_root
35
+ File.expand_path File.join __FILE__, '..'
38
36
  end
39
37
 
38
+ # The site root
39
+ # @!attribute [r] site_root
40
+ # @return [String]
41
+ def site_root
42
+ File.expand_path Dir.pwd
43
+ end
44
+
45
+ # Reset the state of flatrack and its configuration
40
46
  def reset!
41
47
  @delegate_instance = nil
42
48
  end
43
49
 
44
50
  private
45
51
 
52
+ def delegate_instance
53
+ @delegate_instance ||= new
54
+ end
55
+
46
56
  def method_missing(m, *args, &block)
47
57
  delegate_instance.public_method(m).call(*args, &block)
48
58
  end
49
59
  end
50
60
 
61
+ # Configure the flatrack instance
62
+ # @yield [Flatrack] configuration for the flatrack instance
63
+ # @return [Flatrack]
51
64
  def config(&block)
52
65
  yield self
66
+ self
53
67
  end
54
68
 
69
+ # The flatrack sprockets environment
70
+ # @!attribute [r] assets
71
+ # @return [Sprockets::Environment]
55
72
  def assets
56
73
  @assets ||= begin
57
74
  Sprockets::Environment.new.tap do |environment|
@@ -63,16 +80,23 @@ class Flatrack
63
80
  end
64
81
  end
65
82
 
83
+ # register a format extension by its mime type
84
+ # @param ext [String] the extension
85
+ # @param mime [String] the mime type
66
86
  def register_format(ext, mime)
67
87
  FORMATS[ext.to_s] = mime
68
88
  end
69
89
 
90
+ # The middleware stack for flatrack
70
91
  def middleware
71
92
  @middleware ||= []
72
93
  end
73
94
 
74
- def use(*args)
75
- middleware << args
95
+ # Insert a rack middleware at the end of the stack
96
+ # @param middleware [Class] the middleware class
97
+ # @param options [Hash] the options for the middleware
98
+ def use(middleware, options = nil)
99
+ self.middleware << [middleware, options].compact
76
100
  end
77
101
 
78
102
  # By default we know how to render 'text/html'
@@ -0,0 +1,37 @@
1
+ # Rake Extensions
2
+ module Rake
3
+ # Asset class for sprockets pre-compilation
4
+ class AssetTasks::Asset
5
+ # Returns a sprockets asset
6
+ # @param env [Sprockets::Environment]
7
+ # @param file [String]
8
+ def initialize(env, file)
9
+ @env = env
10
+ @file = file
11
+ end
12
+
13
+ # Returns the path of an asset
14
+ # @return [String]
15
+ def path
16
+ File.expand_path(file).sub(/(#{@env.paths.join('|')})\//, '')
17
+ end
18
+
19
+ private
20
+
21
+ def basename
22
+ File.basename @file
23
+ end
24
+
25
+ def parts
26
+ basename.split('.').size
27
+ end
28
+
29
+ def file
30
+ if parts > 2
31
+ @file.split('.').tap(&:pop).join('.')
32
+ else
33
+ @file
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # The tasks defined for pre-compilation of assets
2
+ Rake::AssetTasks::TASKS = proc do
3
+ namespace :assets do
4
+ desc 'precompile assets'
5
+ task :precompile do
6
+ with_logger do
7
+ manifest.compile(assets)
8
+ end
9
+ end
10
+
11
+ desc 'Remove all assets'
12
+ task :clobber do
13
+ with_logger do
14
+ manifest.clobber
15
+ end
16
+ end
17
+
18
+ desc 'Clean old assets'
19
+ task :clean do
20
+ with_logger do
21
+ manifest.clean(keep)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -4,11 +4,12 @@ require 'flatrack'
4
4
  require 'logger'
5
5
 
6
6
  module Rake
7
- # Simple Sprockets compilation Rake task macro.
8
- #
9
- # Rake::AssetTasks.new
10
- #
7
+ # helps define asset tasks
11
8
  class AssetTasks < Rake::TaskLib
9
+ extend ActiveSupport::Autoload
10
+ autoload :Asset
11
+ autoload :TASKS
12
+
12
13
  attr_accessor :output
13
14
  attr_reader :environment
14
15
  attr_reader :index
@@ -17,39 +18,9 @@ module Rake
17
18
 
18
19
  delegate :paths, to: :environment
19
20
 
20
- DEFINED_TASKS = proc do
21
-
22
- namespace :assets do
23
- desc 'precompile assets'
24
- task :precompile do
25
- with_logger do
26
- manifest.compile(assets)
27
- end
28
- end
29
-
30
- desc 'Remove all assets'
31
- task :clobber do
32
- with_logger do
33
- manifest.clobber
34
- end
35
- end
36
-
37
- desc 'Clean old assets'
38
- task :clean do
39
- with_logger do
40
- manifest.clean(keep)
41
- end
42
- end
43
- end
44
-
45
- end
46
-
47
21
  # Number of old assets to keep.
48
22
 
49
23
  # Logger to use during rake tasks. Defaults to using stderr.
50
- #
51
- # t.logger = Logger.new($stdout)
52
- #
53
24
  attr_accessor :logger
54
25
 
55
26
  # Returns logger level Integer.
@@ -58,10 +29,7 @@ module Rake
58
29
  end
59
30
 
60
31
  # Set logger level with constant or symbol.
61
- #
62
- # t.log_level = Logger::INFO
63
- # t.log_level = :debug
64
- #
32
+ # @param level [Symbol, const] the logger level
65
33
  def log_level=(level)
66
34
  if level.is_a?(Integer)
67
35
  @logger.level = level
@@ -70,6 +38,7 @@ module Rake
70
38
  end
71
39
  end
72
40
 
41
+ # Initialize the rake task-builder
73
42
  def initialize
74
43
  @environment = Flatrack.assets
75
44
  @logger = Logger.new($stderr)
@@ -82,25 +51,24 @@ module Rake
82
51
  define
83
52
  end
84
53
 
54
+ private
55
+
56
+ def define
57
+ instance_eval(&TASKS)
58
+ end
59
+
85
60
  def assets
86
- files = paths.reduce([]) do |ary, path|
87
- ary + Dir[File.join path, '**', '*']
88
- end
89
61
  files.map do |file|
90
- file_basename = File.basename file
91
- parts = file_basename.split('.').size
92
- file = file.split('.').tap(&:pop).join('.') if parts > 2
93
- File.expand_path(file).sub(/(#{environment.paths.join('|')})\//, '')
62
+ Asset.new(environment, file).path
94
63
  end
95
64
  end
96
65
 
97
- # Define tasks
98
- def define
99
- instance_eval(&DEFINED_TASKS)
66
+ def files
67
+ paths.reduce([]) do |ary, path|
68
+ ary + Dir[File.join path, '**', '*']
69
+ end
100
70
  end
101
71
 
102
- private
103
-
104
72
  # Sub out environment logger with our rake task logger that
105
73
  # writes to stderr.
106
74
  def with_logger
@@ -1,4 +1,5 @@
1
1
  class Flatrack
2
+ # Helps define fixtures for specs
2
3
  module FixtureHelper
3
4
  extend FileUtils
4
5
  include FileUtils
@@ -1,4 +1,5 @@
1
1
  class Flatrack
2
+ # Helps to build sample sites for specs
2
3
  module SiteHelper
3
4
  extend FileUtils
4
5
  include FileUtils
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flatrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Waldrip
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-25 00:00:00.000000000 Z
11
+ date: 2014-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -288,6 +288,20 @@ dependencies:
288
288
  - - "~>"
289
289
  - !ruby/object:Gem::Version
290
290
  version: '0.3'
291
+ - !ruby/object:Gem::Dependency
292
+ name: yard
293
+ requirement: !ruby/object:Gem::Requirement
294
+ requirements:
295
+ - - "~>"
296
+ - !ruby/object:Gem::Version
297
+ version: '0.3'
298
+ type: :development
299
+ prerelease: false
300
+ version_requirements: !ruby/object:Gem::Requirement
301
+ requirements:
302
+ - - "~>"
303
+ - !ruby/object:Gem::Version
304
+ version: '0.3'
291
305
  description: |+
292
306
  A rack based static site builder with templates, layouts and project structure
293
307
  based routing.
@@ -345,6 +359,8 @@ files:
345
359
  - lib/flatrack/view/request_helper.rb
346
360
  - lib/flatrack/view/tag_helper.rb
347
361
  - lib/rake/asset_tasks.rb
362
+ - lib/rake/asset_tasks/asset.rb
363
+ - lib/rake/asset_tasks/tasks.rb
348
364
  - public/assets/manifest-6d7e78baa6d2c0670a82be957315fcbc.json
349
365
  - spec/fixtures/assets/sample.css.scss
350
366
  - spec/fixtures/assets/sample.js.coffee