Class: Volt::AssetFiles

Inherits:
Object show all
Defined in:
lib/volt/server/rack/asset_files.rb

Instance Method Summary collapse

Constructor Details

#initialize(component_name, component_paths) ⇒ AssetFiles

Returns a new instance of AssetFiles.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/volt/server/rack/asset_files.rb', line 7

def initialize(component_name, component_paths)
  @component_paths     = component_paths
  @assets              = []
  @included_components = {}
  @components          = []
  @disable_auto_import = []
  
  # Include each of the default included components
  Volt.config.default_components.each do |def_comp_name|
    component(def_comp_name)
  end

  component(component_name)
end

Instance Method Details

#add_assets(path) ⇒ Object



92
93
94
95
# File 'lib/volt/server/rack/asset_files.rb', line 92

def add_assets(path)
  asset_folder = File.join(path, 'assets')
  @assets << [:folder, asset_folder] if File.directory?(asset_folder)
end

#component(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/volt/server/rack/asset_files.rb', line 41

def component(name)
  unless @included_components[name]
    # Track that we added
    @included_components[name] = true

    # Get the path to the component
    component_path = @component_paths.component_paths(name)

    unless component_path
      fail "Unable to find component '#{name}', make sure the gem is included in your Gemfile"
    end

    component_path.each do |path|
      # Load the dependencies
      load_dependencies(path, name)

      # Add any assets
      add_assets(path) unless @disable_auto_import.include?(name)
      @components << [path, name]
    end
  end
end

#component_pathsObject



88
89
90
# File 'lib/volt/server/rack/asset_files.rb', line 88

def component_paths
  @components
end

#componentsObject



64
65
66
# File 'lib/volt/server/rack/asset_files.rb', line 64

def components
  @included_components.keys
end

#css_file(locator) ⇒ Object



72
73
74
# File 'lib/volt/server/rack/asset_files.rb', line 72

def css_file(locator)
  @assets << [:css_file, prepare_locator(locator, ['css','scss'])]
end

#css_filesObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/volt/server/rack/asset_files.rb', line 121

def css_files
  css_files = []
  @assets.each do |type, path|
    case type
      when :folder
        # Don't import any css/scss files that start with an underscore, so scss partials
        # aren't imported by default:
        #  http://sass-lang.com/guide
        css_files += Dir["#{path}/**/[^_]*.{css,scss}"].sort.map do |folder|
          '/assets' + folder[path.size..-1].gsub(/[.]scss$/, '')
        end
      when :css_file
        css_files << path
    end
  end

  css_files.uniq
end

#disable_auto_importObject



22
23
24
# File 'lib/volt/server/rack/asset_files.rb', line 22

def disable_auto_import
  @disable_auto_import.push(*@current_component).uniq
end

#javascript_file(locator) ⇒ Object



68
69
70
# File 'lib/volt/server/rack/asset_files.rb', line 68

def javascript_file(locator)
  @assets << [:javascript_file, prepare_locator(locator, ['js'])]
end

#javascript_files(opal_files) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/volt/server/rack/asset_files.rb', line 97

def javascript_files(opal_files)
  javascript_files = []
  @assets.each do |type, path|
    case type
      when :folder
        javascript_files += Dir["#{path}/**/*.js"].sort.map { |folder| '/assets' + folder[path.size..-1] }
      when :javascript_file
        javascript_files << path
    end
  end

  opal_js_files = []
  if Volt.source_maps?
    opal_js_files += opal_files.environment['volt/volt/app'].to_a.map { |v| '/assets/' + v.logical_path + '?body=1' }
  else
    opal_js_files << '/assets/volt/volt/app.js'
  end
  opal_js_files << '/components/main.js'

  javascript_files += opal_js_files

  javascript_files.uniq
end

#load_dependencies(path, component_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/volt/server/rack/asset_files.rb', line 26

def load_dependencies(path, component_name)
  if path
    dependencies_file = File.join(path, 'config/dependencies.rb')
  else
    fail "Unable to find component #{component_name.inspect}"
  end

  if File.exist?(dependencies_file)
    # Run the dependencies file in this asset files context
    code = File.read(dependencies_file)
    @current_component = component_name
    instance_eval(code, dependencies_file, 0)
  end
end

#prepare_locator(locator, valid_extensions) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/volt/server/rack/asset_files.rb', line 76

def prepare_locator(locator, valid_extensions)
  unless url_or_path?(locator)
    locator = File.join('/assets', @current_component, '/assets', valid_extensions.first, "#{locator}")
    locator += '.css' unless locator =~ /^.*\.(#{valid_extensions.join('|')})$/
  end
  locator
end

#url_or_path?(url) ⇒ Boolean

Returns:



84
85
86
# File 'lib/volt/server/rack/asset_files.rb', line 84

def url_or_path?(url)
  (url =~ URI::regexp || url =~ /^\/(\/)?.*/) ? true : false
end