machined 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,11 +15,11 @@ module Machined
15
15
  path_to_asset source, options
16
16
  end
17
17
  end
18
-
18
+
19
19
  # Redefine image_path to work with Sprockets::Helpers.
20
20
  def image_path(source, options = {})
21
21
  asset_path source, { :dir => 'images' }.merge(options)
22
22
  end
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -9,7 +9,7 @@ module Machined
9
9
  def locals
10
10
  @locals ||= ActiveSupport::HashWithIndifferentAccess.new
11
11
  end
12
-
12
+
13
13
  # Adds psuedo local variables from the given hash, where
14
14
  # the key is the name of the variable. This is provided so
15
15
  # processors can add local variables without having access
@@ -21,7 +21,7 @@ module Machined
21
21
  self.locals.merge! locals
22
22
  end
23
23
  end
24
-
24
+
25
25
  # Temporarily changes the locals. The given +temporary_locals+
26
26
  # will be merged into the current locals. After the block is
27
27
  # executed, the locals will be restored to their original state.
@@ -31,13 +31,13 @@ module Machined
31
31
  ensure
32
32
  @locals = old_locals
33
33
  end
34
-
34
+
35
35
  # Returns true if the given +name+ has been set as a local
36
36
  # variable.
37
37
  def has_local?(name)
38
38
  locals.key? name
39
39
  end
40
-
40
+
41
41
  def method_missing(method, *args, &block) # :nodoc:
42
42
  if args.empty? && has_local?(method)
43
43
  locals[method]
@@ -45,7 +45,7 @@ module Machined
45
45
  super
46
46
  end
47
47
  end
48
-
48
+
49
49
  def respond_to?(method) # :nodoc:
50
50
  super or has_local?(method)
51
51
  end
@@ -16,9 +16,9 @@ module Machined
16
16
  'Tilt::ErubisTemplate' => :erubis,
17
17
  'Slim::Template' => :slim
18
18
  }
19
-
19
+
20
20
  protected
21
-
21
+
22
22
  # Attempts to return the current engine based on
23
23
  # the processors for this file. This is used by
24
24
  # Padrino's helpers to determine which type of template
@@ -33,7 +33,7 @@ module Machined
33
33
  end
34
34
  end
35
35
  end
36
-
36
+
37
37
  @current_engine
38
38
  end
39
39
  end
@@ -12,7 +12,7 @@ module Machined
12
12
  machined.config.layout
13
13
  end
14
14
  end
15
-
15
+
16
16
  # Returns the local variable, title, if set. Otherwise
17
17
  # return a titleized version of the filename.
18
18
  def title
@@ -22,7 +22,7 @@ module Machined
22
22
  File.basename(logical_path).titleize
23
23
  end
24
24
  end
25
-
25
+
26
26
  # Returns the URL to this asset, appending the sprocket's URL.
27
27
  # For HTML files, this will return pretty URLs.
28
28
  def url
@@ -10,7 +10,7 @@ module Machined
10
10
  # <%= render 'ad', :collection => advertisements %>
11
11
  # # is the same as:
12
12
  # <%= render_collection advertisements, 'ad' %>
13
- #
13
+ #
14
14
  def render(partial, options = {})
15
15
  if collection = options.delete(:collection)
16
16
  render_collection collection, partial, options
@@ -18,7 +18,7 @@ module Machined
18
18
  render_partial partial, options
19
19
  end
20
20
  end
21
-
21
+
22
22
  # Renders the given +collection+ of objects with the given
23
23
  # +partial+ template. This follows the same conventions
24
24
  # of Rails' partial rendering, where the individual objects
@@ -33,7 +33,7 @@ module Machined
33
33
  # template would be fed +ad_counter+.
34
34
  def render_collection(collection, partial, options = {})
35
35
  return if collection.nil? || collection.empty?
36
-
36
+
37
37
  template = resolve_partial partial
38
38
  counter = 0
39
39
  collection.inject('') do |output, object|
@@ -41,7 +41,7 @@ module Machined
41
41
  output << render_partial(template, options.merge(:object => object, :counter => counter))
42
42
  end
43
43
  end
44
-
44
+
45
45
  # Renders a single +partial+. The primary options are:
46
46
  #
47
47
  # * <tt>:locals</tt> - A hash of local variables to use when
@@ -83,12 +83,12 @@ module Machined
83
83
  def render_partial(partial, options = {})
84
84
  template = resolve_partial partial
85
85
  depend_on template
86
-
86
+
87
87
  partial_locals = {}
88
-
88
+
89
89
  # Temporarily use a different layout (default to no layout)
90
90
  partial_locals[:layout] = options.delete(:layout) || false
91
-
91
+
92
92
  # Add object with the name of the partial
93
93
  # as the local variable name.
94
94
  if object = options.delete(:object)
@@ -96,34 +96,34 @@ module Machined
96
96
  partial_locals[object_name] = object
97
97
  partial_locals["#{object_name}_counter"] = options.delete :counter
98
98
  end
99
-
99
+
100
100
  # Add locals from leftover options
