Class: Mint::Config

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

Constant Summary collapse

DEFAULT_STDIN_MODE =
false
DEFAULT_VERBOSE =
false
DEFAULT_LAYOUT_NAME =
"default"
DEFAULT_STYLE_NAME =
"default"
DEFAULT_STYLE_MODE =
:inline
DEFAULT_OUTPUT_FILE_FORMAT =
'%{name}.%{ext}'
DEFAULT_WORKING_DIRECTORY =
lambda { Pathname.getwd.expand_path }
DEFAULT_DESTINATION_DIRECTORY =
lambda { Pathname.getwd.expand_path }
DEFAULT_PRESERVE_STRUCTURE =
true
DEFAULT_INSERT_TITLE_HEADING =
false
DEFAULT_AUTODROP =
true
DEFAULT_NAVIGATION =
false
DEFAULT_NAVIGATION_DEPTH =
3
DEFAULT_NAVIGATION_TITLE =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mint/config.rb', line 38

def initialize(options = {})
  @stdin_mode = options[:stdin_mode] if options.key?(:stdin_mode)
  @help = options[:help]
  @verbose = options[:verbose] if options.key?(:verbose)
  @layout_name = options[:layout_name] || options[:template_name]
  @style_name = options[:style_name] || options[:template_name]
  @style_mode = options[:style_mode]
  @output_file_format = options[:output_file_format]
  @working_directory = options[:working_directory]&.expand_path
  @destination_directory = options[:destination_directory]
  @style_destination_directory = options[:style_destination_directory]
  @preserve_structure = options[:preserve_structure] if options.key?(:preserve_structure)
  @insert_title_heading = options[:insert_title_heading] if options.key?(:insert_title_heading)
  @autodrop = options[:autodrop] if options.key?(:autodrop)
  @navigation = options[:navigation] if options.key?(:navigation)
  @navigation_depth = options[:navigation_depth] if options.key?(:navigation_depth)
  @navigation_title = options[:navigation_title]
end

Instance Attribute Details

#autodropObject

Returns the value of attribute autodrop.



18
19
20
# File 'lib/mint/config.rb', line 18

def autodrop
  @autodrop
end

#destination_directoryObject

Returns the value of attribute destination_directory.



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

def destination_directory
  @destination_directory
end

#filesObject

Returns the value of attribute files.



5
6
7
# File 'lib/mint/config.rb', line 5

def files
  @files
end

#helpObject

Returns the value of attribute help.



7
8
9
# File 'lib/mint/config.rb', line 7

def help
  @help
end

#insert_title_headingObject

Returns the value of attribute insert_title_heading.



17
18
19
# File 'lib/mint/config.rb', line 17

def insert_title_heading
  @insert_title_heading
end

#layout_nameObject

Returns the value of attribute layout_name.



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

def layout_name
  @layout_name
end

Returns the value of attribute navigation.



19
20
21
# File 'lib/mint/config.rb', line 19

def navigation
  @navigation
end

Returns the value of attribute navigation_depth.



20
21
22
# File 'lib/mint/config.rb', line 20

def navigation_depth
  @navigation_depth
end

Returns the value of attribute navigation_title.



21
22
23
# File 'lib/mint/config.rb', line 21

def navigation_title
  @navigation_title
end

#output_file_formatObject

Returns the value of attribute output_file_format.



12
13
14
# File 'lib/mint/config.rb', line 12

def output_file_format
  @output_file_format
end

#preserve_structureObject

Returns the value of attribute preserve_structure.



16
17
18
# File 'lib/mint/config.rb', line 16

def preserve_structure
  @preserve_structure
end

#stdin_modeObject

Returns the value of attribute stdin_mode.



6
7
8
# File 'lib/mint/config.rb', line 6

def stdin_mode
  @stdin_mode
end

#style_destination_directoryObject

Returns the value of attribute style_destination_directory.



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

def style_destination_directory
  @style_destination_directory
end

#style_modeObject

Returns the value of attribute style_mode.



11
12
13
# File 'lib/mint/config.rb', line 11

def style_mode
  @style_mode
end

#style_nameObject

Returns the value of attribute style_name.



10
11
12
# File 'lib/mint/config.rb', line 10

def style_name
  @style_name
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/mint/config.rb', line 8

def verbose
  @verbose
end

#working_directoryObject

This can’t be set by the user



13
14
15
# File 'lib/mint/config.rb', line 13

def working_directory
  @working_directory
end

Class Method Details

.defaultsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mint/config.rb', line 99

def self.defaults
  dest_dir = DEFAULT_DESTINATION_DIRECTORY.call
  Config.new({
    stdin_mode: DEFAULT_STDIN_MODE,
    verbose: DEFAULT_VERBOSE,
    layout_name: DEFAULT_LAYOUT_NAME,
    style_name: DEFAULT_STYLE_NAME,
    style_mode: DEFAULT_STYLE_MODE,
    output_file_format: DEFAULT_OUTPUT_FILE_FORMAT,
    working_directory: DEFAULT_WORKING_DIRECTORY.call,
    destination_directory: dest_dir,
    style_destination_directory: Pathname.new('.'),
    preserve_structure: DEFAULT_PRESERVE_STRUCTURE,
    insert_title_heading: DEFAULT_INSERT_TITLE_HEADING,
    autodrop: DEFAULT_AUTODROP,
    navigation: DEFAULT_NAVIGATION,
    navigation_depth: DEFAULT_NAVIGATION_DEPTH,
    navigation_title: DEFAULT_NAVIGATION_TITLE
  })
end

.ensure_config(config) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/mint/config.rb', line 82

def self.ensure_config(config)
  case config
  when Config
    config
  when Hash
    Config.new(config)
  else
    raise ArgumentError, "config must be a Config object or Hash"
  end
end

.load_file(file) ⇒ Object



93
94
95
96
97
# File 'lib/mint/config.rb', line 93

def self.load_file(file)
  toml_config = TOML.load_file(file)
  mapped_config = map_toml_keys_to_config(toml_config) if toml_config.is_a?(Hash)
  Config.new(mapped_config || {})
end

.with_defaults(overrides = {}) ⇒ Object



120
121
122
# File 'lib/mint/config.rb', line 120

def self.with_defaults(overrides = {})
  defaults.merge(new(overrides))
end

Instance Method Details

#merge(config = Config.new) ⇒ Object



78
79
80
# File 'lib/mint/config.rb', line 78

def merge(config = Config.new)
  Config.new(to_h.merge(config.to_h.reject {|_, v| v.nil? }))
end

#to_hObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mint/config.rb', line 57

def to_h
  {
    stdin_mode: @stdin_mode,
    help: @help,
    verbose: @verbose,
    layout_name: @layout_name,
    style_name: @style_name,
    style_mode: @style_mode,
    output_file_format: @output_file_format,
    working_directory: @working_directory,
    destination_directory: @destination_directory,
    style_destination_directory: @style_destination_directory,
    preserve_structure: @preserve_structure,
    insert_title_heading: @insert_title_heading,
    autodrop: @autodrop,
    navigation: @navigation,
    navigation_depth: @navigation_depth,
    navigation_title: @navigation_title
  }
end