Class: Dotsync::Mapping

Inherits:
Object
  • Object
show all
Includes:
PathUtils
Defined in:
lib/dotsync/models/mapping.rb

Constant Summary

Constants included from PathUtils

PathUtils::ENV_VARS_COLOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PathUtils

#colorize_env_vars, #expand_env_vars, #extract_env_vars, #path_is_parent_or_same?, #relative_to_absolute, #sanitize_path, #translate_tmp_path

Constructor Details

#initialize(attributes) ⇒ Mapping

Returns a new instance of Mapping.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dotsync/models/mapping.rb', line 9

def initialize(attributes)
  @original_src = attributes["src"]
  @original_dest = attributes["dest"]
  @original_ignores = Array(attributes["ignore"])
  @original_only = Array(attributes["only"])
  @force = attributes["force"] || false

  @sanitized_src, @sanitized_dest, @sanitized_ignores, @sanitized_only = process_paths(
    @original_src,
    @original_dest,
    @original_ignores,
    @original_only
  )
end

Instance Attribute Details

#original_destObject (readonly)

Returns the value of attribute original_dest.



7
8
9
# File 'lib/dotsync/models/mapping.rb', line 7

def original_dest
  @original_dest
end

#original_ignoresObject (readonly)

Returns the value of attribute original_ignores.



7
8
9
# File 'lib/dotsync/models/mapping.rb', line 7

def original_ignores
  @original_ignores
end

#original_onlyObject (readonly)

Returns the value of attribute original_only.



7
8
9
# File 'lib/dotsync/models/mapping.rb', line 7

def original_only
  @original_only
end

#original_srcObject (readonly)

Returns the value of attribute original_src.



7
8
9
# File 'lib/dotsync/models/mapping.rb', line 7

def original_src
  @original_src
end

Instance Method Details

#apply_to(path) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dotsync/models/mapping.rb', line 107

def apply_to(path)
  relative_path = if Pathname.new(path).absolute?
    path.delete_prefix(File.join(src, "/"))
  else
    path
  end

  Dotsync::Mapping.new(
    "src" => File.join(@original_src, relative_path),
    "dest" => File.join(@original_dest, relative_path),
    "force" => @force,
    "only" => @only,
    "ignore" => @original_ignores
  )
end

#backup_basenameObject



78
79
80
81
82
# File 'lib/dotsync/models/mapping.rb', line 78

def backup_basename
  return unless valid?
  return File.dirname(dest) unless File.exist?(dest)
  File.basename(dest)
end

#backup_possible?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/dotsync/models/mapping.rb', line 74

def backup_possible?
  valid? && File.exist?(dest)
end

#bidirectional_include?(path) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
# File 'lib/dotsync/models/mapping.rb', line 129

def bidirectional_include?(path)
  return true unless has_inclusions?
  return true if path == src
  inclusions.any? { |inclusion| path_is_parent_or_same?(inclusion, path) || path_is_parent_or_same?(path, inclusion) }
end

#decorated_destObject



103
104
105
# File 'lib/dotsync/models/mapping.rb', line 103

def decorated_dest
  colorize_env_vars(original_dest)
end

#decorated_srcObject



99
100
101
# File 'lib/dotsync/models/mapping.rb', line 99

def decorated_src
  colorize_env_vars(original_src)
end

#destObject



28
29
30
# File 'lib/dotsync/models/mapping.rb', line 28

def dest
  @sanitized_dest
end

#directories?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/dotsync/models/mapping.rb', line 44

def directories?
  File.directory?(src) && File.directory?(dest)
end

#file_changed?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/dotsync/models/mapping.rb', line 66

def file_changed?
  return false unless files_present?
  # Check size first for quick comparison
  return true if File.size(src) != File.size(dest)
  # If sizes match, compare content
  FileUtils.compare_file(src, dest) == false
end

#file_present_in_src_only?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/dotsync/models/mapping.rb', line 56

def file_present_in_src_only?
  File.file?(src) && !File.exist?(dest) && File.directory?(File.dirname(dest))
end

#files?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/dotsync/models/mapping.rb', line 48

def files?
  files_present? || file_present_in_src_only?
end

#files_present?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/dotsync/models/mapping.rb', line 52

def files_present?
  File.file?(src) && File.file?(dest)
end

#force?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dotsync/models/mapping.rb', line 40

def force?
  @force
end

#iconsObject



84
85
86
87
88
89
90
91
# File 'lib/dotsync/models/mapping.rb', line 84

def icons
  msg = []
  msg << Icons.force if force?
  msg << Icons.only if has_inclusions?
  msg << Icons.ignore if has_ignores?
  msg << Icons.invalid unless valid?
  msg.join
end

#ignore?(path) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/dotsync/models/mapping.rb', line 135

def ignore?(path)
  ignores.any? { |ignore| path.start_with?(ignore) }
end

#ignoresObject



32
33
34
# File 'lib/dotsync/models/mapping.rb', line 32

def ignores
  @sanitized_ignores
end

#include?(path) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
# File 'lib/dotsync/models/mapping.rb', line 123

def include?(path)
  return true unless has_inclusions?
  return true if path == src
  inclusions.any? { |inclusion| path_is_parent_or_same?(inclusion, path) }
end

#inclusionsObject



36
37
38
# File 'lib/dotsync/models/mapping.rb', line 36

def inclusions
  @sanitized_only
end

#skip?(path) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/dotsync/models/mapping.rb', line 139

def skip?(path)
  ignore?(path) || !include?(path)
end

#srcObject



24
25
26
# File 'lib/dotsync/models/mapping.rb', line 24

def src
  @sanitized_src
end

#to_sObject



93
94
95
96
97
# File 'lib/dotsync/models/mapping.rb', line 93

def to_s
  msg = "#{decorated_src} → #{decorated_dest}"
  msg += " #{icons}" if icons != ""
  msg
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/dotsync/models/mapping.rb', line 60

def valid?
  return false unless paths_are_distinct?
  return false unless paths_not_nested?
  directories? || files? || file_present_in_src_only?
end