Module: EasyML::Support::GitIgnorable

Extended by:
ActiveSupport::Concern
Defined in:
lib/easy_ml/support/git_ignorable.rb

Defined Under Namespace

Modules: GitignoreInitializer

Instance Method Summary collapse

Instance Method Details

#update_gitignoreObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/easy_ml/support/git_ignorable.rb', line 32

def update_gitignore
  self.class.gitignore_attributes.each do |attribute, block|
    attribute_value = send(attribute)
    next if attribute_value.blank?

    patterns = block ? block.call(attribute_value) : attribute_value
    next if patterns.nil? || (patterns.respond_to?(:empty?) && patterns.empty?)

    patterns = [patterns] unless patterns.is_a?(Array)
    patterns = relativize(patterns)
    gitignore_path = File.join(Dir.pwd, ".gitignore")

    FileUtils.mkdir_p(File.dirname(gitignore_path))
    FileUtils.touch(gitignore_path) unless File.exist?(gitignore_path)

    existing_content = File.read(gitignore_path).split("\n")
    new_patterns = patterns.reject { |pattern| existing_content.include?(pattern) }
    next if new_patterns.empty?

    new_content = (existing_content + new_patterns).join("\n").strip
    File.write(gitignore_path, new_content)
  end
end