Class: Coradoc::AsciiDoc::Model::Spacing
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Model::Spacing
- Defined in:
- lib/coradoc/asciidoc/model/spacing.rb
Overview
Value object for defining spacing behavior of document elements
Provides a unified way to control spacing around blocks, replacing the scattered spacing_* attributes and trailing_newlines.
Instance Attribute Summary collapse
-
#after_close ⇒ Integer
readonly
Number of newlines after closing delimiter (most common spacing control).
-
#after_open ⇒ Integer
readonly
Number of newlines after opening delimiter (for delimited blocks like ===, ****, etc.).
-
#before_block ⇒ Integer
readonly
Number of newlines before the block element.
-
#before_close ⇒ Integer
readonly
Number of newlines before closing delimiter (for delimited blocks).
-
#trailing_newlines ⇒ String?
readonly
Exact trailing newlines for round-trip preservation When nil, uses calculated spacing from after_close When set to a string, uses that exact value.
Class Method Summary collapse
-
.compact ⇒ Spacing
Compact spacing for tight layouts Used in lists, nested blocks, etc.
-
.default ⇒ Spacing
Default spacing for standard blocks Most blocks have 2 newlines after them.
-
.none ⇒ Spacing
No spacing after the element Used for the last element in a container.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Two spacings are equal if all attributes match.
-
#after_open_newlines ⇒ String
Get newlines after opening delimiter.
-
#before ⇒ String
Get newlines before block.
-
#before_close_newlines ⇒ String
Get newlines before closing delimiter.
-
#compact? ⇒ Boolean
Check if this is compact spacing.
-
#default? ⇒ Boolean
Check if this is the default spacing.
-
#exact_mode? ⇒ Boolean
Check if exact trailing newlines are set.
-
#hash ⇒ Integer
Hash code for use in Hash keys.
-
#initialize(before_block: 0, after_open: 1, before_close: 1, after_close: 2, trailing_newlines: nil) ⇒ Spacing
constructor
Create a new Spacing value object.
-
#inspect ⇒ String
(also: #to_s)
String representation for debugging.
-
#none? ⇒ Boolean
Check if this has no trailing spacing.
-
#trailing ⇒ String
Get the trailing newlines string.
-
#with_after_close(value) ⇒ Spacing
Create a new spacing with modified after_close value.
-
#with_trailing(value) ⇒ Spacing
Create a new spacing with exact trailing newlines.
Constructor Details
#initialize(before_block: 0, after_open: 1, before_close: 1, after_close: 2, trailing_newlines: nil) ⇒ Spacing
Create a new Spacing value object
82 83 84 85 86 87 88 89 90 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 82 def initialize(before_block: 0, after_open: 1, before_close: 1, after_close: 2, trailing_newlines: nil) @before_block = before_block @after_open = after_open @before_close = before_close @after_close = after_close @trailing_newlines = trailing_newlines freeze end |
Instance Attribute Details
#after_close ⇒ Integer (readonly)
Number of newlines after closing delimiter (most common spacing control)
56 57 58 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 56 def after_close @after_close end |
#after_open ⇒ Integer (readonly)
Number of newlines after opening delimiter (for delimited blocks like ===, ****, etc.)
44 45 46 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 44 def after_open @after_open end |
#before_block ⇒ Integer (readonly)
Number of newlines before the block element
38 39 40 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 38 def before_block @before_block end |
#before_close ⇒ Integer (readonly)
Number of newlines before closing delimiter (for delimited blocks)
50 51 52 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 50 def before_close @before_close end |
#trailing_newlines ⇒ String? (readonly)
Exact trailing newlines for round-trip preservation When nil, uses calculated spacing from after_close When set to a string, uses that exact value
63 64 65 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 63 def trailing_newlines @trailing_newlines end |
Class Method Details
.compact ⇒ Spacing
Compact spacing for tight layouts Used in lists, nested blocks, etc.
112 113 114 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 112 def self.compact new(after_close: 1) end |
.default ⇒ Spacing
Default spacing for standard blocks Most blocks have 2 newlines after them
100 101 102 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 100 def self.default new end |
.none ⇒ Spacing
No spacing after the element Used for the last element in a container
124 125 126 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 124 def self.none new(after_close: 0, trailing_newlines: '') end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Two spacings are equal if all attributes match
261 262 263 264 265 266 267 268 269 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 261 def ==(other) return false unless other.is_a?(Spacing) before_block == other.before_block && after_open == other.after_open && before_close == other.before_close && after_close == other.after_close && trailing_newlines == other.trailing_newlines end |
#after_open_newlines ⇒ String
Get newlines after opening delimiter
197 198 199 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 197 def after_open_newlines "\n" * after_open end |
#before ⇒ String
Get newlines before block
189 190 191 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 189 def before "\n" * before_block end |
#before_close_newlines ⇒ String
Get newlines before closing delimiter
205 206 207 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 205 def before_close_newlines "\n" * before_close end |
#compact? ⇒ Boolean
Check if this is compact spacing
144 145 146 147 148 149 150 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 144 def compact? before_block.zero? && after_open == 1 && before_close == 1 && after_close == 1 && trailing_newlines.nil? end |
#default? ⇒ Boolean
Check if this is the default spacing
132 133 134 135 136 137 138 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 132 def default? before_block.zero? && after_open == 1 && before_close == 1 && after_close == 2 && trailing_newlines.nil? end |
#exact_mode? ⇒ Boolean
Check if exact trailing newlines are set
164 165 166 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 164 def exact_mode? !trailing_newlines.nil? end |
#hash ⇒ Integer
Hash code for use in Hash keys
276 277 278 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 276 def hash [before_block, after_open, before_close, after_close, trailing_newlines].hash end |
#inspect ⇒ String Also known as: to_s
String representation for debugging
249 250 251 252 253 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 249 def inspect "#<#{self.class.name} before=#{before_block} after_open=#{after_open} " \ "before_close=#{before_close} after_close=#{after_close} " \ "trailing=#{trailing_newlines.inspect}>" end |
#none? ⇒ Boolean
Check if this has no trailing spacing
156 157 158 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 156 def none? after_close.zero? && trailing_newlines == '' end |
#trailing ⇒ String
Get the trailing newlines string
Returns the exact trailing_newlines if set, otherwise calculates from after_close
179 180 181 182 183 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 179 def trailing return trailing_newlines if exact_mode? "\n" * after_close end |
#with_after_close(value) ⇒ Spacing
Create a new spacing with modified after_close value
217 218 219 220 221 222 223 224 225 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 217 def with_after_close(value) Spacing.new( before_block: before_block, after_open: after_open, before_close: before_close, after_close: value, trailing_newlines: trailing_newlines ) end |
#with_trailing(value) ⇒ Spacing
Create a new spacing with exact trailing newlines
235 236 237 238 239 240 241 242 243 |
# File 'lib/coradoc/asciidoc/model/spacing.rb', line 235 def with_trailing(value) Spacing.new( before_block: before_block, after_open: after_open, before_close: before_close, after_close: after_close, trailing_newlines: value ) end |