Class: Infobar::Message
- Extended by:
- Tins::Delegate
- Defined in:
- lib/infobar/message.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute format.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
- .directive_default_options ⇒ Object
- .directives ⇒ Object
- .register(directive, **opts, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Message
constructor
A new instance of Message.
- #opts_for(directive) ⇒ Object
- #to_hash ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Message
Returns a new instance of Message.
122 123 124 125 126 |
# File 'lib/infobar/message.rb', line 122 def initialize(opts = {}) @format = opts.delete(:format) or raise ArgumentError, 'format option required' @opts = opts.each_with_object({}) { |(k, v), h| h[k.to_s] = v } end |
Instance Attribute Details
#format ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute format.
145 146 147 |
# File 'lib/infobar/message.rb', line 145 def format @format end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
128 129 130 |
# File 'lib/infobar/message.rb', line 128 def opts @opts end |
Class Method Details
.directive_default_options ⇒ Object
21 22 23 |
# File 'lib/infobar/message.rb', line 21 def ||= {} end |
.directives ⇒ Object
17 18 19 |
# File 'lib/infobar/message.rb', line 17 def directives @directives ||= {} end |
.register(directive, **opts, &block) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/infobar/message.rb', line 9 def register(directive, **opts, &block) directives.key?(directive) and warn "Overwriting old directive #{directive}." directives[directive] = block [directive] = opts self end |
Instance Method Details
#opts_for(directive) ⇒ Object
134 135 136 |
# File 'lib/infobar/message.rb', line 134 def opts_for(directive) @opts.fetch(directive, [directive]) end |
#to_hash ⇒ Object
149 150 151 152 153 |
# File 'lib/infobar/message.rb', line 149 def to_hash { format: format, }.merge(@opts) end |
#to_str ⇒ Object
138 139 140 141 142 143 |
# File 'lib/infobar/message.rb', line 138 def to_str keys = directives.keys.sort_by { |k| -k.size } @format.gsub(/(?<!%)(#{keys * ?|})/) do directives[$1].call($1, opts_for($1)) end end |