101
101
  if leftover_locals = options.delete(:locals) || options
102
102
  partial_locals.merge! leftover_locals
103
103
  end
104
-
104
+
105
105
  # Now evaluate the partial
106
106
  with_locals(partial_locals) { return evaluate template }
107
107
  end
108
-
108
+
109
109
  protected
110
-
110
+
111
111
  # Attempts to find a view with the given path,
112
112
  # while also looking for a version with a partial-style
113
113
  # name (prefixed with an '_').
114
114
  def resolve_partial(path) # :nodoc:
115
115
  path = Pathname.new path
116
116
  path.absolute? and return path
117
-
117
+
118
118
  # First look for the normal path
119
119
  machined.views.resolve(path) { |found| return found }
120
-
120
+
121
121
  # Then look for the partial-style version
122
122
  unless path.basename.to_s =~ /^_/
123
123
  partial = path.dirname.join "_#{path.basename}"
124
124
  machined.views.resolve(partial) { |found| return found }
125
125
  end
126
-
126
+
127
127
  raise Sprockets::FileNotFound, "couldn't find file '#{path}'"
128
128
  end
129
129
  end
@@ -8,15 +8,15 @@ module Machined
8
8
  # A reference to the Machined environment which
9
9
  # created this instance.
10
10
  attr_reader :machined
11
-
11
+
12
12
  # A reference to the configuration.
13
13
  attr_reader :config
14
-
14
+
15
15
  #
16
16
  def initialize(environment)
17
17
  @machined = environment.machined
18
18
  @config = environment.config
19
-
19
+
20
20
  super
21
21
  end
22
22
  end
@@ -12,7 +12,7 @@ module Machined
12
12
  # I don't want to `require 'rails'` if I don't have to.
13
13
  module Initializable
14
14
  extend ActiveSupport::Concern
15
-
15
+
16
16
  class Initializer < Struct.new(:name, :block)
17
17
  # Run's the initializer's +block+ with the given
18
18
  # +context+ and yields the given +args+
@@ -21,14 +21,14 @@ module Machined
21
21
  context.instance_exec(*args, &block)
22
22
  end
23
23
  end
24
-
24
+
25
25
  module ClassMethods
26
26
  # Returns an array of the initializers for
27
27
  # this class.
28
28
  def initializers
29
29
  @initializers ||= []
30
30
  end
31
-
31
+
32
32
  # Creates a new initializer with the given name.
33
33
  # You can optionally pace initializers before or after
34
34
  # other initializers using the `:before` and `:after`
@@ -36,7 +36,7 @@ module Machined
36
36
  # of the list.
37
37
  def initializer(name, options = {}, &block)
38
38
  initializer = Initializer.new(name, block)
39
-
39
+
40
40
  if after = options[:after]
41
41
  initializers.insert initializer_index(after) + 1, initializer
42
42
  elsif before = options[:before]
@@ -45,9 +45,9 @@ module Machined
45
45
  initializers << initializer
46
46
  end
47
47
  end
48
-
48
+
49
49
  protected
50
-
50
+
51
51
  # Returns the index of the initializer with
52
52
  # the given name.
53
53
  def initializer_index(name) # :nodoc:
@@ -56,7 +56,7 @@ module Machined
56
56
  end or raise "The specified initializer, #{name.inspect}, does not exist"
57
57
  end
58
58
  end
59
-
59
+
60
60
  # Run each initializer with the given args
61
61
  # yielded to each initializer's block.
62
62
  def run_initializers(*args)
@@ -15,11 +15,11 @@ module Machined
15
15
  )
16
16
  (.*)\Z # Rest of File
17
17
  /mx
18
-
18
+
19
19
  # See `Tilt::Template#prepare`.
20
20
  def prepare
21
21
  end
22
-
22
+
23
23
  # See `Tilt::Template#evaluate`.
24
24
  def evaluate(context, locals = {}, &block)
25
25
  output = data
@@ -5,14 +5,14 @@ module Machined
5
5
  class LayoutProcessor < Tilt::Template
6
6
  # A reference to the Sprockets context
7
7
  attr_reader :context
8
-
8
+
9
9
  # Path to the layout file
10
10
  attr_reader :layout_path
11
-
11
+
12
12
  # See `Tilt::Template#prepare`.
13
13
  def prepare
14
14
  end
15
-
15
+
16
16
  # See `Tilt::Template#evaluate`.
17
17
  def evaluate(context, locals, &block)
18
18
  @context = context
@@ -23,21 +23,21 @@ module Machined
23
23
  data
24
24
  end
25
25
  end
26
-
26
+
27
27
  protected
28
-
28
+
29
29
  # A reference to the Views sprocket, where the
30
30
  # layout asset will be.
31
31
  def views
32
32
  context.machined.views
33
33
  end
34
-
34
+
35
35
  # Determine if we should attempt to wrap the
36
36
  # content with a layout.
37
37
  def layout?
38
38
  context.layout != false
39
39
  end
40
-
40
+
41
41
  # Attempt to find the layout file in the Views
