Class: PlatformosCheck::AppFile

Inherits:
Object
  • Object
show all
Defined in:
lib/platformos_check/app_file.rb

Direct Known Subclasses

AssetFile, GraphqlFile, JsonFile, LiquidFile, YamlFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relative_path, storage) ⇒ AppFile

Returns a new instance of AppFile.



9
10
11
12
13
14
15
# File 'lib/platformos_check/app_file.rb', line 9

def initialize(relative_path, storage)
  @relative_path = relative_path
  @storage = storage
  @source = nil
  @version = nil
  @eol = "\n"
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



7
8
9
# File 'lib/platformos_check/app_file.rb', line 7

def storage
  @storage
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/platformos_check/app_file.rb', line 7

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



128
129
130
# File 'lib/platformos_check/app_file.rb', line 128

def ==(other)
  other.is_a?(self.class) && relative_path == other.relative_path
end

#build_nameObject



33
34
35
36
37
38
39
40
# File 'lib/platformos_check/app_file.rb', line 33

def build_name
  n = remove_extension(relative_path.sub(dir_prefix, '')).to_s
  return n if module_name.nil?

  return n if n.start_with?(module_prefix)

  "#{module_prefix}#{n}"
end

#dir_namesObject



74
75
76
# File 'lib/platformos_check/app_file.rb', line 74

def dir_names
  @dir_names ||= @relative_path.split(File::SEPARATOR).reject(&:empty?)
end

#dir_prefixObject



42
43
44
# File 'lib/platformos_check/app_file.rb', line 42

def dir_prefix
  nil
end

#graphql?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/platformos_check/app_file.rb', line 112

def graphql?
  false
end

#liquid?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/platformos_check/app_file.rb', line 108

def liquid?
  false
end

#module_nameObject



50
51
52
53
54
55
56
# File 'lib/platformos_check/app_file.rb', line 50

def module_name
  @module_name ||= if module_original_file?
                     dir_names[1]
                   elsif module_overwrite_file?
                     return dir_names[2]
                   end
end

#module_original_file?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/platformos_check/app_file.rb', line 58

def module_original_file?
  dir_names[0] == 'modules'
end

#module_original_file_pathObject



70
71
72
# File 'lib/platformos_check/app_file.rb', line 70

def module_original_file_path
  @module_original_file_path ||= module_overwrite_file? ? dir_names[1..].join(File::SEPARATOR) : nil
end

#module_overwrite_file?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/platformos_check/app_file.rb', line 62

def module_overwrite_file?
  dir_names[0] == 'app' && dir_names[1] == 'modules'
end

#module_overwrite_file_pathObject



66
67
68
# File 'lib/platformos_check/app_file.rb', line 66

def module_overwrite_file_path
  @module_overwrite_file_path ||= module_original_file? ? 'app/' + relative_path.to_s : nil
end

#module_prefixObject



46
47
48
# File 'lib/platformos_check/app_file.rb', line 46

def module_prefix
  @module_prefix ||= "modules#{File::SEPARATOR}#{module_name}#{File::SEPARATOR}"
end

#nameObject



25
26
27
# File 'lib/platformos_check/app_file.rb', line 25

def name
  @name ||= dir_prefix.nil? ? remove_extension(relative_path).to_s : build_name
end

#page?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/platformos_check/app_file.rb', line 116

def page?
  false
end

#partial?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/platformos_check/app_file.rb', line 120

def partial?
  false
end

#pathObject



17
18
19
# File 'lib/platformos_check/app_file.rb', line 17

def path
  @storage.path(@relative_path)
end

#relative_pathObject



21
22
23
# File 'lib/platformos_check/app_file.rb', line 21

def relative_path
  @relative_pathname ||= Pathname.new(@relative_path)
end

#remove_extension(path) ⇒ Object



29
30
31
# File 'lib/platformos_check/app_file.rb', line 29

def remove_extension(path)
  path.sub_ext('')
end

#sourceObject

For the corrector to work properly, we should have a simple mental model of the internal representation of eol characters (Windows uses rn, Linux uses n).

Parser::Source::Buffer strips the r from the source file, so if you are autocorrecting the file you might lose that info and cause a git diff. It also makes the node.start_index/end_index calculation break. That’s not cool.

So in here we track whether the source file has rn in it and we’ll make sure that the file we write has the same eol as the source file.



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/platformos_check/app_file.rb', line 90

def source
  return @source if @source

  if @storage.versioned?
    @source, @version = @storage.read_version(@relative_path)
  else
    @source = @storage.read(@relative_path)
  end
  @eol = @source.include?("\r\n") ? "\r\n" : "\n"
  @source = @source
            .gsub(/\r(?!\n)/, "\r\n") # fix rogue \r without followup \n with \r\n
            .gsub("\r\n", "\n")
end

#translation?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/platformos_check/app_file.rb', line 124

def translation?
  false
end

#yaml?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/platformos_check/app_file.rb', line 104

def yaml?
  false
end