Class: CSVPlusPlus::ValidatedModifier

Inherits:
Modifier
  • Object
show all
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

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’.

Parameters:

  • value (String)

    The unvalidated user input



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.

Parameters:

  • value (String)

    The unvalidated user input



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’.

Parameters:

  • value (String)

    The unvalidated user input



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.

Parameters:

  • value (String)

    The unvalidated user input



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.

Parameters:

  • value (String)

    The unvalidated user input



45
46
47
# File 'lib/csv_plus_plus/validated_modifier.rb', line 45

def expand=(value)
  super(::CSVPlusPlus::Expand.new(positive_integer(:expand, value)))
end

#fontcolor=(value) ⇒ Object

Validates that fontcolor is a hex color.

Parameters:

  • value (String)

    The unvalidated user input



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

Parameters:

  • value (String)

    The unvalidated user input



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’.

Parameters:

  • value (String)

    The unvalidated user input



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’.

Parameters:

  • value (String)

    The unvalidated user input



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’.

Parameters:

  • value (String)

    The unvalidated user input



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:

Parameters:

  • value (String)

    The unvalidated user input



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’.

Parameters:

  • value (String)

    The unvalidated user input



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.

Parameters:

  • value (String)

    The unvalidated user input



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