class SparkEngine::Plugin

Attributes

destination[R]
engine[R]
gem[R]
javascripts[R]
name[R]
stylesheets[R]
svgs[R]

Public Class Methods

new(options) click to toggle source
# File lib/spark_engine/plugin.rb, line 6
def initialize(options)
  @name            = options.delete(:engine).downcase
  @gem             = Gem.loaded_specs[options.delete(:gem)]
  config(options)
  expand_asset_paths

  # Store the gem path for access later when overriding root
  parent_module.instance_variable_set(:@gem_path, root)
  parent_module.instance_variable_set(:@spark_plugin_name, name)
  add_assets
end

Public Instance Methods

add_assets() click to toggle source
# File lib/spark_engine/plugin.rb, line 89
def add_assets
  @javascripts = Assets::Javascripts.new(self, paths[:javascripts])
  @stylesheets = Assets::Stylesheets.new(self, paths[:stylesheets])
  @svgs        = Assets::Svgs.new(self, paths[:svgs])
end
asset_path(file=nil) click to toggle source
# File lib/spark_engine/plugin.rb, line 165
def asset_path(file=nil)
  dest = destination
  dest = File.join(dest, file) if file
  dest
end
asset_root() click to toggle source
# File lib/spark_engine/plugin.rb, line 127
def asset_root
  asset_prefix = Rails.application.config.assets.prefix || '/assets'
  File.join asset_prefix, name
end
asset_url(file=nil) click to toggle source
# File lib/spark_engine/plugin.rb, line 171
def asset_url(file=nil)

  path = if SparkEngine.production? && !ENV[name.upcase + '_FORCE_LOCAL_ASSETS']
    production_root
  else
    asset_root
  end

  path = File.join(path, file) if file
  path
end
assets(options={}) click to toggle source
# File lib/spark_engine/plugin.rb, line 95
def assets(options={})
  assets = []
  if options[:select_assets]
    assets.push @svgs if options[:svg]
    assets.push @stylesheets if options[:css]
    assets.push @javascripts if options[:js]
  else
    assets = [@svgs, @stylesheets, @javascripts]
  end

  assets
end
build(options={}) click to toggle source
# File lib/spark_engine/plugin.rb, line 112
def build(options={})
  FileUtils.mkdir_p(asset_path)
  assets(options).each do |asset|
    asset.build
  end
end
config() click to toggle source
# File lib/spark_engine/plugin.rb, line 31
def config
  @config ||= Rails::Engine::Configuration.new(spark_plugin_path)
end
create_engine(&block) click to toggle source

Create a new Rails::Engine

# File lib/spark_engine/plugin.rb, line 23
def create_engine(&block)
  @engine = parent_module.const_set('Engine', Class.new(Rails::Engine) do

    def spark_plugin_path
      parent = Object.const_get(self.class.name.sub(/::Engine/,''))
      Pathname.new parent.instance_variable_get("@gem_path")
    end

    def config
      @config ||= Rails::Engine::Configuration.new(spark_plugin_path)
    end

    engine_name SparkEngine.plugin.name

    require 'spark_engine/middleware'

    # Ensure compiled assets in /public are served
    initializer "#{name}.static_assets" do |app|
      if app.config.public_file_server.enabled
        app.middleware.insert_after ::ActionDispatch::Static, SparkEngine::StaticAssets, "#{root}/public", engine_name: SparkEngine.plugin.name
        app.middleware.insert_before ::ActionDispatch::Static, Rack::Deflater
      end
    end

    initializer "#{name}.view_paths" do |app|
      # Ensure Components are readable from engine paths
      ActiveSupport.on_load :action_controller do
        append_view_path "#{SparkEngine.plugin.paths[:components]}"
      end

    end

    initializer "#{name}.asset_paths" do |app|
      app.config.assets.paths << SparkEngine.plugin.paths[:components]
    end

  end)

  # Autoload engine lib and components path
  @engine.config.autoload_paths.concat [
    File.join(@engine.spark_plugin_path, "lib"),
    SparkEngine.plugin.paths[:components]
  ]

  @engine.config.after_initialize do |app|
    if defined?(SassC) && defined?(SassC::Rails)
      # Inject Sass importer for yaml files
      require "spark_engine/sassc/importer"
      SassC::Rails::Importer::EXTENSIONS << SassC::SparkEngine::Importer::SassYamlExtension.new
    elsif defined?(Sass)
      # Overwrite Sass engine with Yaml support
      require "spark_engine/sass/engine"
    end
  end

  # Takes a block passed an evaluates it in the context of a Rails engine
  # This allows plugins to modify engines when created.
  @engine.instance_eval(&block) if block_given?
end
debug?() click to toggle source
# File lib/spark_engine/plugin.rb, line 119
def debug?
  ENV['SPARK_DEBUG']
end
engine_name() click to toggle source
# File lib/spark_engine/plugin.rb, line 18
def engine_name
  @engine.name.sub(/::Engine/,'')
end
expand_asset_paths() click to toggle source
# File lib/spark_engine/plugin.rb, line 158
def expand_asset_paths
  @paths.each do |type, path|
    @paths[type] = File.join(root, path)
  end
  @destination = File.join(root, @destination)
end
parent_module() click to toggle source
# File lib/spark_engine/plugin.rb, line 83
def parent_module
  mods = self.class.to_s.split('::')
  mods.pop
  Object.const_get(mods.join('::'))
end
production_root() click to toggle source
# File lib/spark_engine/plugin.rb, line 132
def production_root
  @production_asset_root ||= asset_root
end
spark_plugin_path() click to toggle source
# File lib/spark_engine/plugin.rb, line 26
def spark_plugin_path
  parent = Object.const_get(self.class.name.sub(/::Engine/,''))
  Pathname.new parent.instance_variable_get("@gem_path")
end
svgs?() click to toggle source
# File lib/spark_engine/plugin.rb, line 108
def svgs?
  @svgs.icons.nil?
end
watch(options) click to toggle source
# File lib/spark_engine/plugin.rb, line 123
def watch(options)
  assets(options).map(&:watch)
end

Private Instance Methods

add_files(klass) click to toggle source

Find files based on class type and return an array of Classes for each file

# File lib/spark_engine/plugin.rb, line 191
def add_files(klass)
  ext = asset_ext klass
  find_files(ext).map do |path|
    klass.new(self, path)
  end
end
asset_ext(klass) click to toggle source
# File lib/spark_engine/plugin.rb, line 185
def asset_ext(klass)
  klass.name.split('::').last.downcase
end
asset_glob(type) click to toggle source
# File lib/spark_engine/plugin.rb, line 206
def asset_glob(type)
  case type
  when "sass"
    "*.s[ca]ss"
  else
    "*.#{type}"
  end
end
find_files(ext) click to toggle source

Find files by class type and extension

# File lib/spark_engine/plugin.rb, line 199
def find_files(ext)
  files = Dir[File.join(paths[ext.to_sym], asset_glob(ext))]

  # Filter out partials
  files.reject { |f| File.basename(f).start_with?('_') }
end
set_instance(name, value) click to toggle source

Convert configuration into instance variables

# File lib/spark_engine/plugin.rb, line 216
    def set_instance(name, value)
      instance_variable_set("@#{name}", value)

      instance_eval(<<-EOS, __FILE__, __LINE__ + 1)
        def #{name}
          @#{name}
        end
      EOS
    end