Class: Erbf::Config

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

Constant Summary collapse

DEFAULT_FILEPATHS =
%w[config/erbf.yml .erbf.yml].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_length:, logger:, debug:, embedded:, ruby:) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
# File 'lib/erbf/config.rb', line 11

def initialize(line_length:, logger:, debug:, embedded:, ruby:)
  @line_length = line_length
  @logger = logger
  @debug = debug
  @embedded = embedded
  @ruby = ruby
end

Instance Attribute Details

#embeddedObject (readonly)

Returns the value of attribute embedded.



9
10
11
# File 'lib/erbf/config.rb', line 9

def embedded
  @embedded
end

#line_lengthObject (readonly)

Returns the value of attribute line_length.



9
10
11
# File 'lib/erbf/config.rb', line 9

def line_length
  @line_length
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/erbf/config.rb', line 9

def logger
  @logger
end

#rubyObject (readonly)

Returns the value of attribute ruby.



9
10
11
# File 'lib/erbf/config.rb', line 9

def ruby
  @ruby
end

Class Method Details

.default_embeddedObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/erbf/config.rb', line 31

def default_embedded
  prettier = "node_modules/.bin/prettier"
  if File.exist?(prettier)
    [
      {
        types: %w[text/javascript module],
        command: "#{prettier} --stdin-filepath file.js --print-width %<line_length>d"
      },
      {
        types: ["text/css"],
        command: "#{prettier} --stdin-filepath file.css --print-width %<line_length>d"
      },
      {
        types: %w[importmap application/json application/ld+json],
        command: "#{prettier} --stdin-filepath file.json --print-width %<line_length>d"
      }
    ]
  else
    []
  end
end

.default_optionsObject



57
58
59
60
61
62
63
64
65
# File 'lib/erbf/config.rb', line 57

def default_options
  {
    line_length: 80,
    logger: Logger.new($stderr, level: Logger::WARN),
    debug: false,
    embedded: default_embedded,
    ruby: default_ruby
  }
end

.default_rubyObject



53
54
55
# File 'lib/erbf/config.rb', line 53

def default_ruby
  { formatter: "syntax_tree", syntax_tree_plugins: [] }
end

.load(config_file: nil, **options) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/erbf/config.rb', line 22

def load(config_file: nil, **options)
  opts =
    default_options.merge(
      config_file.nil? ? options_from_default_file : options_from_file(config_file),
      options
    )
  new(**opts)
end

.options_from_default_fileObject



72
73
74
75
76
77
78
79
# File 'lib/erbf/config.rb', line 72

def options_from_default_file
  DEFAULT_FILEPATHS.each do |path|
    next unless File.exist?(path)

    return options_from_file(path)
  end
  {}
end

.options_from_file(path) ⇒ Object



67
68
69
70
# File 'lib/erbf/config.rb', line 67

def options_from_file(path)
  yaml = YAML.safe_load_file(path, symbolize_names: true)
  { line_length: yaml[:line_length], embedded: yaml[:embedded], ruby: yaml[:ruby] }.compact
end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


19
# File 'lib/erbf/config.rb', line 19

def debug? = @debug