Class: StandupMD::Config::Post

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

Overview

The configuration class for chat posting.

Constant Summary collapse

DEFAULTS =

The default posting options.

Returns:

  • (Hash)
{
  default_adapter: :slack,
  title: nil
}.freeze
CONFIG_ATTRIBUTES =

Attributes copied into request-scoped config snapshots.

Returns:

  • (Array<Symbol>)
DEFAULTS.keys.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePost

Initializes the config with default values.



60
61
62
# File 'lib/standup_md/config/post.rb', line 60

def initialize
  reset
end

Instance Attribute Details

#adapter_optionsHash (readonly)

Non-secret adapter options.

Returns:

  • (Hash)


56
57
58
# File 'lib/standup_md/config/post.rb', line 56

def adapter_options
  @adapter_options
end

#adaptersHash (readonly)

Registered adapter classes or instances.

Returns:

  • (Hash)


50
51
52
# File 'lib/standup_md/config/post.rb', line 50

def adapters
  @adapters
end

#default_adapterSymbol

The adapter used when ‘standup –post` is called without a platform.

Returns:

  • (Symbol)


29
30
31
# File 'lib/standup_md/config/post.rb', line 29

def default_adapter
  @default_adapter
end

#titleString?

Format string for posted entry titles.

This only affects messages sent through chat posting adapters. It does not change stored standup markdown files. Use ‘%s` as a placeholder for the normal entry title, such as the entry date. This is useful when chat clients post through workspace apps or bots with shared names like “StandupMD”, but the message should still identify whose standup it is.

Examples:

Include the person’s name after the entry date

StandupMD.config.post.title = "%s - Evan Gray"

Returns:

  • (String, nil)


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

def title
  @title
end

Instance Method Details

#build_adapter(name = nil) ⇒ Object

Builds the adapter requested by name.

Parameters:

  • name (String, Symbol, nil) (defaults to: nil)

Returns:

  • (Object)


132
133
134
135
136
137
138
139
140
141
# File 'lib/standup_md/config/post.rb', line 132

def build_adapter(name = nil)
  adapter_name = (name || default_adapter).to_sym
  adapter = adapters.fetch(adapter_name) do
    raise StandupMD::Post::UnknownAdapter, "No post adapter registered for #{adapter_name}"
  end
  return adapter unless adapter.respond_to?(:new)
  return adapter.new if adapter.instance_method(:initialize).arity.zero?

  adapter.new(options_for(adapter_name))
end

#configure_adapter(name, options = {}) ⇒ Hash

Configures non-secret adapter options.

Parameters:

  • name (String, Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (Hash)


112
113
114
# File 'lib/standup_md/config/post.rb', line 112

def configure_adapter(name, options = {})
  options_for(name).merge!(symbolize_keys(options))
end

#copy_from(config) ⇒ StandupMD::Config::Post

Copies values from another post config.

Parameters:

Returns:



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

def copy_from(config)
  CONFIG_ATTRIBUTES.each do |attribute|
    instance_variable_set("@#{attribute}", config.public_send(attribute))
  end
  @adapters = config.adapters.dup
  @adapter_options = Hash.new { |hash, key| hash[key] = {} }
  config.adapter_options.each do |name, options|
    @adapter_options[name] = options.dup
  end
  self
end

#options_for(name) ⇒ Hash

Returns non-secret options for an adapter.

Parameters:

  • name (String, Symbol)

Returns:

  • (Hash)


122
123
124
# File 'lib/standup_md/config/post.rb', line 122

def options_for(name)
  adapter_options[name.to_sym]
end

#register_adapter(name, adapter) ⇒ Class, Object

Registers a posting adapter.

Parameters:

  • name (String, Symbol)
  • adapter (Class, Object)

Returns:

  • (Class, Object)


101
102
103
# File 'lib/standup_md/config/post.rb', line 101

def register_adapter(name, adapter)
  adapters[name.to_sym] = adapter
end

#resetHash

Sets all config values back to their defaults.

Returns:

  • (Hash)


68
69
70
71
72
73
74
# File 'lib/standup_md/config/post.rb', line 68

def reset
  DEFAULTS.each { |k, v| instance_variable_set("@#{k}", v) }
  @adapters = {}
  @adapter_options = Hash.new { |hash, key| hash[key] = {} }
  register_adapter(:slack, StandupMD::Post::Adapters::Slack)
  DEFAULTS
end