Class: SparkEngine::Plugin
- Inherits:
-
Object
- Object
- SparkEngine::Plugin
- Defined in:
- lib/spark_engine/plugin.rb
Instance Attribute Summary collapse
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#gem ⇒ Object
readonly
Returns the value of attribute gem.
-
#javascripts ⇒ Object
readonly
Returns the value of attribute javascripts.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stylesheets ⇒ Object
readonly
Returns the value of attribute stylesheets.
-
#svgs ⇒ Object
readonly
Returns the value of attribute svgs.
Instance Method Summary collapse
- #add_assets ⇒ Object
- #asset_path(file = nil) ⇒ Object
- #asset_root ⇒ Object
- #asset_url(file = nil) ⇒ Object
- #assets(options = {}) ⇒ Object
- #build(options = {}) ⇒ Object
- #config(options) ⇒ Object
-
#create_engine(&block) ⇒ Object
Create a new Rails::Engine.
- #debug? ⇒ Boolean
- #engine_name ⇒ Object
- #expand_asset_paths ⇒ Object
-
#initialize(options) ⇒ Plugin
constructor
A new instance of Plugin.
- #parent_module ⇒ Object
- #production_root ⇒ Object
- #svgs? ⇒ Boolean
- #watch(options) ⇒ Object
Constructor Details
#initialize(options) ⇒ Plugin
Returns a new instance of Plugin.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/spark_engine/plugin.rb', line 6 def initialize() @name = .delete(:engine).downcase @gem = Gem.loaded_specs[.delete(:gem)] config() # 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 |
Instance Attribute Details
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
3 4 5 |
# File 'lib/spark_engine/plugin.rb', line 3 def destination @destination end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
3 4 5 |
# File 'lib/spark_engine/plugin.rb', line 3 def engine @engine end |
#gem ⇒ Object (readonly)
Returns the value of attribute gem.
3 4 5 |
# File 'lib/spark_engine/plugin.rb', line 3 def gem @gem end |
#javascripts ⇒ Object (readonly)
Returns the value of attribute javascripts.
3 4 5 |
# File 'lib/spark_engine/plugin.rb', line 3 def javascripts @javascripts end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/spark_engine/plugin.rb', line 3 def name @name end |
#stylesheets ⇒ Object (readonly)
Returns the value of attribute stylesheets.
3 4 5 |
# File 'lib/spark_engine/plugin.rb', line 3 def stylesheets @stylesheets end |
#svgs ⇒ Object (readonly)
Returns the value of attribute svgs.
3 4 5 |
# File 'lib/spark_engine/plugin.rb', line 3 def svgs @svgs end |
Instance Method Details
#add_assets ⇒ Object
89 90 91 92 93 |
# 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) ⇒ Object
165 166 167 168 169 |
# 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 ⇒ Object
127 128 129 130 |
# 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) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# 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 = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/spark_engine/plugin.rb', line 95 def assets(={}) assets = [] if [:select_assets] assets.push @svgs if [:svg] assets.push @stylesheets if [:css] assets.push @javascripts if [:js] else assets = [@svgs, @stylesheets, @javascripts] end assets end |
#build(options = {}) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/spark_engine/plugin.rb', line 112 def build(={}) FileUtils.mkdir_p(asset_path) assets().each do |asset| asset.build end end |
#config(options) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/spark_engine/plugin.rb', line 136 def config() = { production_asset_root: nil, destination: "public/", root: @gem.full_gem_path, version: @gem.version.to_s, gem_name: @gem.name, paths: { stylesheets: "app/assets/stylesheets/#{name}", javascripts: "app/assets/javascripts/#{name}", components: "app/components/#{name}", images: "app/assets/images/#{name}", svgs: "app/assets/svgs/#{name}", } }.merge() .each do |k,v| set_instance(k.to_s,v) end end |
#create_engine(&block) ⇒ Object
Create a new Rails::Engine
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 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 |
# 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? ⇒ Boolean
119 120 121 |
# File 'lib/spark_engine/plugin.rb', line 119 def debug? ENV['SPARK_DEBUG'] end |
#engine_name ⇒ Object
18 19 20 |
# File 'lib/spark_engine/plugin.rb', line 18 def engine_name @engine.name.sub(/::Engine/,'') end |
#expand_asset_paths ⇒ Object
158 159 160 161 162 163 |
# File 'lib/spark_engine/plugin.rb', line 158 def @paths.each do |type, path| @paths[type] = File.join(root, path) end @destination = File.join(root, @destination) end |
#parent_module ⇒ Object
83 84 85 86 87 |
# 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 ⇒ Object
132 133 134 |
# File 'lib/spark_engine/plugin.rb', line 132 def production_root @production_asset_root ||= asset_root end |
#svgs? ⇒ Boolean
108 109 110 |
# File 'lib/spark_engine/plugin.rb', line 108 def svgs? @svgs.icons.nil? end |
#watch(options) ⇒ Object
123 124 125 |
# File 'lib/spark_engine/plugin.rb', line 123 def watch() assets().map(&:watch) end |