Class: KDirector::Directors::BaseDirector

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/k_director/directors/base_director.rb

Overview

Base Director is paired with the ActionsBuilder and provides a base on which to build code generation directors.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k_builder, builder, **opts) ⇒ BaseDirector

rubocop:disable Metrics/AbcSize



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/k_director/directors/base_director.rb', line 61

def initialize(k_builder, builder, **opts)
  @k_builder  = k_builder
  @builder    = builder
  @options    = OpenStruct.new(**opts)

  @options.director_name        ||= default_director_name
  @options.template_base_folder ||= default_template_base_folder
  @options.on_exist             ||= self.class.on_exist # %i[skip write compare]
  @options.on_action            ||= self.class.on_action # %i[queue execute]
  @options.active = true unless defined?(@options.active)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



56
57
58
# File 'lib/k_director/directors/base_director.rb', line 56

def builder
  @builder
end

#k_builderObject (readonly)

Returns the value of attribute k_builder.



57
58
59
# File 'lib/k_director/directors/base_director.rb', line 57

def k_builder
  @k_builder
end

#optionsObject (readonly)

Returns the value of attribute options.



58
59
60
# File 'lib/k_director/directors/base_director.rb', line 58

def options
  @options
end

Class Method Details

.builder_typeObject



31
32
33
34
35
# File 'lib/k_director/directors/base_director.rb', line 31

def builder_type
  return @builder_type if defined? @builder_type

  @builder_type = KDirector::Builders::ActionsBuilder
end

.default_builder_type(type) ⇒ Object



27
28
29
# File 'lib/k_director/directors/base_director.rb', line 27

def default_builder_type(type)
  @builder_type = type
end

.default_on_action(on_action) ⇒ Object

valid values %i[queue execute]



47
48
49
# File 'lib/k_director/directors/base_director.rb', line 47

def default_on_action(on_action)
  @on_action = on_action
end

.default_on_exist(on_exist) ⇒ Object

valid values %i[skip write compare]



38
39
40
# File 'lib/k_director/directors/base_director.rb', line 38

def default_on_exist(on_exist)
  @on_exist = on_exist
end

.defaults(**opts) ⇒ Object



21
22
23
24
25
# File 'lib/k_director/directors/base_director.rb', line 21

def defaults(**opts)
  default_builder_type(opts[:builder_type]) if opts[:builder_type]
  default_on_exist(opts[:on_exist]) if opts[:on_exist]
  default_on_action(opts[:on_action]) if opts[:on_action]
end

.init(k_builder, builder = nil, **opts) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/k_director/directors/base_director.rb', line 11

def init(k_builder, builder = nil, **opts)
  if builder.nil?
    builder = builder_type.new
  else
    builder.reset
  end

  new(k_builder, builder, **opts)
end

.on_actionObject



51
52
53
# File 'lib/k_director/directors/base_director.rb', line 51

def on_action
  @on_action ||= :queue
end

.on_existObject



42
43
44
# File 'lib/k_director/directors/base_director.rb', line 42

def on_exist
  @on_exist ||= :skip
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/k_director/directors/base_director.rb', line 114

def active?
  @options.active == true
end

#add(output_file, **opts) ⇒ Object

Add a single file into the code base

This is a wrapper around add_file that will add the file to the codebase using template path rules

Parameters:

  • output_filename (String)

    The output file name, this can be a relative path

  • **opts (Hash)

    The options

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :template_filename (String)

    Template filename can be set or it will default to the same value as the output file name

  • :template_subfolder (String)

    Template subfolder



146
147
148
149
150
151
152
153
154
155
# File 'lib/k_director/directors/base_director.rb', line 146

def add(output_file, **opts)
  template_file = opts[:template_file] || output_file
  template_parts = [template_base_folder, opts[:template_subfolder], template_file].reject(&:blank?)
  template_path = File.join(*template_parts)

  # maybe template_file should be renamed to template_path in k_builder
  opts[:template_file] = template_path

  add_file(output_file, **opts)
end

#add_clipboard(**opts) ⇒ Object Also known as: clipboard_copy, clipboard

Add content to the clipboard

Extra options will be used as data for templates, e.g

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :content (String)

    Supply the content that you want to write to the file

  • :template (String)

    Supply the template that you want to write to the file, template will be processed (‘nobody’) From address

  • :content_file (String)

    File with content, file location is based on where the program is running

  • :template_file (String)

    File with handlebars templated content that will be transformed, file location is based on the configured template_path

  • :to (String)

    Recipient email

  • :body (String)

    The email’s body



203
204
205
206
207
208
# File 'lib/k_director/directors/base_director.rb', line 203

def add_clipboard(**opts)
  # RUN (not handle), current folder effects subsequent actions and so it needs to be executed straight away.
  run_action(k_builder.add_clipboard_action(**opts))

  self
end

#add_file(file, **opts) ⇒ Object

Add a file to target folder



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/k_director/directors/base_director.rb', line 170

def add_file(file, **opts)
  opts = {
    on_exist: on_exist
  }.merge(opts)

  opts[:dom] = dom.except(:actions) if dom

  handle_action(k_builder.add_file_action(file, **opts))

  self
end

#blueprint(**opts, &block) ⇒ Object



248
249
250
251
252
253
# File 'lib/k_director/directors/base_director.rb', line 248

def blueprint(**opts, &block)
  blueprint = KDirector::Dsls::Children::Blueprint.new(self, **opts)
  blueprint.instance_eval(&block) if blueprint.active? && block_given?

  self
