Class: Jazzy::Config::Attribute

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

Overview

rubocop:disable Style/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.



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

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.



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

def command_line
  @command_line
end

#config_file_keyObject (readonly)

Returns the value of attribute config_file_key.



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

def config_file_key
  @config_file_key
end

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parseObject (readonly)

Returns the value of attribute parse.



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

def parse
  @parse
end

Instance Method Details

#attach_to_option_parser(config, opt) ⇒ Object



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

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)


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

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

#get(config) ⇒ Object



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

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

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



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

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



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

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

#set_raw(config, val) ⇒ Object



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

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

#set_to_default(config) ⇒ Object



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

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