Class: Observ::AssetInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/observ/asset_installer.rb

Overview

Main service class for installing Observ assets in a Rails application

Constant Summary collapse

DEFAULT_STYLES_DEST =
"app/javascript/stylesheets/observ"
DEFAULT_JS_DEST =
"app/javascript/controllers/observ"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_root:, app_root:, logger: $stdout) ⇒ AssetInstaller

Returns a new instance of AssetInstaller.

Parameters:

  • gem_root (String, Pathname)

    Root directory of the gem

  • app_root (String, Pathname)

    Root directory of the host application

  • logger (Logger, IO) (defaults to: $stdout)

    Logger for output (defaults to STDOUT)



14
15
16
17
18
# File 'lib/observ/asset_installer.rb', line 14

def initialize(gem_root:, app_root:, logger: $stdout)
  @gem_root = Pathname.new(gem_root)
  @app_root = Pathname.new(app_root)
  @logger = logger
end

Instance Attribute Details

#app_rootObject (readonly)

Returns the value of attribute app_root.



6
7
8
# File 'lib/observ/asset_installer.rb', line 6

def app_root
  @app_root
end

#gem_rootObject (readonly)

Returns the value of attribute gem_root.



6
7
8
# File 'lib/observ/asset_installer.rb', line 6

def gem_root
  @gem_root
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/observ/asset_installer.rb', line 6

def logger
  @logger
end

Instance Method Details

#install(styles_dest: nil, js_dest: nil, generate_index: true) ⇒ Hash

Install assets with custom or default destinations

Parameters:

  • styles_dest (String, nil) (defaults to: nil)

    Custom destination for stylesheets

  • js_dest (String, nil) (defaults to: nil)

    Custom destination for JavaScript controllers

  • generate_index (Boolean) (defaults to: true)

    Whether to generate index files

Returns:

  • (Hash)

    Installation results



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
# File 'lib/observ/asset_installer.rb', line 25

def install(styles_dest: nil, js_dest: nil, generate_index: true)
  styles_dest ||= DEFAULT_STYLES_DEST
  js_dest ||= DEFAULT_JS_DEST

  styles_dest_path = app_root.join(styles_dest)
  js_dest_path = app_root.join(js_dest)

  log_header(styles_dest_path, js_dest_path)

  syncer = AssetSyncer.new(gem_root: gem_root, app_root: app_root, logger: logger)

  # Sync stylesheets
  styles_result = syncer.sync_stylesheets(styles_dest_path)
  log ""

  # Sync JavaScript controllers
  js_result = syncer.sync_javascript_controllers(js_dest_path)
  log ""

  # Check controller registration if requested
  # Note: index.js is already included in the gem's source files and gets synced,
  # so we don't need to generate it - we just check if it's properly registered
  registration_status = nil

  if generate_index
    generator = IndexFileGenerator.new(app_root: app_root, logger: logger)

    log "Checking controller registration..."
    log "-" * 80
    registration_status = generator.check_main_controllers_registration

    if registration_status[:suggestions]
      registration_status[:suggestions].each { |msg| log "  #{msg}" }
    elsif registration_status[:registered]
      log "  ✓ Observ controllers are already registered"
    end
    log ""
  end

  log_footer(styles_dest_path, js_dest_path)

  {
    styles: styles_result,
    javascript: js_result,
    registration: registration_status,
    paths: {
      styles: styles_dest_path,
      javascript: js_dest_path
    }
  }
end

#sync(styles_dest: nil, js_dest: nil) ⇒ Hash

Sync existing assets (update only)

Parameters:

  • styles_dest (String, nil) (defaults to: nil)

    Custom destination for stylesheets

  • js_dest (String, nil) (defaults to: nil)

    Custom destination for JavaScript controllers

Returns:

  • (Hash)

    Sync results



81
82
83
# File 'lib/observ/asset_installer.rb', line 81

def sync(styles_dest: nil, js_dest: nil)
  install(styles_dest: styles_dest, js_dest: js_dest, generate_index: false)
end