Class: Gm::Notepad::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/gm/notepad/configuration.rb

Overview

Global configuration values

Constant Summary collapse

CLI_CONFIG_DEFAULTS =
{
  report_config: false,
  defer_output: false,
  filesystem_directory: '.',
  interactive_buffer: $stderr,
  interactive_color: true,
  output_color: false,
  list_tables: false,
  output_buffer: $stdout,
  paths: ['.'],
  column_delimiter: Gm::Notepad::DEFAULT_COLUMN_DELIMITER,
  shell_prompt: Gm::Notepad::DEFAULT_SHELL_PROMPT,
  skip_readlines: false,
  table_extension: '.txt',
  with_timestamp: false
}.freeze
INTERNAL_CONFIG_DEFAULTS_METHOD_NAMES =

NOTE: ORDER MATTERS! I have a temporal dependency in these defaults

[
  :table_registry,
  :input_handler_registry,
  :input_processor,
  :renderer,
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_handler_registry: nil, table_registry: nil, renderer: nil, input_processor: nil, **overrides) ⇒ Configuration

Returns a new instance of Configuration.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gm/notepad/configuration.rb', line 60

def initialize(input_handler_registry: nil, table_registry: nil, renderer: nil, input_processor: nil, **overrides)
  CLI_CONFIG_DEFAULTS.each_pair do |key, default_value|
    value = overrides.fetch(key, default_value)
    if !value.is_a?(IO)
      value = value.clone
      value.freeze
    end
    self.send("#{key}=", value)
  end
  self.table_registry = (table_registry || default_table_registry)
  self.input_handler_registry = (input_handler_registry || default_input_handler_registry)
  self.renderer = (renderer || default_renderer)
  self.input_processor = (input_processor || default_input_processor)
end

Class Method Details

.init!(target:, from_config: [], additional_params: [], &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gm/notepad/configuration.rb', line 32

def self.init!(target:, from_config: [], additional_params: [], &block)
  # Need to rebind the parameter
  _additional_params = additional_params
  target.define_method(:initialize) do |config: nil, **params|
    @config = config || CLI_CONFIG_DEFAULTS
    from_config.each do |method_name|
      send("#{method_name}=", @config[method_name])
    end
    _additional_params.each do |key|
      send("#{key}=", params.fetch(key))
    end
    instance_exec(&block) if block
  end
  from_config.each do |method_name|
    target.module_exec do
      attr_accessor(method_name)
      protected "#{method_name}="
    end
  end
  _additional_params.each do |method_name|
    target.module_exec do
      attr_accessor(method_name)
      protected "#{method_name}="
    end
  end
  target.attr_reader :config
end

Instance Method Details

#[](value) ⇒ Object



86
87
88
# File 'lib/gm/notepad/configuration.rb', line 86

def [](value)
  public_send(value)
end

#to_hashObject



77
78
79
80
81
82
83
84
# File 'lib/gm/notepad/configuration.rb', line 77

def to_hash
  config = CLI_CONFIG_DEFAULTS.keys.each_with_object({}) do |key, mem|
    mem[key] = send(key)
  end
  INTERNAL_CONFIG_DEFAULTS_METHOD_NAMES.each_with_object(config) do |key, mem|
    mem[key] = send(key)
  end
end