Class: Jazzy::Config::Attribute

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

Overview

rubocop:disable Naming/AccessorMethodName

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description: nil, command_line: nil, default: nil, parse: ->(x) { x }, per_module: false) ⇒ Attribute

Returns a new instance of Attribute.



18
19
20
21
22
23
24
25
26
27
# File 'lib/jazzy/config.rb', line 18

def initialize(name, description: nil, command_line: nil,
               default: nil, parse: ->(x) { x }, per_module: false)
  @name = name.to_s
  @description = Array(description)
  @command_line = Array(command_line)
  @default = default
  @parse = parse
  @config_file_key = full_command_line_name || @name
  @per_module = per_module
end

Instance Attribute Details

#command_lineObject (readonly)

Returns the value of attribute command_line.



15
16
17
# File 'lib/jazzy/config.rb', line 15

def command_line
  @command_line
end

#config_file_keyObject (readonly)

Returns the value of attribute config_file_key.



15
16
17
# File 'lib/jazzy/config.rb', line 15

def config_file_key
  @config_file_key
end

#defaultObject (readonly)

Returns the value of attribute default.



15
16
17
# File 'lib/jazzy/config.rb', line 15

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



15
16
17
# File 'lib/jazzy/config.rb', line 15

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/jazzy/config.rb', line 15

def name
  @name
end

#parseObject (readonly)

Returns the value of attribute parse.



15
16
17
# File 'lib/jazzy/config.rb', line 15

def parse
  @parse
end

#per_moduleObject (readonly)

Returns the value of attribute per_module.



15
16
17
# File 'lib/jazzy/config.rb', line 15

def per_module
  @per_module
end

Instance Method Details

#attach_to_option_parser(config, opt) ⇒ Object



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

def attach_to_option_parser(config, opt)
  return if command_line.empty?

  opt.on(*command_line, *description) do |val|
    set(config, val)
  end
end

#configured?(config) ⇒ Boolean

Returns:

  • (Boolean)


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

def configured?(config)
  config.method("#{name}_configured").call
end

#get(config) ⇒ Object



29
30
31
# File 'lib/jazzy/config.rb', line 29

def get(config)
  config.method(name).call
end

#set(config, val, mark_configured: true) ⇒ Object



37
38
39
40
# File 'lib/jazzy/config.rb', line 37

def set(config, val, mark_configured: true)
  set_raw(config, config.instance_exec(val, &parse))
  config.method("#{name}_configured=").call(true) if mark_configured
end

#set_if_unconfigured(config, val) ⇒ Object



46
47
48
# File 'lib/jazzy/config.rb', line 46

def set_if_unconfigured(config, val)
  set(config, val) unless configured?(config)
end

#set_raw(config, val) ⇒ Object



33
34
35
# File 'lib/jazzy/config.rb', line 33

def set_raw(config, val)
  config.method("#{name}=").call(val)
end

#set_to_default(config) ⇒ Object



42
43
44
# File 'lib/jazzy/config.rb', line 42

def set_to_default(config)
  set(config, default, mark_configured: false) if default
end