Class: Workarea::Lint

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/lint.rb,
lib/workarea/lint/inconsistent_details.rb,
lib/workarea/lint/skus_missing_pricing.rb,
lib/workarea/lint/skus_missing_variants.rb,
lib/workarea/lint/skus_missing_inventory.rb,
lib/workarea/lint/products_missing_images.rb,
lib/workarea/lint/products_missing_variants.rb

Defined Under Namespace

Classes: InconsistentDetails, ProductsMissingImages, ProductsMissingVariants, SkusMissingInventory, SkusMissingPricing, SkusMissingVariants

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLint

Returns a new instance of Lint.



58
59
60
61
# File 'lib/workarea/lint.rb', line 58

def initialize
  @warnings = 0
  @errors = 0
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/workarea/lint.rb', line 3

def errors
  @errors
end

#warningsObject (readonly)

Returns the value of attribute warnings.



3
4
5
# File 'lib/workarea/lint.rb', line 3

def warnings
  @warnings
end

Class Method Details

.lint_classesObject



25
26
27
# File 'lib/workarea/lint.rb', line 25

def self.lint_classes
  lints_paths.map { |path| load_lint_classes(path) }.flatten.compact
end

.lints_pathsObject



30
31
32
33
34
35
# File 'lib/workarea/lint.rb', line 30

def self.lints_paths
  lints_path = 'lib/workarea/lint/*.rb'
  ["#{Core::Engine.root}/#{lints_path}"] +
    Plugin.installed.map { |p| "#{p.root}/#{lints_path}" } +
    ["#{Rails.root}/#{lints_path}"]
end

.load_lint_classes(path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/workarea/lint.rb', line 37

def self.load_lint_classes(path)
  klasses = []

  Dir[path].each do |file|
    require file

    class_name = file.split('/').last.gsub('.rb', '').camelize

    begin
      klasses << "Workarea::Lint::#{class_name}".constantize
    rescue NameError
      puts <<-eos.strip_heredoc
        Could not load #{class_name} from #{file},
        make sure file name matches class name.
      eos
    end
  end

  klasses
end

.runObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/workarea/lint.rb', line 5

def self.run
  lint_classes.each do |klass|
    puts "\e[#{Workarea::COLOR_CODES[:yellow]}m== Linting #{klass.name.demodulize.titleize}...\e[0m"
    lint = klass.new
    lint.run

    message = "#{lint.warnings} warnings, #{lint.errors} errors"
    color = if lint.warnings.zero? && lint.errors.zero?
              :green
            elsif lint.errors.zero?
              :yellow
            else
              :red
            end

    puts "\e[#{Workarea::COLOR_CODES[color]}m#{message}\e[0m"
    puts ''
  end
end

Instance Method Details

#catalog_skusObject



77
78
79
# File 'lib/workarea/lint.rb', line 77

def catalog_skus
  @catalog_skus ||= Catalog::Product.all.distinct('variants.sku')
end

#error(message) ⇒ Object



72
73
74
75
# File 'lib/workarea/lint.rb', line 72

def error(message)
  @errors += 1
  puts message unless Rails.env.test?
end

#inventory_skusObject



85
86
87
# File 'lib/workarea/lint.rb', line 85

def inventory_skus
  @inventory_skus ||= Inventory::Sku.all.distinct(:id)
end

#pricing_skusObject



81
82
83
# File 'lib/workarea/lint.rb', line 81

def pricing_skus
  @pricing_skus ||= Pricing::Sku.all.distinct(:id)
end

#runObject

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/workarea/lint.rb', line 63

def run
  raise NotImplementedError, "#{self.class.name} must implement #run"
end

#warn(message) ⇒ Object



67
68
69
70
# File 'lib/workarea/lint.rb', line 67

def warn(message)
  @warnings += 1
  puts message unless Rails.env.test?
end