Class: Xlsxrb::StyleBuilder
- Inherits:
-
Object
- Object
- Xlsxrb::StyleBuilder
- Defined in:
- lib/xlsxrb/style_builder.rb
Overview
Helper class for building cell styles with a fluent DSL. Encapsulates font, fill, border, alignment, and number format properties.
Instance Attribute Summary collapse
-
#alignment ⇒ Object
readonly
Returns the value of attribute alignment.
-
#border_props ⇒ Object
readonly
Returns the value of attribute border_props.
-
#fill_props ⇒ Object
readonly
Returns the value of attribute fill_props.
-
#font_props ⇒ Object
readonly
Returns the value of attribute font_props.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#num_fmt_id ⇒ Object
readonly
Returns the value of attribute num_fmt_id.
Instance Method Summary collapse
-
#align_horizontal(value) ⇒ Object
--- Alignment Properties ---.
- #align_vertical(value) ⇒ Object
-
#apply_options!(**opts) ⇒ Object
Applies option-style definitions so callers can use add_style(name, **opts) as an alternative to block-based fluent chaining.
-
#bold(value = true) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter.
-
#border_all(style: "thin", color: nil) ⇒ Object
--- Border Properties ---.
- #border_bottom(style: "thin", color: nil) ⇒ Object
-
#border_diagonal(style: "thin", color: nil, up: false, down: false) ⇒ Object
rubocop:disable Naming/MethodParameterName.
- #border_left(style: "thin", color: nil) ⇒ Object
- #border_right(style: "thin", color: nil) ⇒ Object
- #border_top(style: "thin", color: nil) ⇒ Object
- #fill(pattern: "solid", fg_color: nil, bg_color: nil) ⇒ Object
- #fill_color(color) ⇒ Object
- #fill_gradient(type:, degree: nil, stops: []) ⇒ Object
-
#fill_pattern(pattern, fg_color: nil, bg_color: nil) ⇒ Object
--- Fill Properties ---.
- #font_color(color) ⇒ Object
- #font_name(name) ⇒ Object
- #indent(value) ⇒ Object
-
#initialize(name = nil) ⇒ StyleBuilder
constructor
A new instance of StyleBuilder.
- #italic(value = true) ⇒ Object
-
#number_format(num_fmt_id) ⇒ Object
(also: #num_fmt)
--- Number Format ---.
-
#register_with(writer) ⇒ Object
Register this style with the given Writer, returning the style_id.
- #shrink_to_fit(value = true) ⇒ Object
- #size(size_value) ⇒ Object
- #strike(value = true) ⇒ Object
-
#text_rotation(value) ⇒ Object
rubocop:enable Style/OptionalBooleanParameter.
- #underline(val = "single") ⇒ Object
- #vert_align(value) ⇒ Object
-
#wrap_text(value = true) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter.
Constructor Details
#initialize(name = nil) ⇒ StyleBuilder
Returns a new instance of StyleBuilder.
7 8 9 10 11 12 13 14 |
# File 'lib/xlsxrb/style_builder.rb', line 7 def initialize(name = nil) @name = name @font_props = {} @fill_props = {} @border_props = {} @num_fmt_id = nil @alignment = {} end |
Instance Attribute Details
#alignment ⇒ Object (readonly)
Returns the value of attribute alignment.
16 17 18 |
# File 'lib/xlsxrb/style_builder.rb', line 16 def alignment @alignment end |
#border_props ⇒ Object (readonly)
Returns the value of attribute border_props.
16 17 18 |
# File 'lib/xlsxrb/style_builder.rb', line 16 def border_props @border_props end |
#fill_props ⇒ Object (readonly)
Returns the value of attribute fill_props.
16 17 18 |
# File 'lib/xlsxrb/style_builder.rb', line 16 def fill_props @fill_props end |
#font_props ⇒ Object (readonly)
Returns the value of attribute font_props.
16 17 18 |
# File 'lib/xlsxrb/style_builder.rb', line 16 def font_props @font_props end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/xlsxrb/style_builder.rb', line 16 def name @name end |
#num_fmt_id ⇒ Object (readonly)
Returns the value of attribute num_fmt_id.
16 17 18 |
# File 'lib/xlsxrb/style_builder.rb', line 16 def num_fmt_id @num_fmt_id end |
Instance Method Details
#align_horizontal(value) ⇒ Object
--- Alignment Properties ---
173 174 175 176 |
# File 'lib/xlsxrb/style_builder.rb', line 173 def align_horizontal(value) @alignment[:horizontal] = value self end |
#align_vertical(value) ⇒ Object
178 179 180 181 |
# File 'lib/xlsxrb/style_builder.rb', line 178 def align_vertical(value) @alignment[:vertical] = value self end |
#apply_options!(**opts) ⇒ Object
Applies option-style definitions so callers can use add_style(name, **opts) as an alternative to block-based fluent chaining.
20 21 22 23 24 25 26 27 28 29 30 31 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 |
# File 'lib/xlsxrb/style_builder.rb', line 20 def (**opts) bold(opts[:bold]) if opts.key?(:bold) italic(opts[:italic]) if opts.key?(:italic) size(opts[:size]) if opts.key?(:size) font_name(opts[:font_name]) if opts.key?(:font_name) font_color(opts[:font_color]) if opts.key?(:font_color) underline(opts[:underline]) if opts.key?(:underline) strike(opts[:strike]) if opts.key?(:strike) vert_align(opts[:vert_align]) if opts.key?(:vert_align) fill_color(opts[:fill_color]) if opts.key?(:fill_color) if opts.key?(:fill_pattern) pattern = opts[:fill_pattern] || {} fill_pattern( pattern[:pattern] || "solid", fg_color: pattern[:fg_color], bg_color: pattern[:bg_color] ) end fill_gradient(**opts[:fill_gradient]) if opts.key?(:fill_gradient) && opts[:fill_gradient] border_all(**opts[:border_all]) if opts.key?(:border_all) && opts[:border_all] border_left(**opts[:border_left]) if opts.key?(:border_left) && opts[:border_left] border_right(**opts[:border_right]) if opts.key?(:border_right) && opts[:border_right] border_top(**opts[:border_top]) if opts.key?(:border_top) && opts[:border_top] border_bottom(**opts[:border_bottom]) if opts.key?(:border_bottom) && opts[:border_bottom] number_format(opts[:number_format]) if opts.key?(:number_format) align_horizontal(opts[:align_horizontal]) if opts.key?(:align_horizontal) align_vertical(opts[:align_vertical]) if opts.key?(:align_vertical) wrap_text(opts[:wrap_text]) if opts.key?(:wrap_text) text_rotation(opts[:text_rotation]) if opts.key?(:text_rotation) indent(opts[:indent]) if opts.key?(:indent) shrink_to_fit(opts[:shrink_to_fit]) if opts.key?(:shrink_to_fit) self end |
#bold(value = true) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter
62 63 64 65 |
# File 'lib/xlsxrb/style_builder.rb', line 62 def bold(value = true) @font_props[:bold] = value self end |
#border_all(style: "thin", color: nil) ⇒ Object
--- Border Properties ---
133 134 135 136 137 138 139 140 |
# File 'lib/xlsxrb/style_builder.rb', line 133 def border_all(style: "thin", color: nil) color_opt = color ? { color: color } : {} @border_props[:left] = { style: style, **color_opt } @border_props[:right] = { style: style, **color_opt } @border_props[:top] = { style: style, **color_opt } @border_props[:bottom] = { style: style, **color_opt } self end |
#border_bottom(style: "thin", color: nil) ⇒ Object
157 158 159 160 |
# File 'lib/xlsxrb/style_builder.rb', line 157 def border_bottom(style: "thin", color: nil) @border_props[:bottom] = { style: style, color: color }.compact self end |
#border_diagonal(style: "thin", color: nil, up: false, down: false) ⇒ Object
rubocop:disable Naming/MethodParameterName
163 164 165 166 167 168 |
# File 'lib/xlsxrb/style_builder.rb', line 163 def border_diagonal(style: "thin", color: nil, up: false, down: false) @border_props[:diagonal] = { style: style, color: color }.compact @border_props[:diagonal_up] = true if up @border_props[:diagonal_down] = true if down self end |
#border_left(style: "thin", color: nil) ⇒ Object
142 143 144 145 |
# File 'lib/xlsxrb/style_builder.rb', line 142 def border_left(style: "thin", color: nil) @border_props[:left] = { style: style, color: color }.compact self end |
#border_right(style: "thin", color: nil) ⇒ Object
147 148 149 150 |
# File 'lib/xlsxrb/style_builder.rb', line 147 def border_right(style: "thin", color: nil) @border_props[:right] = { style: style, color: color }.compact self end |
#border_top(style: "thin", color: nil) ⇒ Object
152 153 154 155 |
# File 'lib/xlsxrb/style_builder.rb', line 152 def border_top(style: "thin", color: nil) @border_props[:top] = { style: style, color: color }.compact self end |
#fill(pattern: "solid", fg_color: nil, bg_color: nil) ⇒ Object
118 119 120 |
# File 'lib/xlsxrb/style_builder.rb', line 118 def fill(pattern: "solid", fg_color: nil, bg_color: nil) fill_pattern(pattern, fg_color: fg_color, bg_color: bg_color) end |
#fill_color(color) ⇒ Object
112 113 114 115 116 |
# File 'lib/xlsxrb/style_builder.rb', line 112 def fill_color(color) @fill_props[:pattern] = "solid" @fill_props[:fg_color] = color self end |
#fill_gradient(type:, degree: nil, stops: []) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/xlsxrb/style_builder.rb', line 122 def fill_gradient(type:, degree: nil, stops: []) @fill_props[:gradient] = { type: type, degree: degree, stops: stops }.compact self end |
#fill_pattern(pattern, fg_color: nil, bg_color: nil) ⇒ Object
--- Fill Properties ---
105 106 107 108 109 110 |
# File 'lib/xlsxrb/style_builder.rb', line 105 def fill_pattern(pattern, fg_color: nil, bg_color: nil) @fill_props[:pattern] = pattern @fill_props[:fg_color] = fg_color if fg_color @fill_props[:bg_color] = bg_color if bg_color self end |
#font_color(color) ⇒ Object
82 83 84 85 |
# File 'lib/xlsxrb/style_builder.rb', line 82 def font_color(color) @font_props[:color] = color self end |
#font_name(name) ⇒ Object
77 78 79 80 |
# File 'lib/xlsxrb/style_builder.rb', line 77 def font_name(name) @font_props[:name] = name self end |
#indent(value) ⇒ Object
200 201 202 203 |
# File 'lib/xlsxrb/style_builder.rb', line 200 def indent(value) @alignment[:indent] = value.to_i self end |
#italic(value = true) ⇒ Object
67 68 69 70 |
# File 'lib/xlsxrb/style_builder.rb', line 67 def italic(value = true) @font_props[:italic] = value self end |
#number_format(num_fmt_id) ⇒ Object Also known as: num_fmt
--- Number Format ---
207 208 209 210 |
# File 'lib/xlsxrb/style_builder.rb', line 207 def number_format(num_fmt_id) @num_fmt_id = num_fmt_id self end |
#register_with(writer) ⇒ Object
Register this style with the given Writer, returning the style_id.
- writer
Xlsxrb::Ooxml::Writer instance
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/xlsxrb/style_builder.rb', line 215 def register_with(writer) font_id = 0 fill_id = 0 border_id = 0 font_id = writer.add_font(**@font_props) if @font_props.any? fill_id = writer.add_fill(**@fill_props) if @fill_props.any? border_id = writer.add_border(**@border_props) if @border_props.any? resolved_num_fmt_id = if @num_fmt_id.is_a?(String) writer.add_number_format(@num_fmt_id) else @num_fmt_id end cell_style_opts = { num_fmt_id: resolved_num_fmt_id, font_id: font_id, fill_id: fill_id, border_id: border_id } cell_style_opts[:alignment] = @alignment if @alignment.any? writer.add_cell_style(**cell_style_opts) end |
#shrink_to_fit(value = true) ⇒ Object
189 190 191 192 |
# File 'lib/xlsxrb/style_builder.rb', line 189 def shrink_to_fit(value = true) @alignment[:shrink_to_fit] = value self end |
#size(size_value) ⇒ Object
72 73 74 75 |
# File 'lib/xlsxrb/style_builder.rb', line 72 def size(size_value) @font_props[:sz] = size_value.to_i self end |
#strike(value = true) ⇒ Object
92 93 94 95 |
# File 'lib/xlsxrb/style_builder.rb', line 92 def strike(value = true) @font_props[:strike] = value self end |
#text_rotation(value) ⇒ Object
rubocop:enable Style/OptionalBooleanParameter
195 196 197 198 |
# File 'lib/xlsxrb/style_builder.rb', line 195 def text_rotation(value) @alignment[:text_rotation] = value self end |
#underline(val = "single") ⇒ Object
87 88 89 90 |
# File 'lib/xlsxrb/style_builder.rb', line 87 def underline(val = "single") @font_props[:underline] = val self end |
#vert_align(value) ⇒ Object
97 98 99 100 |
# File 'lib/xlsxrb/style_builder.rb', line 97 def vert_align(value) @font_props[:vert_align] = value self end |
#wrap_text(value = true) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter
184 185 186 187 |
# File 'lib/xlsxrb/style_builder.rb', line 184 def wrap_text(value = true) @alignment[:wrap_text] = value self end |