Class: Cyborg::Plugin
- Inherits:
-
Object
- Object
- Cyborg::Plugin
- Defined in:
- lib/cyborg/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
- #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/cyborg/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(:@cyborg_plugin_name, name) add_assets end |
Instance Attribute Details
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
3 4 5 |
# File 'lib/cyborg/plugin.rb', line 3 def destination @destination end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
3 4 5 |
# File 'lib/cyborg/plugin.rb', line 3 def engine @engine end |
#gem ⇒ Object (readonly)
Returns the value of attribute gem.
3 4 5 |
# File 'lib/cyborg/plugin.rb', line 3 def gem @gem end |
#javascripts ⇒ Object (readonly)
Returns the value of attribute javascripts.
3 4 5 |
# File 'lib/cyborg/plugin.rb', line 3 def javascripts @javascripts end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/cyborg/plugin.rb', line 3 def name @name end |
#stylesheets ⇒ Object (readonly)
Returns the value of attribute stylesheets.
3 4 5 |
# File 'lib/cyborg/plugin.rb', line 3 def stylesheets @stylesheets end |
#svgs ⇒ Object (readonly)
Returns the value of attribute svgs.
3 4 5 |
# File 'lib/cyborg/plugin.rb', line 3 def svgs @svgs end |
Instance Method Details
#add_assets ⇒ Object
57 58 59 60 61 |
# File 'lib/cyborg/plugin.rb', line 57 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
129 130 131 132 133 |
# File 'lib/cyborg/plugin.rb', line 129 def asset_path(file=nil) dest = destination dest = File.join(dest, file) if file dest end |
#asset_root ⇒ Object
92 93 94 95 |
# File 'lib/cyborg/plugin.rb', line 92 def asset_root asset_prefix = Rails.application.config.assets.prefix || '/assets' File.join asset_prefix, name end |
#asset_url(file = nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cyborg/plugin.rb', line 135 def asset_url(file=nil) path = if Cyborg.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
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cyborg/plugin.rb', line 63 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
80 81 82 83 84 85 86 |
# File 'lib/cyborg/plugin.rb', line 80 def build(={}) FileUtils.mkdir_p(asset_path) assets().each do |asset| asset.build end end |
#config(options) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cyborg/plugin.rb', line 101 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}", 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
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/cyborg/plugin.rb', line 22 def create_engine(&block) # Create a new Rails::Engine @engine = parent_module.const_set('Engine', Class.new(Rails::Engine) do def cyborg_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(cyborg_plugin_path) end engine_name Cyborg.plugin.name require 'cyborg/middleware' initializer "#{name}.static_assets" do |app| if !Cyborg.rails5? || app.config.public_file_server.enabled app.middleware.insert_after ::ActionDispatch::Static, Cyborg::StaticAssets, "#{root}/public", engine_name: Cyborg.plugin.name app.middleware.insert_before ::ActionDispatch::Static, Rack::Deflater end end end) @engine.instance_eval(&block) if block_given? end |
#engine_name ⇒ Object
18 19 20 |
# File 'lib/cyborg/plugin.rb', line 18 def engine_name @engine.name.sub(/::Engine/,'') end |
#expand_asset_paths ⇒ Object
122 123 124 125 126 127 |
# File 'lib/cyborg/plugin.rb', line 122 def @paths.each do |type, path| @paths[type] = File.join(root, path) end @destination = File.join(root, @destination) end |
#parent_module ⇒ Object
51 52 53 54 55 |
# File 'lib/cyborg/plugin.rb', line 51 def parent_module mods = self.class.to_s.split('::') mods.pop Object.const_get(mods.join('::')) end |
#production_root ⇒ Object
97 98 99 |
# File 'lib/cyborg/plugin.rb', line 97 def production_root @production_asset_root ||= asset_root end |
#svgs? ⇒ Boolean
76 77 78 |
# File 'lib/cyborg/plugin.rb', line 76 def svgs? @svgs.icons.nil? end |
#watch(options) ⇒ Object
88 89 90 |
# File 'lib/cyborg/plugin.rb', line 88 def watch() assets().map(&:watch) end |