Class: Packs::Private::FileMoveOperation

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/packs/private/file_move_operation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.destination_pathname_for_new_public_api(origin_pathname) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/packs/private/file_move_operation.rb', line 33

def self.destination_pathname_for_new_public_api(origin_pathname)
  origin_pack = get_origin_pack(origin_pathname)
  if origin_pack
    filepath_without_pack_name = origin_pathname.to_s.gsub("#{origin_pack.name}/", '')
  else
    filepath_without_pack_name = origin_pathname.to_s
  end

  # We join the pack name with the rest of the path...
  path_parts = filepath_without_pack_name.split('/')
  Pathname.new(origin_pack&.name || ParsePackwerk::ROOT_PACKAGE_NAME).join(
    # ... keeping the "app" or "spec"
    T.must(path_parts[0]),
    # ... substituting "controllers," "services," etc. with "public"
    'public',
    # ... then the rest is the same
    T.must(path_parts[2..]).join('/')
    # and we take the cleanpath so `./app/...` becomes `app/...`
  ).cleanpath
end

.destination_pathname_for_package_move(origin_pathname, new_package_root) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/packs/private/file_move_operation.rb', line 23

def self.destination_pathname_for_package_move(origin_pathname, new_package_root)
  origin_pack = get_origin_pack(origin_pathname)
  if origin_pack
    Pathname.new(origin_pathname.to_s.gsub(origin_pack.name, new_package_root.to_s)).cleanpath
  else
    new_package_root.join(origin_pathname).cleanpath
  end
end

.get_origin_pack(origin_pathname) ⇒ Object



18
19
20
# File 'lib/packs/private/file_move_operation.rb', line 18

def self.get_origin_pack(origin_pathname)
  Packs.for_file(origin_pathname)
end

Instance Method Details

#origin_packObject



13
14
15
# File 'lib/packs/private/file_move_operation.rb', line 13

def origin_pack
  self.class.get_origin_pack(origin_pathname)
end

#spec_file_move_operationObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/packs/private/file_move_operation.rb', line 55

def spec_file_move_operation
  # This could probably be implemented by some "strategy pattern" where different extension types are handled by different helpers
  # Such a thing could also include, for example, when moving a controller, moving its ERB view too.
  if origin_pathname.extname == '.rake'
    new_origin_pathname = origin_pathname.sub('/lib/', '/spec/lib/').sub(%r{^lib/}, 'spec/lib/').sub('.rake', '_spec.rb')
    new_destination_pathname = destination_pathname.sub('/lib/', '/spec/lib/').sub(%r{^lib/}, 'spec/lib/').sub('.rake', '_spec.rb')
  else
    new_origin_pathname = origin_pathname.sub('/app/', '/spec/').sub(%r{^app/}, 'spec/').sub('.rb', '_spec.rb')
    new_destination_pathname = destination_pathname.sub('/app/', '/spec/').sub(%r{^app/}, 'spec/').sub('.rb', '_spec.rb')
  end
  FileMoveOperation.new(
    origin_pathname: new_origin_pathname,
    destination_pathname: new_destination_pathname,
    destination_pack: destination_pack
  )
end