end

#configurationObject



110
111
112
# File 'lib/k_director/directors/base_director.rb', line 110

def configuration
  k_builder.configuration
end

#data(name = nil, **opts) ⇒ Object

rubocop:enable Metrics/AbcSize



74
75
76
77
78
# File 'lib/k_director/directors/base_director.rb', line 74

def data(name = nil, **opts)
  KDirector::Directors::Data.new(self, name, **opts)

  self
end

#debugObject



255
256
257
258
259
260
# File 'lib/k_director/directors/base_director.rb', line 255

def debug
  debug_options
  debug_dom

  self
end

#debug_domObject



274
275
276
277
278
279
280
# File 'lib/k_director/directors/base_director.rb', line 274

def debug_dom
  log.section_heading 'DOM'

  builder.debug

  nil
end

#debug_optionsObject



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/k_director/directors/base_director.rb', line 262

def debug_options
  log.section_heading director_name

  h = options.to_h.sort.to_h
  h.each_key do |key|
    # requires k_funky
    log.kv(titleize.call(key.to_s), h[key])
  end

  nil
end

#director_nameObject



118
119
120
# File 'lib/k_director/directors/base_director.rb', line 118

def director_name
  @options.director_name
end

#director_name=(name) ⇒ Object



122
123
124
# File 'lib/k_director/directors/base_director.rb', line 122

def director_name=(name)
  @options.director_name = name
end

#domObject



86
87
88
89
90
# File 'lib/k_director/directors/base_director.rb', line 86

def dom
  return builder.dom if defined?(builder.dom)

  nil
end

#fadd(name, **opts) ⇒ Object



165
166
167
# File 'lib/k_director/directors/base_director.rb', line 165

def fadd(name, **opts)
  add(name, **{ on_exist: :write    }.merge(opts))
end

#github(**opts, &block) ⇒ Object

Common child directors



234
235
236
237
238
239
# File 'lib/k_director/directors/base_director.rb', line 234

def github(**opts, &block)
  github = KDirector::Dsls::Children::Github.new(self, **opts)
  github.instance_eval(&block) if github.active? && block_given?

  self
end

#inherited_opts(**opts) ⇒ Object

Used by child directors to inherit options from parent



101
102
103
104
105
106
107
108
# File 'lib/k_director/directors/base_director.rb', line 101

def inherited_opts(**opts)
  {
    on_exist: @options.on_exist,
    on_action: @options.on_action,
    template_base_folder: @options.template_base_folder,
    active: @options.active
  }.merge(opts)
end

#json_domObject



96
97
98
# File 'lib/k_director/directors/base_director.rb', line 96

def json_dom
  builder.to_json
end

#oadd(name, **opts) ⇒ Object



157
158
159
# File 'lib/k_director/directors/base_director.rb', line 157

def oadd(name, **opts)
  add(name, **{ open: true          }.merge(opts))
end

#on_actionObject



134
135
136
# File 'lib/k_director/directors/base_director.rb', line 134

def on_action
  @options.on_action
end

#on_existObject



130
131
132
# File 'lib/k_director/directors/base_director.rb', line 130

def on_exist
  @options.on_exist
end

#package_json(**opts, &block) ⇒ Object



241
242
243
244
245
246
# File 'lib/k_director/directors/base_director.rb', line 241

def package_json(**opts, &block)
  package_json = KDirector::Dsls::Children::PackageJson.new(self, **opts)
  package_json.instance_eval(&block) if package_json.active? && block_given?

  self
end

#play_actionsObject

play any un-played actions



228
229
230
# File 'lib/k_director/directors/base_director.rb', line 228

def play_actions
  k_builder.play_actions(builder.actions)
end

#run_command(command) ⇒ Object

Run a command using shell, this is useful with command line tools



213
214
215
216
217
# File 'lib/k_director/directors/base_director.rb', line 213

def run_command(command)
  handle_action(k_builder.run_command_action(command))

  self
end

#run_script(script) ⇒ Object

Run a command using Open3.capture2, can be used in place of run_command but is also useful with multiline scripts



221
222
223
224
225
# File 'lib/k_director/directors/base_director.rb', line 221

def run_script(script)
  handle_action(k_builder.run_script_action(script))

  self
end

#set_current_folder_action(folder_key) ⇒ Object Also known as: cd

Set current target folder rubocop:disable Naming/AccessorMethodName



184
185
186
187
188
189
# File 'lib/k_director/directors/base_director.rb', line 184

def set_current_folder_action(folder_key)
  # RUN (not handle), current folder effects subsequent actions and so it needs to be executed straight away.
  run_action(k_builder.set_current_folder_action(folder_key))

  self
end

#settings(**opts) ⇒ Object



80
81
82
83
84
# File 'lib/k_director/directors/base_director.rb', line 80

def settings(**opts)
  KDirector::Directors::Data.new(self, :settings, **opts)

  self
end

#tadd(name, **opts) ⇒ Object



161
162
163
# File 'lib/k_director/directors/base_director.rb', line 161

def tadd(name, **opts)
  add(name, **{ open_template: true }.merge(opts))
end

#template_base_folderObject



126
127
128
# File 'lib/k_director/directors/base_director.rb', line 126

def template_base_folder
  @options.template_base_folder
end

#typed_domObject



92
93
94
# File 'lib/k_director/directors/base_director.rb', line 92

def typed_dom
  builder.build
end