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 }) ⇒ Attribute

Returns a new instance of Attribute.



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

def initialize(name, description: nil, command_line: nil,
               default: nil, parse: ->(x) { x })
  @name = name.to_s
  @description = Array(description)
  @command_line = Array(command_line)
  @default = default
  @parse = parse
  @config_file_key = full_command_line_name || @name
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

Instance Method Details

#attach_to_option_parser(config, opt) ⇒ Object



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

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)


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

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

#get(config) ⇒ Object



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

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

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



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

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



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

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

#set_raw(config, val) ⇒ Object



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

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

#set_to_default(config) ⇒ Object



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

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