Class: FlashUnified::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/flash_unified/installer.rb

Overview

Pure-Ruby installer logic extracted from the generator so it can be unit-tested without loading Rails. Responsible for copying javascript, view partials and locale files from the gem source into a target app.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_root: nil, target_root:, force: false) ⇒ Installer

Returns a new instance of Installer.



11
12
13
14
15
16
# File 'lib/flash_unified/installer.rb', line 11

def initialize(source_root: nil, target_root:, force: false)
  # When source_root is not given, assume the gem root two levels up from this file (lib/flash_unified)
  @source_root = Pathname.new(source_root || File.expand_path('../..', __dir__))
  @target_root = Pathname.new(target_root)
  @force = !!force
end

Instance Attribute Details

#forceObject (readonly)

Returns the value of attribute force.



9
10
11
# File 'lib/flash_unified/installer.rb', line 9

def force
  @force
end

#source_rootObject (readonly)

Returns the value of attribute source_root.



9
10
11
# File 'lib/flash_unified/installer.rb', line 9

def source_root
  @source_root
end

#target_rootObject (readonly)

Returns the value of attribute target_root.



9
10
11
# File 'lib/flash_unified/installer.rb', line 9

def target_root
  @target_root
end

Instance Method Details

#copy_helpers(&block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/flash_unified/installer.rb', line 49

def copy_helpers(&block)
  src_dir = source_root.join('app', 'helpers', 'flash_unified')
  dst_dir = target_root.join('app', 'helpers', 'flash_unified')
  files = %w[
    view_helper.rb
  ]
  copy_files(files, src_dir, dst_dir, &block)
end

#copy_javascript(&block) ⇒ Object



18
19
20
21
22
# File 'lib/flash_unified/installer.rb', line 18

def copy_javascript(&block)
  src = source_root.join('app', 'javascript', 'flash_unified')
  dst = target_root.join('app', 'javascript', 'flash_unified')
  copy_tree(src, dst, &block)
end

#copy_locales(&block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/flash_unified/installer.rb', line 41

def copy_locales(&block)
  src_dir = source_root.join('config', 'locales')
  dst_dir = target_root.join('config', 'locales')
  FileUtils.mkdir_p(dst_dir) unless dst_dir.exist?
  files = Dir.glob(src_dir.join('*.yml')).map { |p| File.basename(p) }
  copy_files(files, src_dir, dst_dir, &block)
end

#copy_templates(&block) ⇒ Object

Copy only _templates.html.erb



25
26
27
28
29
30
# File 'lib/flash_unified/installer.rb', line 25

def copy_templates(&block)
  src_dir = source_root.join('app', 'views', 'flash_unified')
  dst_dir = target_root.join('app', 'views', 'flash_unified')
  files = %w[_templates.html.erb]
  copy_files(files, src_dir, dst_dir, &block)
end

#copy_views(&block) ⇒ Object

Copy all files in views/flash_unified/ directory



33
34
35
36
37
38
39
# File 'lib/flash_unified/installer.rb', line 33

def copy_views(&block)
  src_dir = source_root.join('app', 'views', 'flash_unified')
  dst_dir = target_root.join('app', 'views', 'flash_unified')
  raise "source missing: #{src_dir}" unless src_dir.directory?
  files = Dir.children(src_dir).select { |f| File.file?(src_dir.join(f)) }
  copy_files(files, src_dir, dst_dir, &block)
end