Class: CypressViewportUpdater::ViewportPresetJsFile

Inherits:
ExistingGithubFile show all
Defined in:
app/sidekiq/cypress_viewport_updater/viewport_preset_js_file.rb

Instance Attribute Summary

Attributes inherited from ExistingGithubFile

#github_path, #name, #raw_content, #sha, #updated_content

Instance Method Summary collapse

Constructor Details

#initializeViewportPresetJsFile

Returns a new instance of ViewportPresetJsFile.



5
6
7
8
# File 'app/sidekiq/cypress_viewport_updater/viewport_preset_js_file.rb', line 5

def initialize
  super(github_path: 'src/platform/testing/e2e/cypress/support/commands/viewportPreset.js',
        name: 'viewportPreset.js')
end

Instance Method Details

#create_viewport_presets(line:, viewports:) ⇒ Object (private)



32
33
34
35
36
37
38
39
40
41
# File 'app/sidekiq/cypress_viewport_updater/viewport_preset_js_file.rb', line 32

def create_viewport_presets(line:, viewports:)
  viewport_type = /(mobile|tablet|desktop)/.match(line)[0].to_sym

  viewports.send(viewport_type).each do |viewport|
    rank = viewport.rank
    width = viewport.width
    height = viewport.height
    yield("  'va-top-#{viewport_type}-#{rank}': { width: #{width}, height: #{height} },")
  end
end

#update(viewports:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/sidekiq/cypress_viewport_updater/viewport_preset_js_file.rb', line 10

def update(viewports:)
  new_lines = []

  raw_content.split("\n").each do |line|
    if /va-top-(mobile|tablet|desktop)-\d+/.match(line)
      if /va-top-(mobile|tablet|desktop)-1/.match(line)
        create_viewport_presets(line:,
                                viewports:) do |updated_line|
                                  new_lines << updated_line
                                end
      end
    else
      new_lines << line
    end
  end

  self.updated_content = "#{new_lines.join("\n")}\n"
  self
end