42
42
  # sprocket.
43
43
  def resolve_layout
@@ -45,14 +45,14 @@ module Machined
45
45
  rescue Sprockets::FileNotFound, Sprockets::ContentTypeMismatch
46
46
  nil
47
47
  end
48
-
48
+
49
49
  # Recreate `Sprockets::Context#evaluate`, because it doesn't
50
50
  # support yielding. I'm not even sure it's necessary to
51
51
  # support multiple processors for a layout, though.
52
52
  def evaluate_layout
53
53
  processors = views.attributes_for(layout_path).processors
54
54
  result = Sprockets::Utils.read_unicode layout_path
55
-
55
+
56
56
  processors.each do |processor|
57
57
  begin
58
58
  template = processor.new(layout_path.to_s) { result }
@@ -62,10 +62,10 @@ module Machined
62
62
  raise
63
63
  end
64
64
  end
65
-
65
+
66
66
  result
67
67
  end
68
68
  end
69
69
  end
70
70
  end
71
-
71
+
@@ -10,14 +10,14 @@ module Machined
10
10
  :assets => false,
11
11
  :compile => true
12
12
  }.freeze
13
-
13
+
14
14
  # A reference to the Machined environment which
15
15
  # created this instance.
16
16
  attr_reader :machined
17
-
17
+
18
18
  # A reference to the configuration.
19
19
  attr_reader :config
20
-
20
+
21
21
  # Creates a new Machined sprocket. The API is
22
22
  # a bit different than `Sprockets::Environment` to
23
23
  # allow for per-Sprockets-environment configuration
@@ -25,12 +25,12 @@ module Machined
25
25
  def initialize(machined, options = {})
26
26
  @machined = machined
27
27
  @config = OpenStruct.new DEFAULT_OPTIONS.dup.merge(options)
28
-
28
+
29
29
  super config.root
30
-
30
+
31
31
  @context_class = Class.new Context
32
32
  end
33
-
33
+
34
34
  # Returns true, if this sprocket should be
35
35
  # compiled. Nine times out of ten, you will want
36
36
  # your sprocket compiled, but sometimes - like
@@ -39,12 +39,12 @@ module Machined
39
39
  def compile?
40
40
  config.compile && config.url
41
41
  end
42
-
42
+
43
43
  # Override to use Machined's Index
44
44
  def index
45
45
  Index.new(self)
46
46
  end
47
-
47
+
48
48
  # Loops through the available Tilt templates
49
49
  # and registers them as processor engines for
50
50
  # Sprockets. By default, Sprockets cherry picks
@@ -5,13 +5,13 @@ module Machined
5
5
  # A reference to the Machined environment which
6
6
  # created this instance.
7
7
  attr_reader :machined
8
-
8
+
9
9
  # Creates a new instance, which will compile
10
10
  # the assets to the given +output_path+.
11
11
  def initialize(machined)
12
12
  @machined = machined
13
13
  end
14
-
14
+
15
15
  # Loop through and compile each available
16
16
  # asset to the appropriate output path.
17
17
  def compile
@@ -21,7 +21,7 @@ module Machined
21
21
  sprocket.each_logical_path do |logical_path|
22
22
  url = File.join(sprocket.config.url, logical_path)
23
23
  next unless compiled_assets[url].nil? && compile?(url)
24
-
24
+
25
25
  if asset = sprocket.find_asset(logical_path)
26
26
  compiled_assets[url] = write_asset(sprocket, asset)
27
27
  end
@@ -29,14 +29,14 @@ module Machined
29
29
  end
30
30
  compiled_assets
31
31
  end
32
-
32
+
33
33
  # Determines if we should precompile the asset
34
34
  # with the given url. By default, we skip over any
35
35
  # files that begin with '_', like partials.
36
36
  def compile?(url)
37
37
  File.basename(url) !~ /^_/
38
38
  end
39
-
39
+
40
40
  # Writes the asset to its destination, also
41
41
  # writing a gzipped version if necessary.
42
42
  def write_asset(environment, asset)
@@ -46,9 +46,9 @@ module Machined
46
46
  asset.write_to "#{filename}.gz" if gzip?(filename)
47
47
  asset.digest
48
48
  end
49
-
49
+
50
50
  protected
51
-
51
+
52
52
  # Gets the full output path for the given asset.
53
53
  # If it's supposed to include a digest, it will return the
54
54
  # digest_path.
@@ -56,13 +56,13 @@ module Machined
56
56
  path = digest?(environment, asset) ? asset.digest_path : asset.logical_path
57
57
  File.join(machined.output_path, environment.config.url, path)
58
58
  end
59
-
59
+
60
60
  # Determines if we should use the digest_path for the given
61
61
  # asset.
62
62
  def digest?(environment, asset) # :nodoc:
63
63
  machined.config.digest_assets && environment.config.assets
64
64
  end
65
-
65
+
66
66
  # Determines if we should gzip the asset.
67
67
  def gzip?(filename) # :nodoc:
68
68
  machined.config.gzip_assets && filename =~ /\.(css|js)$/