Class: CSVPlusPlus::Modifier::ModifierValidator

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

Instance Method Summary collapse

Constructor Details

#initialize(modifier) ⇒ ModifierValidator

Returns a new instance of ModifierValidator.

Parameters:



21
22
23
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 21

def initialize(modifier)
  @modifier = modifier
end

Instance Attribute Details

#modifierObject (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’.

Parameters:

Returns:



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.

Parameters:

  • border_color (::String)

    The unvalidated user input



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

Parameters:

  • border_style (::String)

    The unvalidated user input



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.

Parameters:

  • color (::String)

    The unvalidated user input



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.

Parameters:

  • repetitions (String)

    The unvalidated user input



69
70
71
# File 'lib/csv_plus_plus/modifier/modifier_validator.rb', line 69

def expand=(repetitions)
  @modifier.expand = ::CSVPlusPlus::Modifier::Expand.new(repetitions: positive_integer(:expand, repetitions))
end

#fontcolor=(font_color) ⇒ Object

Validates that font_color is a hex color.

Parameters:

  • font_color (::String)

    The unvalidated user input



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

Parameters:

  • font_family (::String)

    The unvalidated user input



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

Parameters:

  • font_size (::String)

    The unvalidated user input



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

Parameters:

  • text_format (::String)

    The unvalidated user input



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

Parameters:

  • halign (::String)

    The unvalidated user input



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 infinite_expand!
  @modifier.infinite_expand!
end

#note=(note) ⇒ Object

Validates that note is a quoted string.

Parameters:

  • note (::String)

    The unvalidated user input



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

Parameters:

  • number_format (::String)

    The unvalidated user input



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:

Parameters:

  • rule (::String)

    The validation rule to apply to this row or cell

See Also:



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

Parameters:

  • valign (::String)

    The unvalidated user input



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.

Parameters:

  • var (::String)

    The unvalidated user input



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