Class: Middleman::Presentation::FrontendComponent

Inherits:
Component
  • Object
show all
Defined in:
lib/middleman-presentation-core/frontend_component.rb

Overview

A frontend component

It represents a bower component. A bower component can contain JavaScript-files, stylesheets, images, fonts, … . A frontend component know where to get a bower component from, which version of the component is required and which JavaScript-files and stylesheets should be includes in “javascripts/application.js” and “stylesheets”application.scss“.

Instance Attribute Summary collapse

Attributes inherited from Component

#ignorable_files, #importable_files, #loadable_files, #name, #output_paths, #resource_locator, #version

Instance Method Summary collapse

Methods inherited from Component

#<=>

Constructor Details

#initialize(**args) ⇒ FrontendComponent

Create new frontend component

Parameters:

  • root_directory (String)

    The directory where all frontend components can be found

See Also:

  • For all other params


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/middleman-presentation-core/frontend_component.rb', line 20

def initialize(**args)
  super(**args)

  @resource_locator = if @resource_locator =~ /\A#{URI.regexp}\z/
                        Addressable::URI.heuristic_parse @resource_locator
                      elsif @github
                        Addressable::URI.heuristic_parse format('https://github.com/%s.git', @github)
                      elsif version
                        Class.new do
                          attr_reader :to_s

                          def initialize(value)
                            @to_s = value
                          end
                        end.new(version)
                      end
  fail ArgumentError, Middleman::Presentation.t('errors.undefined_arguments', arguments: %w(resource_locator github version).to_list) if @resource_locator.blank?

  @name = if version
            name
          elsif name.blank?
            File.basename(@resource_locator.path)
          else
            name
          end

  fail ArgumentError, Middleman::Presentation.t('errors.argument_error', argument: :name, value: @name) if @name.blank?

  @root_directory = root_directory
end

Instance Attribute Details

#root_directoryObject

Returns the value of attribute root_directory.



12
13
14
# File 'lib/middleman-presentation-core/frontend_component.rb', line 12

def root_directory
  @root_directory
end

Instance Method Details

#base_pathObject

Base path for component



63
64
65
# File 'lib/middleman-presentation-core/frontend_component.rb', line 63

def base_path
  path.dirname
end

#fetchable?Boolean

Should middleman-presentation fetch the component

Returns:

  • (Boolean)


68
69
70
# File 'lib/middleman-presentation-core/frontend_component.rb', line 68

def fetchable?
  true
end

#pathObject

Return path to component

If root directory is set, it will be prepended to name.



54
55
56
57
58
59
60
# File 'lib/middleman-presentation-core/frontend_component.rb', line 54

def path
  args = []
  args << root_directory if root_directory
  args << name.to_s

  Pathname.new(File.join(*args))
end