Sha256: 7ae6295d50398c441784c80dd526ff3a3a7a215cf173a2b49af396a90c580faa

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require 'rails_best_practices/checks'
require 'rails_best_practices/core'

module RailsBestPractices
  
  class <<self
    def analyze_files(dir = '.', options = {})
      files = expand_dirs_to_files(dir)
      files = model_first_sort(files)
      files = add_duplicate_migrations(files)
      ['vendor', 'spec', 'test', 'stories'].each do |pattern|
        files = ignore_files(files, "#{pattern}/") unless options[pattern]
      end
      files
    end
  
    def expand_dirs_to_files *dirs
      extensions = ['rb', 'erb', 'haml', 'builder']

      dirs.flatten.map { |p|
        if File.directory? p
          Dir[File.join(p, '**', "*.{#{extensions.join(',')}}")]
        else
          p
        end
      }.flatten
    end

    # for law_of_demeter_check
    def model_first_sort files
      files.sort { |a, b|
        if a =~ /models\/.*rb/
          -1
        elsif b =~ /models\/.*rb/
          1
        else
          a <=> b
        end
      }
    end

    # for always_add_db_index_check
    def add_duplicate_migrations files
      migration_files = files.select { |file| file.index("db/migrate") }
      (files << migration_files).flatten
    end

    def ignore_files files, pattern
      files.reject { |file| file.index(pattern) }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_best_practices-0.3.21 lib/rails_best_practices.rb
rails_best_practices-0.3.20 lib/rails_best_practices.rb
rails_best_practices-0.3.19 lib/rails_best_practices.rb
rails_best_practices-0.3.18 lib/rails_best_practices.rb
rails_best_practices-0.3.17 lib/rails_best_practices.rb
rails_best_practices-0.3.16 lib/rails_best_practices.rb