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_url(file = nil) ⇒ Object
- #assets(options = {}) ⇒ Object
- #build(options = {}) ⇒ Object
- #clean ⇒ Object
- #config(options) ⇒ Object
- #create_engine ⇒ Object
- #engine_name ⇒ Object
- #expand_asset_paths ⇒ Object
-
#initialize(options) ⇒ Plugin
constructor
A new instance of Plugin.
- #parent_module ⇒ 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
49 50 51 52 53 |
# File 'lib/cyborg/plugin.rb', line 49 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
120 121 122 123 124 |
# File 'lib/cyborg/plugin.rb', line 120 def asset_path(file=nil) dest = File.join(destination, asset_root) dest = File.join(dest, file) if file dest end |
#asset_url(file = nil) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/cyborg/plugin.rb', line 126 def asset_url(file=nil) path = if Cyborg.production? production_asset_root else asset_root end path = File.join(path, file) if file path end |
#assets(options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cyborg/plugin.rb', line 55 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
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cyborg/plugin.rb', line 72 def build(={}) # TODO: be sure gem builds use a clean asset_path #FileUtils.rm_rf(root) if Cyborg.production? FileUtils.mkdir_p(asset_path) threads = [] assets().each do |asset| threads << Thread.new { asset.build } end threads end |
#clean ⇒ Object
88 89 90 91 92 |
# File 'lib/cyborg/plugin.rb', line 88 def clean FileUtils.rm_rf(Cyborg.rails_path('tmp/cache/')) FileUtils.rm_rf('.sass-cache') FileUtils.rm_rf(Cyborg.rails_path('.sass-cache')) end |
#config(options) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cyborg/plugin.rb', line 94 def config() = { production_asset_root: "/assets/#{name}", asset_root: "/assets/#{name}", destination: "public/", root: @gem.full_gem_path, version: @gem.version.to_s, paths: { stylesheets: "app/assets/stylesheets/#{name}", javascripts: "app/assets/javascripts/#{name}", svgs: "app/assets/svgs/#{name}", } }.merge() .each do |k,v| set_instance(k.to_s,v) end end |
#create_engine ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cyborg/plugin.rb', line 22 def create_engine # 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 initializer "#{name}.static_assets" do |app| app.middleware.insert_before(::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public") end end) 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
113 114 115 116 117 118 |
# File 'lib/cyborg/plugin.rb', line 113 def @paths.each do |type, path| @paths[type] = File.join(root, path) end @destination = File.join(root, @destination) end |
#parent_module ⇒ Object
43 44 45 46 47 |
# File 'lib/cyborg/plugin.rb', line 43 def parent_module mods = self.class.to_s.split('::') mods.pop Object.const_get(mods.join('::')) end |
#svgs? ⇒ Boolean
68 69 70 |
# File 'lib/cyborg/plugin.rb', line 68 def svgs? @svgs.icons.nil? end |
#watch(options) ⇒ Object
84 85 86 |
# File 'lib/cyborg/plugin.rb', line 84 def watch() assets().map(&:watch) end |