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?



88
89
90
# File 'lib/platformos_check/app_file.rb', line 88

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

#build_nameObject



33
34
35
36
37
38
39
40
41
# 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?

  prefix = "modules#{File::SEPARATOR}#{module_name}#{File::SEPARATOR}"
  return n if n.start_with?(prefix)

  "#{prefix}#{n}"
end

#dir_prefixObject



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

def dir_prefix
  nil
end

#liquid?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/platformos_check/app_file.rb', line 84

def liquid?
  false
end

#module_nameObject



47
48
49
50
51
52
# File 'lib/platformos_check/app_file.rb', line 47

def module_name
  @module_name ||= begin
    dir_names = @relative_path.split(File::SEPARATOR).reject(&:empty?)
    dir_names.first == 'modules' ? dir_names[1] : nil
  end
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

#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.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/platformos_check/app_file.rb', line 66

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

#yaml?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/platformos_check/app_file.rb', line 80

def yaml?
  false
end