Class: CSVPlusPlus::Modifier::ModifierValidator
- Inherits:
-
Object
- Object
- CSVPlusPlus::Modifier::ModifierValidator
- Extended by:
- T::Sig
- Defined in:
- lib/csv_plus_plus/modifier/modifier_validator.rb
Overview
Validates and coerces modifier user inputs as they are parsed.
Previously this logic was handled in the parser’s grammar, but with the introduction of variable binding the grammar is no longer context free so we need the parser to be a little looser on what it accepts and validate it here. Having this layer is also nice because we can provide better error messages to the user for what went wrong during the parse. rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#modifier ⇒ Object
readonly
Returns the value of attribute modifier.
Instance Method Summary collapse
-
#border=(border_side) ⇒ Set<Modifier::BorderSide>
Validates that
border_side
is ‘all’, ‘top’, ‘bottom’, ‘left’ or ‘right’. -
#bordercolor=(border_color) ⇒ Object
Validates that
border_color
is a hex color. -
#borderstyle=(border_style) ⇒ Object
Validates that
borderstyle
is ‘dashed’, ‘dotted’, ‘double’, ‘solid’, ‘solid_medium’ or ‘solid_thick’. -
#color=(color) ⇒ Object
Validates that
color
is a hex color. -
#expand=(repetitions) ⇒ Object
Validates that
repetitions
is a positive integer. -
#fontcolor=(font_color) ⇒ Object
Validates that
font_color
is a hex color. -
#fontfamily=(font_family) ⇒ Object
Validates that
font_family
is a string that looks like a valid font family. -
#fontsize=(font_size) ⇒ Object
Validates that
font_size
is a positive integer. -
#format=(text_format) ⇒ Object
Validates that
text_format
is ‘bold’, ‘italic’, ‘strikethrough’ or ‘underline’. -
#freeze! ⇒ Object
Sets the row or cell to be frozen.
-
#halign=(halign) ⇒ Object
Validates that
halign
is a string representation ofModifier::HorizontalAlign
. -
#infinite_expand! ⇒ Object
Sets an infinite
Expand
on theModifier
. -
#initialize(modifier) ⇒ ModifierValidator
constructor
A new instance of ModifierValidator.
-
#note=(note) ⇒ Object
Validates that
note
is a quoted string. -
#numberformat=(number_format) ⇒ Object
Validates that
number_format
is a string version of aModifier::NumberFormat
. -
#validate=(rule) ⇒ Object
Validates that the conditional validating rules are well-formed.
-
#valign=(valign) ⇒ Object
Validates that
valign
is a string representation ofModifier::VerticalAlign
. -
#var=(var) ⇒ Object
Validates
var
is a valid variable identifier.
Constructor Details
#initialize(modifier) ⇒ ModifierValidator
Returns a new instance of ModifierValidator.
21 22 23 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 21 def initialize(modifier) @modifier = modifier end |
Instance Attribute Details
#modifier ⇒ Object (readonly)
Returns the value of attribute modifier.
17 18 19 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 17 def modifier @modifier end |
Instance Method Details
#border=(border_side) ⇒ Set<Modifier::BorderSide>
Validates that border_side
is ‘all’, ‘top’, ‘bottom’, ‘left’ or ‘right’.
31 32 33 34 35 36 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 31 def border=(border_side) @modifier.border = ::T.cast( one_of(:border, border_side, ::CSVPlusPlus::Modifier::BorderSide), ::CSVPlusPlus::Modifier::BorderSide ) end |
#bordercolor=(border_color) ⇒ Object
Validates that border_color
is a hex color.
42 43 44 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 42 def bordercolor=(border_color) @modifier.bordercolor = color_value(:bordercolor, border_color) end |
#borderstyle=(border_style) ⇒ Object
Validates that borderstyle
is ‘dashed’, ‘dotted’, ‘double’, ‘solid’, ‘solid_medium’ or ‘solid_thick’.
50 51 52 53 54 55 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 50 def borderstyle=(border_style) @modifier.borderstyle = ::T.cast( one_of(:borderstyle, border_style, ::CSVPlusPlus::Modifier::BorderStyle), ::CSVPlusPlus::Modifier::BorderStyle ) end |
#color=(color) ⇒ Object
Validates that color
is a hex color.
61 62 63 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 61 def color=(color) @modifier.color = color_value(:color, color) end |
#expand=(repetitions) ⇒ Object
Validates that repetitions
is a positive integer.
69 70 71 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 69 def (repetitions) @modifier. = ::CSVPlusPlus::Modifier::Expand.new(repetitions: positive_integer(:expand, repetitions)) end |
#fontcolor=(font_color) ⇒ Object
Validates that font_color
is a hex color.
77 78 79 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 77 def fontcolor=(font_color) @modifier.fontcolor = color_value(:fontcolor, font_color) end |
#fontfamily=(font_family) ⇒ Object
Validates that font_family
is a string that looks like a valid font family. There’s only so much validation we can do here
86 87 88 89 90 91 92 93 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 86 def fontfamily=(font_family) @modifier.fontfamily = matches_regexp( :fontfamily, ::CSVPlusPlus::Lexer.unquote(font_family), /^[\w\s]+$/, 'It is not a valid font family.' ) end |
#fontsize=(font_size) ⇒ Object
Validates that font_size
is a positive integer
99 100 101 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 99 def fontsize=(font_size) @modifier.fontsize = positive_integer(:fontsize, font_size) end |
#format=(text_format) ⇒ Object
Validates that text_format
is ‘bold’, ‘italic’, ‘strikethrough’ or ‘underline’.
107 108 109 110 111 112 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 107 def format=(text_format) @modifier.format = ::T.cast( one_of(:format, text_format, ::CSVPlusPlus::Modifier::TextFormat), ::CSVPlusPlus::Modifier::TextFormat ) end |
#freeze! ⇒ Object
Sets the row or cell to be frozen
116 117 118 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 116 def freeze! @modifier.freeze! end |
#halign=(halign) ⇒ Object
Validates that halign
is a string representation of Modifier::HorizontalAlign
130 131 132 133 134 135 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 130 def halign=(halign) @modifier.halign = ::T.cast( one_of(:halign, halign, ::CSVPlusPlus::Modifier::HorizontalAlign), ::CSVPlusPlus::Modifier::HorizontalAlign ) end |
#infinite_expand! ⇒ Object
Sets an infinite Expand
on the Modifier
.
122 123 124 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 122 def @modifier. end |
#note=(note) ⇒ Object
Validates that note
is a quoted string.
141 142 143 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 141 def note=(note) @modifier.note = note end |
#numberformat=(number_format) ⇒ Object
Validates that number_format
is a string version of a Modifier::NumberFormat
149 150 151 152 153 154 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 149 def numberformat=(number_format) @modifier.numberformat = ::T.cast( one_of(:numberformat, number_format, ::CSVPlusPlus::Modifier::NumberFormat), ::CSVPlusPlus::Modifier::NumberFormat ) end |
#validate=(rule) ⇒ Object
Validates that the conditional validating rules are well-formed.
Pretty much based off of the Google Sheets API spec here:
174 175 176 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 174 def validate=(rule) @modifier.validate = a_data_validation(:validate, rule) end |
#valign=(valign) ⇒ Object
Validates that valign
is a string representation of Modifier::VerticalAlign
160 161 162 163 164 165 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 160 def valign=(valign) @modifier.valign = ::T.cast( one_of(:valign, valign, ::CSVPlusPlus::Modifier::VerticalAlign), ::CSVPlusPlus::Modifier::VerticalAlign ) end |
#var=(var) ⇒ Object
Validates var
is a valid variable identifier.
182 183 184 185 |
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 182 def var=(var) # TODO: I need a shared definition of what a variable can be (I guess the :ID token) @modifier.var = matches_regexp(:var, var, /^\w+$/, 'It must be a sequence of letters, numbers and _.').to_sym end |