Sha256: 2d8b22604af864ad2ddfc6d27a4dc7dc2931624278552b0ced28a24bce6d5a1d

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# encoding: utf-8
require 'rails_best_practices/checks'
require 'rails_best_practices/core'

module RailsBestPractices

  class <<self
    def prepare_files
      expand_dirs_to_files 'app/models'
    end

    def analyze_files(dir = '.', options = {})
      files = expand_dirs_to_files(dir)
      files = model_first_sort(files)
      ['vendor', 'spec', 'test', 'stories', 'features'].each do |pattern|
        files = ignore_files(files, "#{pattern}/") unless options[pattern]
      end

      # Exclude files based on exclude regexes if the option is set.
      for pattern in options[:exclude]
        files = ignore_files(files, 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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_best_practices-0.5.5 lib/rails_best_practices.rb