lib/lookbook/engine.rb in lookbook-1.3.4 vs lib/lookbook/engine.rb in lookbook-1.4.0

- old
+ new

@@ -40,19 +40,20 @@ end end initializer "lookbook.file_watcher.pages" do file_watcher.watch(opts.page_paths, opts.page_extensions) do |changes| - self.class.websocket.broadcast(:reload) + Engine.pages.load(Engine.page_paths) + Engine.websocket.broadcast(:reload) run_hooks(:after_change, changes) end end - initializer "lookbook.parser.preview_load_callback" do - parser.after_parse do |registry| - Preview.load!(registry.all(:class)) - self.class.websocket.broadcast(:reload) + initializer "lookbook.parser.previews_load_callback" do + parser.after_parse do |code_objects| + Engine.previews.load(code_objects.all(:class)) + Engine.websocket.broadcast(:reload) end end # The preview controller handles the rendering of individual previews. # @@ -72,82 +73,103 @@ file_watcher.start if listen? end end config.after_initialize do + Engine.pages.load(Engine.page_paths) parser.parse { run_hooks(:after_initialize) } end def opts Lookbook.config end def run_hooks(event_name, *args) - self.class.hooks.for_event(event_name).each do |hook| + Engine.hooks.for_event(event_name).each do |hook| hook.call(Lookbook, *args) end end def parser - @parser ||= PreviewParser.new(opts.preview_paths, Engine.tags) + @_parser ||= PreviewParser.new(opts.preview_paths, Engine.tags) end def file_watcher - @file_watcher ||= FileWatcher.new(force_polling: opts.listen_use_polling) + @_file_watcher ||= FileWatcher.new(force_polling: opts.listen_use_polling) end def process - @process ||= Process.new(env: Rails.env) + @_process ||= Process.new(env: Rails.env) end def listen? opts.listen && process.supports_listening? end - def self.mount_path - routes.find_script_name({}) - end + class << self + def mount_path + routes.find_script_name({}) + end - def self.mounted? - mount_path.present? - end + def mounted? + mount_path.present? + end - def self.app_name - name = if Rails.application.class.respond_to?(:module_parent_name) - Rails.application.class.module_parent_name - else - Rails.application.class.parent_name + def app_name + name = if Rails.application.class.respond_to?(:module_parent_name) + Rails.application.class.module_parent_name + else + Rails.application.class.parent_name + end + name.underscore end - name.underscore - end - def self.websocket - if mounted? - use_websocket = opts.auto_refresh && opts.listen && process.supports_listening? - @websocket ||= use_websocket ? Websocket.new(mount_path, logger: Lookbook.logger) : Websocket.noop - else - Websocket.noop + def websocket + if mounted? + use_websocket = opts.auto_refresh && opts.listen && process.supports_listening? + @websocket ||= use_websocket ? Websocket.new(mount_path, logger: Lookbook.logger) : Websocket.noop + else + Websocket.noop + end end - end - def self.panels - @panels ||= PanelStore.init_from_config - end + def panels + @_panels ||= PanelStore.init_from_config + end - def self.inputs - @inputs ||= InputStore.init_from_config - end + def inputs + @_inputs ||= InputStore.init_from_config + end - def self.tags - @tags ||= TagStore.init_from_config - end + def tags + @_tags ||= TagStore.init_from_config + end - def self.hooks - @hooks ||= HookStore.init_from_config - end + def hooks + @_hooks ||= HookStore.init_from_config + end - def self.preview_controller - @preview_controller + def component_paths + @_component_paths ||= Array(PathUtils.to_absolute(opts.components_path)) + end + + def page_paths + @_page_paths ||= PathUtils.normalize_paths(opts.page_paths) + end + + def preview_paths + @_preview_paths ||= PathUtils.normalize_paths(opts.preview_paths) + end + + def pages + @_pages ||= PageCollection.new + end + + def previews + @_previews ||= PreviewCollection.new + end + + attr_reader :preview_controller end at_exit do file_watcher.stop run_hooks(:before_exit)