Class: CSVPlusPlus::ValidatedModifier
- Defined in:
- lib/csv_plus_plus/validated_modifier.rb
Overview
Validates and coerces modifier values 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.
Instance Attribute Summary
Attributes inherited from Modifier
#bordercolor, #borders, #borderstyle, #color, #expand, #fontcolor, #fontfamily, #fontsize, #formats, #halign, #note, #numberformat, #row_level, #validation, #valign, #var
Instance Method Summary collapse
-
#border=(value) ⇒ Object
Validates that
borderis ‘all’, ‘top’, ‘bottom’, ‘left’ or ‘right’. -
#bordercolor=(value) ⇒ Object
Validates that
bordercoloris a hex color. -
#borderstyle=(value) ⇒ Object
Validates that
borderstyleis ‘dashed’, ‘dotted’, ‘double’, ‘solid’, ‘solid_medium’ or ‘solid_thick’. -
#color=(value) ⇒ Object
Validates that
coloris a hex color. -
#expand=(value) ⇒ Object
Validates that
expandis a positive integer. -
#fontcolor=(value) ⇒ Object
Validates that
fontcoloris a hex color. -
#fontfamily=(value) ⇒ Object
Validates that
fontcoloris a hex color. -
#fontsize=(value) ⇒ Object
Validates that
fontsizeis a positive integer. -
#format=(value) ⇒ Object
Validates that
formatis ‘bold’, ‘italic’, ‘strikethrough’ or ‘underline’. -
#halign=(value) ⇒ Object
Validates that
halignis ‘left’, ‘center’ or ‘right’. -
#numberformat=(value) ⇒ Object
Validates that
numberformatis ‘currency’, ‘date’, ‘date_time’, ‘number’, ‘percent’, ‘text’, ‘time’ or ‘scientific’. -
#validation=(value) ⇒ Object
Validates that the conditional validating rules are well-formed.
-
#valign=(value) ⇒ Object
Validates that
valignis ‘top’, ‘center’ or ‘bottom’. -
#var=(value) ⇒ Object
Validates
variableis a valid variable identifier.
Methods inherited from Modifier
#any_border?, #border_all?, #border_along?, #cell_level?, #expand!, #formatted?, #freeze!, #frozen?, #initialize, #row_level!, #row_level?, #take_defaults_from!, #to_s
Constructor Details
This class inherits a constructor from CSVPlusPlus::Modifier
Instance Method Details
#border=(value) ⇒ Object
Validates that border is ‘all’, ‘top’, ‘bottom’, ‘left’ or ‘right’.
17 18 19 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 17 def border=(value) super(one_of(:border, value, %i[all top bottom left right])) end |
#bordercolor=(value) ⇒ Object
Validates that bordercolor is a hex color.
24 25 26 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 24 def bordercolor=(value) super(color_value(:bordercolor, value)) end |
#borderstyle=(value) ⇒ Object
Validates that borderstyle is ‘dashed’, ‘dotted’, ‘double’, ‘solid’, ‘solid_medium’ or ‘solid_thick’.
31 32 33 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 31 def borderstyle=(value) super(one_of(:borderstyle, value, %i[dashed dotted double solid solid_medium solid_thick])) end |
#color=(value) ⇒ Object
Validates that color is a hex color.
38 39 40 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 38 def color=(value) super(color_value(:color, value)) end |
#expand=(value) ⇒ Object
Validates that expand is a positive integer.
45 46 47 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 45 def (value) super(::CSVPlusPlus::Expand.new(positive_integer(:expand, value))) end |
#fontcolor=(value) ⇒ Object
Validates that fontcolor is a hex color.
52 53 54 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 52 def fontcolor=(value) super(color_value(:fontcolor, value)) end |
#fontfamily=(value) ⇒ Object
Validates that fontcolor is a hex color.
57 58 59 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 57 def fontfamily=(value) super(matches_regexp(:fontfamily, unquote(value), /^[\w\s]+$/, 'It is not a valid font family.')) end |
#fontsize=(value) ⇒ Object
Validates that fontsize is a positive integer
64 65 66 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 64 def fontsize=(value) super(positive_integer(:fontsize, value)) end |
#format=(value) ⇒ Object
Validates that format is ‘bold’, ‘italic’, ‘strikethrough’ or ‘underline’.
71 72 73 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 71 def format=(value) super(one_of(:format, value, %i[bold italic strikethrough underline])) end |
#halign=(value) ⇒ Object
Validates that halign is ‘left’, ‘center’ or ‘right’.
78 79 80 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 78 def halign=(value) super(one_of(:halign, value, %i[left center right])) end |
#numberformat=(value) ⇒ Object
Validates that numberformat is ‘currency’, ‘date’, ‘date_time’, ‘number’, ‘percent’, ‘text’, ‘time’ or ‘scientific’.
90 91 92 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 90 def numberformat=(value) super(one_of(:nubmerformat, value, %i[currency date date_time number percent text time scientific])) end |
#validation=(value) ⇒ Object
Validates that the conditional validating rules are well-formed.
Pretty much based off of the Google Sheets API spec here:
106 107 108 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 106 def validation=(value) super(a_data_validation(:validation, value)) end |
#valign=(value) ⇒ Object
Validates that valign is ‘top’, ‘center’ or ‘bottom’.
97 98 99 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 97 def valign=(value) super(one_of(:valign, value, %i[top center bottom])) end |
#var=(value) ⇒ Object
Validates variable is a valid variable identifier.
113 114 115 116 |
# File 'lib/csv_plus_plus/validated_modifier.rb', line 113 def var=(value) # TODO: I need a shared definition of what a variable can be (I guess the :ID token) super(matches_regexp(:var, value, /^\w+$/, 'It must be a sequence of letters, numbers and _.').to_sym) end |