Class: KType::NamedFolders
- Inherits:
-
Object
- Object
- KType::NamedFolders
- Includes:
- KLog::Logging
- Defined in:
- lib/k_type/named_folders.rb
Overview
Named folders allow folders to be stored with easy to remember names/alias’s Secondarily, you can also build up file names based on these named folders.
Named folders makes sense for generated/output folders because you may want more than one type of location to generate output.
Don’t confuse multiple named output folders with sub-paths, when you want to build up a file name in a child folder, you can do that as part of building the filename.
The idea behind named folders is for when you have two or more totally different outputs that (may live in the a similar location) or live in different locations. Samples:
name: :code - generating source code into a project
name: :slide - generating slide deck into a documentation folder
name: :webpack - folder where you might generate webpack files, e.g. webpack.config.*.json
example:
folders = NamedFolders.new
folders.add(:csharp , '~/dev/csharp/cool-project')
folders.add(:package_json , :csharp)
folders.add(:webpack , folders.join(:csharp, 'config'))
folders.add(:builder , folders.join(:csharp, 'builder'))
folders.add(:slides , '~/doc/csharp/cool-project')
puts folders.get(:builder)
puts folders.get_filename(:csharp, 'Program.cs')
puts folders.get_filename(:csharp, 'Models/Order.cs')
puts folders.get_filename(:csharp, 'Models', 'Order.cs')
Do I need to support :default?
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current ⇒ Object
Returns the value of attribute current.
-
#folders ⇒ Object
readonly
Returns the value of attribute folders.
Instance Method Summary collapse
-
#add(folder_key, *folder_parts) ⇒ Object
Add support for file_parts.
- #current_folder ⇒ Object
- #debug(title: 'named folders') ⇒ Object
- #folder_keys ⇒ Object
- #folder_paths ⇒ Object
-
#get(folder_key) ⇒ Object
Get a folder.
-
#initialize ⇒ NamedFolders
constructor
A new instance of NamedFolders.
- #initialize_copy(orig) ⇒ Object
-
#join(folder_key, *file_folder_parts) ⇒ Object
(also: #get_filename)
Join the lookup folder key with the subpath folder parts (optionally + filename) and return the folder or filename.
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ NamedFolders
Returns a new instance of NamedFolders.
43 44 45 46 |
# File 'lib/k_type/named_folders.rb', line 43 def initialize @folders = {} @current = nil end |
Instance Attribute Details
#current ⇒ Object
Returns the value of attribute current.
41 42 43 |
# File 'lib/k_type/named_folders.rb', line 41 def current @current end |
#folders ⇒ Object (readonly)
Returns the value of attribute folders.
39 40 41 |
# File 'lib/k_type/named_folders.rb', line 39 def folders @folders end |
Instance Method Details
#add(folder_key, *folder_parts) ⇒ Object
Add support for file_parts
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/k_type/named_folders.rb', line 66 def add(folder_key, *folder_parts) # get a predefined folder by symbol folder = join_folder_parts(folder_parts) if folder.is_a?(Symbol) folder = get(folder) elsif folder.start_with?('~') folder = File.(folder) end @current = folder_key if @current.nil? folders[folder_key] = folder end |
#current_folder ⇒ Object
59 60 61 62 63 |
# File 'lib/k_type/named_folders.rb', line 59 def current_folder return nil unless @current get(@current) end |
#debug(title: 'named folders') ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/k_type/named_folders.rb', line 108 def debug(title: 'named folders') log.section_heading title log.kv 'current', current log.info '' folders.each_key do |key| folder = folders[key] log.kv key.to_s, folder end nil end |
#folder_keys ⇒ Object
96 97 98 |
# File 'lib/k_type/named_folders.rb', line 96 def folder_keys @folders.keys end |
#folder_paths ⇒ Object
100 101 102 |
# File 'lib/k_type/named_folders.rb', line 100 def folder_paths @folders.values end |
#get(folder_key) ⇒ Object
Get a folder
80 81 82 83 |
# File 'lib/k_type/named_folders.rb', line 80 def get(folder_key) guard_folder_key(folder_key) folders[folder_key] end |
#initialize_copy(orig) ⇒ Object
48 49 50 51 52 |
# File 'lib/k_type/named_folders.rb', line 48 def initialize_copy(orig) super(orig) @folders = orig.folders.clone end |
#join(folder_key, *file_folder_parts) ⇒ Object Also known as: get_filename
Join the lookup folder key with the subpath folder parts (optionally + filename) and return the folder or filename
Return fully qualified filename
88 89 90 91 92 |
# File 'lib/k_type/named_folders.rb', line 88 def join(folder_key, *file_folder_parts) folder = get(folder_key) File.join(folder, file_folder_parts) end |
#to_h ⇒ Object
104 105 106 |
# File 'lib/k_type/named_folders.rb', line 104 def to_h @folders end |