Class: Panache::Style

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_regex) ⇒ Style

Returns a new instance of Style.



38
39
40
41
42
# File 'lib/panache.rb', line 38

def initialize(file_regex)
  @tab_spaces = DEFAULT_TAB_SPACES
  @file_regex = file_regex
  @rules = []
end

Class Method Details

.create(file_regex, &block) ⇒ Object



44
45
46
47
48
# File 'lib/panache.rb', line 44

def self.create(file_regex, &block)
  style = Style.new(file_regex)
  style.instance_eval &block
  Panache.add_style style
end

Instance Method Details

#check(line) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/panache.rb', line 66

def check(line)
  # Replace tabs with spaces according to the specified conversion.
  line.gsub!("\t", " " * @tab_spaces)
  @rules.flat_map do |rule|
    violations = rule.check(line)
    violations.each { |v| v.style = self }
  end
end

#checks?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/panache.rb', line 62

def checks?(file_name)
  file_name =~ @file_regex
end

#rule(regex, message) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/panache.rb', line 54

def rule(regex, message)
  if regex.is_a? Array
    @rules << Rule.new(regex, message)
  else
    @rules << Rule.new([regex], message)
  end
end

#spaces_per_tab(n) ⇒ Object



50
51
52
# File 'lib/panache.rb', line 50

def spaces_per_tab(n)
  @tab_spaces = n
end