Class: ERBLint::LinterConfig

Inherits:
Object
  • Object
show all
Includes:
SmartProperties
Defined in:
lib/erb_lint/linter_config.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ LinterConfig

Returns a new instance of LinterConfig.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/erb_lint/linter_config.rb', line 25

def initialize(config = {})
  config = config.dup.deep_stringify_keys
  allowed_keys = self.class.properties.keys.map(&:to_s)
  given_keys = config.keys
  if (extra_keys = given_keys - allowed_keys).any?
    raise Error, "Given key is not allowed: #{extra_keys.join(", ")}"
  end
  super(config)
rescue SmartProperties::InitializationError => e
  raise Error, "The following properties are required to be set: #{e.properties}"
rescue SmartProperties::InvalidValueError => e
  raise Error, e.message
end

Class Method Details

.array_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/erb_lint/linter_config.rb', line 13

def array_of?(klass)
  lambda { |value| value.is_a?(Array) && value.all? { |s| s.is_a?(klass) } }
end

.to_array_of(klass) ⇒ Object



17
18
19
# File 'lib/erb_lint/linter_config.rb', line 17

def to_array_of(klass)
  lambda { |entries| entries.map { |entry| klass.new(entry) } }
end

Instance Method Details

#[](name) ⇒ Object



39
40
41
42
43
44
# File 'lib/erb_lint/linter_config.rb', line 39

def [](name)
  unless self.class.properties.key?(name)
    raise Error, "No such property: #{name}"
  end
  super
end

#excludes_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/erb_lint/linter_config.rb', line 54

def excludes_file?(filename)
  exclude.any? do |path|
    File.fnmatch?(path, filename)
  end
end

#to_hashObject



46
47
48
49
50
51
52
# File 'lib/erb_lint/linter_config.rb', line 46

def to_hash
  {}.tap do |hash|
    self.class.properties.to_hash.each_key do |key|
      hash[key.to_s] = self[key]
    end
  end
end