Class: Lookbook::Engine
- Inherits:
-
Rails::Engine
- Object
- Rails::Engine
- Lookbook::Engine
- Defined in:
- lib/lookbook/engine.rb
Class Attribute Summary collapse
-
.preview_controller ⇒ Object
readonly
Returns the value of attribute preview_controller.
Class Method Summary collapse
- .app_name ⇒ Object
- .init_listeners ⇒ Object
- .listeners ⇒ Object
- .log_level ⇒ Object
- .mounted_path ⇒ Object
- .parser ⇒ Object
- .prevent_listening? ⇒ Boolean
- .register_listener(listener) ⇒ Object
- .reload_ui ⇒ Object
- .run_hooks(event_name, *args) ⇒ Object
- .running_in_rake_task? ⇒ Boolean
- .stop_listeners ⇒ Object
- .websocket ⇒ Object
- .websocket? ⇒ Boolean
- .websocket_mount_path ⇒ Object
Class Attribute Details
.preview_controller ⇒ Object (readonly)
Returns the value of attribute preview_controller.
240 241 242 |
# File 'lib/lookbook/engine.rb', line 240 def preview_controller @preview_controller end |
Class Method Details
.app_name ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/lookbook/engine.rb', line 196 def app_name name = if Gem::Version.new(Rails.version) >= Gem::Version.new("6.1") Rails.application.class.module_parent_name else Rails.application.class.parent_name end name.underscore end |
.init_listeners ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lookbook/engine.rb', line 125 def init_listeners config = Lookbook.config return unless config.listen == true Listen.logger = Lookbook.logger listen_paths = config.listen_paths.uniq if listen_paths.any? preview_listener = Listen.to(*listen_paths, only: /\.(#{config.listen_extensions.join("|")})$/, force_polling: config.listen_use_polling) do |modified, added, removed| parser.parse do run_hooks(:after_change, {modified: modified, added: added, removed: removed}) end end register_listener(preview_listener) end page_paths = config.page_paths.uniq if page_paths.any? page_listener = Listen.to(*page_paths, only: /\.(html.*|md.*)$/, force_polling: config.listen_use_polling) do |modified, added, removed| changes = {modified: modified, added: added, removed: removed} reload_ui run_hooks(:after_change, changes) end register_listener(page_listener) end end |
.listeners ⇒ Object
210 211 212 |
# File 'lib/lookbook/engine.rb', line 210 def listeners @listeners ||= [] end |
.log_level ⇒ Object
192 193 194 |
# File 'lib/lookbook/engine.rb', line 192 def log_level Lookbook.logger.level end |
.mounted_path ⇒ Object
184 185 186 |
# File 'lib/lookbook/engine.rb', line 184 def mounted_path Lookbook::Engine.routes.find_script_name({}) end |
.parser ⇒ Object
188 189 190 |
# File 'lib/lookbook/engine.rb', line 188 def parser @parser ||= Lookbook::Parser.new(config.lookbook.preview_paths) end |
.prevent_listening? ⇒ Boolean
228 229 230 |
# File 'lib/lookbook/engine.rb', line 228 def prevent_listening? Rails.env.test? || running_in_rake_task? end |
.register_listener(listener) ⇒ Object
205 206 207 208 |
# File 'lib/lookbook/engine.rb', line 205 def register_listener(listener) listener.start listeners << listener end |
.reload_ui ⇒ Object
224 225 226 |
# File 'lib/lookbook/engine.rb', line 224 def reload_ui websocket&.broadcast("reload", {}) end |
.run_hooks(event_name, *args) ⇒ Object
218 219 220 221 222 |
# File 'lib/lookbook/engine.rb', line 218 def run_hooks(event_name, *args) config.lookbook.hooks[event_name].each do |hook| hook.call(Lookbook, *args) end end |
.running_in_rake_task? ⇒ Boolean
232 233 234 235 236 237 238 |
# File 'lib/lookbook/engine.rb', line 232 def running_in_rake_task? if defined?(Rake) && Rake.respond_to?(:application) File.basename($0) == "rake" || Rake.application.top_level_tasks.any? else false end end |
.stop_listeners ⇒ Object
214 215 216 |
# File 'lib/lookbook/engine.rb', line 214 def stop_listeners listeners.each { |listener| listener.stop } end |
.websocket ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/lookbook/engine.rb', line 155 def websocket config = Lookbook.config return @websocket unless @websocket.nil? return unless config.auto_refresh == true && config.listen == true && !Rails.env.test? Lookbook.logger.info "Initializing websocket" cable = ActionCable::Server::Configuration.new cable.cable = {adapter: "async"}.with_indifferent_access cable.mount_path = nil cable.connection_class = -> { Lookbook::Connection } cable.logger = config.cable_logger @websocket ||= if Gem::Version.new(Rails.version) >= Gem::Version.new(6.0) ActionCable::Server::Base.new(config: cable) else ws = ActionCable::Server::Base.new ws.config = cable ws end end |
.websocket? ⇒ Boolean
180 181 182 |
# File 'lib/lookbook/engine.rb', line 180 def websocket? websocket.present? end |
.websocket_mount_path ⇒ Object
176 177 178 |
# File 'lib/lookbook/engine.rb', line 176 def websocket_mount_path "#{mounted_path}#{config.lookbook.cable_mount_path}".gsub("//", "/") if websocket? end |