Class: Middleman::Sprockets::Extension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-sprockets/extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ Extension

Returns a new instance of Extension.



24
25
26
27
28
29
30
31
32
# File 'lib/middleman-sprockets/extension.rb', line 24

def initialize app, options_hash={}, &block
  super

  @resources   = ResourceStore.new
  @environment = ::Sprockets::Environment.new
  @interface   = Interface.new options, @environment

  use_sassc_if_available
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



12
13
14
# File 'lib/middleman-sprockets/extension.rb', line 12

def environment
  @environment
end

#interfaceObject (readonly)

Returns the value of attribute interface.



12
13
14
# File 'lib/middleman-sprockets/extension.rb', line 12

def interface
  @interface
end

#resourcesObject (readonly)

Returns the value of attribute resources.



12
13
14
# File 'lib/middleman-sprockets/extension.rb', line 12

def resources
  @resources
end

Instance Method Details

#after_configurationObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/middleman-sprockets/extension.rb', line 35

def after_configuration
  @environment.append_path((app.source_dir + app.config[:js_dir]).to_s)
  @environment.append_path((app.source_dir + app.config[:css_dir]).to_s)
  @environment.append_path(app.source_dir.to_s)

  append_paths_from_gems

  the_app = app
  the_env = environment

  @environment.context_class.send(:define_method, :app)  { the_app }
  @environment.context_class.send(:define_method, :data) { the_app.data }
  @environment.context_class.send(:define_method, :env)  { the_env }

  @environment.context_class.class_eval do
    def current_resource
      app.extensions[:sprockets].resources.find_by_path(filename)
    end

    def current_path
      current_resource.destination_path if current_resource
    end

    def asset_path path, options={}
      # Handle people calling with the Middleman/Padrino asset path signature
      if path.is_a?(::Symbol) && !options.is_a?(::Hash)
        kind = path
        path = options
      else
        kind = {
          image: :images,
          font: :fonts,
          javascript: :js,
          stylesheet: :css
        }.fetch(options[:type], options[:type])
      end

      if File.extname(path).empty?
        path << { js: '.js', css: '.css' }.fetch(kind, '')
      end

      if app.extensions[:sprockets].check_asset(path)
        link_asset(path)
        app.extensions[:sprockets].sprockets_asset_path(env[path]).sub(/^\/?/, '/')
      else
        app.asset_path(kind, path)
      end
    end
  end

  expose_app_helpers_to_sprockets! if options[:expose_middleman_helpers]
end

#check_asset(path) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/middleman-sprockets/extension.rb', line 112

def check_asset path
  if environment[path]
    true
  else
    false
  end
end

#manipulate_resource_list(resources) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/middleman-sprockets/extension.rb', line 89

def manipulate_resource_list resources
  sprockets_resources, base_resources = resources.partition(&method(:processible?))
  ::Middleman::Util.instrument 'sprockets', name: 'manipulator.sprockets_resources' do
    sprockets_resources.map!(&method(:process_sprockets_resource))
  end

  ::Middleman::Util.instrument 'sprockets', name: 'manipulator.ignore_resources' do
    all_resources = base_resources + sprockets_resources + linked_resources!.to_a

    if app.extensions[:sitemap_ignore].respond_to?(:manipulate_resource_list)
      app.extensions[:sitemap_ignore].manipulate_resource_list all_resources
    else
      all_resources
    end
  end
end

#processible?(r) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/middleman-sprockets/extension.rb', line 107

def processible? r
  !r.is_a?(Resource) && !r.file_descriptor.nil? && interface.processible?(r.source_file)
end

#sprockets_asset_path(sprockets_asset) ⇒ Object



121
122
123
# File 'lib/middleman-sprockets/extension.rb', line 121

def sprockets_asset_path sprockets_asset
  File.join(options[:imported_asset_path], sprockets_asset.logical_path)
end