Class: FullMetalBody::WhitelistWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/full_metal_body/whitelist_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller_path, action_name) ⇒ WhitelistWriter

Returns a new instance of WhitelistWriter.

Parameters:

  • controller_path (String)
  • action_name (String)


14
15
16
17
18
# File 'lib/full_metal_body/whitelist_writer.rb', line 14

def initialize(controller_path, action_name)
  @controller_path = controller_path
  @action_name = action_name
  @data = {}
end

Instance Method Details

#save_pathPathname

Return path to store the whitelist.

Returns:

  • (Pathname)


39
40
41
42
43
44
45
# File 'lib/full_metal_body/whitelist_writer.rb', line 39

def save_path
  @save_path ||= if Rails.env.test?
                   Rails.root.join('tmp', "test#{ENV.fetch('TEST_ENV_NUMBER', '')}", 'whitelist', "#{@controller_path}.yml")
                 else
                   Rails.root.join('tmp', 'whitelist', "#{@controller_path}.yml")
                 end
end

#write!(blocked_keys) ⇒ Object

Parameters:

  • blocked_keys (Array<Array>)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/full_metal_body/whitelist_writer.rb', line 21

def write!(blocked_keys)
  blocked_keys.each { |key| update_data!(key) }

  dir = File.dirname(save_path)
  FileUtils.mkdir_p(dir, mode: 0o777) unless Dir.exist?(dir)
  if File.exist?(save_path)
    whitelist = YAML.load_file(save_path)
    @data.deep_merge!(whitelist)
  end
  @data.deep_sort!
  File.open(save_path, "w") do |file|
    YAML.dump(@data, file)
  end
  FileUtils.chmod(0o777, save_path)
end