Class: Infobar::Message
- Extended by:
- Tins::Delegate
- Defined in:
- lib/infobar/message.rb
Instance Attribute Summary collapse
-
#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
- #directive_default_options ⇒ Object
- #directives ⇒ Object
- #format ⇒ Object (also: #to_s)
-
#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.
119 120 121 |
# File 'lib/infobar/message.rb', line 119 def initialize(opts) @opts = opts.each_with_object({}) { |(k, v), h| h[k.to_s] = v } end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
123 124 125 |
# File 'lib/infobar/message.rb', line 123 def opts @opts end |
Class Method Details
.directive_default_options ⇒ Object
22 23 24 |
# File 'lib/infobar/message.rb', line 22 def @directive_default_options ||= {} end |
.directives ⇒ Object
18 19 20 |
# File 'lib/infobar/message.rb', line 18 def directives @directives ||= {} end |
.register(directive, **opts, &block) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/infobar/message.rb', line 10 def register(directive, **opts, &block) directives.key?(directive) and warn "Overwriting old directive #{directive}." directives[directive] = block [directive] = opts self end |
Instance Method Details
#directive_default_options ⇒ Object
129 130 131 |
# File 'lib/infobar/message.rb', line 129 def self.class. end |
#directives ⇒ Object
125 126 127 |
# File 'lib/infobar/message.rb', line 125 def directives self.class.directives end |
#format ⇒ Object Also known as: to_s
144 145 146 |
# File 'lib/infobar/message.rb', line 144 def format @opts['format'] end |
#opts_for(directive) ⇒ Object
133 134 135 |
# File 'lib/infobar/message.rb', line 133 def opts_for(directive) @opts.fetch(directive, [directive]) end |
#to_hash ⇒ Object
150 151 152 153 154 |
# File 'lib/infobar/message.rb', line 150 def to_hash result = @opts.dup result[:format] = result.delete('format') result end |
#to_str ⇒ Object
137 138 139 140 141 142 |
# File 'lib/infobar/message.rb', line 137 def to_str keys = directives.keys.sort_by { |k| -k.size } format.gsub(/(?<!%)(#{keys * ?|})/) do directives[$1].call($1, opts_for($1)) end end |