Class: Middleman::Presentation::ComponentsManager

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

Overview

Component Manager

It know about all frontend components. Information about all frontend components is used when building ‘application.js` and `application.scss` during website build and when creating the `bower.json`-file on presentation-creation.

It normally gets the information about available components from ‘plugins`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache: Cache.new(store: Set.new)) ⇒ ComponentsManager

Returns a new instance of ComponentsManager.



20
21
22
23
# File 'lib/middleman-presentation-core/components_manager.rb', line 20

def initialize(cache: Cache.new(store: Set.new))
  @cache           = cache
  @bower_directory = nil
end

Instance Attribute Details

#bower_directory=(value) ⇒ Object



30
31
32
33
# File 'lib/middleman-presentation-core/components_manager.rb', line 30

def bower_directory=(value)
  @bower_directory = value
  cache.mark_dirty
end

Instance Method Details

#add(c) ⇒ Object

Add component



67
68
69
70
71
72
73
74
# File 'lib/middleman-presentation-core/components_manager.rb', line 67

def add(c)
  unless c.respond_to? :root_directory=
    Middleman::Presentation.logger.error Middleman::Presentation.t('errors.invalid_component', argument: c)
    return
  end

  cache.add c
end

#available_componentsObject

Return available frontend components



26
27
28
# File 'lib/middleman-presentation-core/components_manager.rb', line 26

def available_components
  components.to_a
end

#componentsObject

Return components

Will used cached results until a new component is added



38
39
40
41
42
# File 'lib/middleman-presentation-core/components_manager.rb', line 38

def components
  cache.each { |c| c.root_directory = bower_directory }

  cache.to_a
end

#each_fetchable_component(&block) ⇒ Object

Iterate over all fetchable components



45
46
47
48
49
50
51
52
53
# File 'lib/middleman-presentation-core/components_manager.rb', line 45

def each_fetchable_component(&block)
  components = self.components.select(&:fetchable?)

  return components.each unless block_given?

  components.each do |c|
    block.call(c, c.equal?(components.last))
  end
end

#each_nonfetchable_component(&block) ⇒ Object

Iterate over all fetchable components



56
57
58
59
60
61
62
63
64
# File 'lib/middleman-presentation-core/components_manager.rb', line 56

def each_nonfetchable_component(&block)
  components = self.components.select { |c| !c.fetchable? }

  return components.each unless block_given?

  components.each do |c|
    block.call(c, c.equal?(components.last))
  end
end

#to_sObject

List installed plugins



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/middleman-presentation-core/components_manager.rb', line 77

def to_s
  data = components.sort.reduce([]) do |a, e|
    a << {
      name: e.name,
      path: e.path,
      base_path: e.base_path,
      resource_locator: e.resource_locator,
      version: e.version,
      loadable_files: e.loadable_files,
      importable_files: e.importable_files,
      ignorable_files: e.ignorable_files,
      output_paths: e.output_paths
    }
  end

  List.new(data).to_s(fields: [:name, :path, :base_path, :resource_locator, :version, :importable_files, :loadable_files, :ignorable_files, :output_paths], vertical: true)
end