Class: TablePal::Validate

Inherits:
Object
  • Object
show all
Defined in:
lib/validate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meth, options) ⇒ Validate

Returns a new instance of Validate.



6
7
8
9
10
11
12
# File 'lib/validate.rb', line 6

def initialize(meth, options)
  @meth = meth

  options.each do |key, value|
    validate_option(key, value)
  end
end

Instance Attribute Details

#methObject (readonly)

Returns the value of attribute meth.



4
5
6
# File 'lib/validate.rb', line 4

def meth
  @meth
end

Instance Method Details

#class_methodObject



41
42
43
# File 'lib/validate.rb', line 41

def class_method
  "Table.#{meth}"
end

#for_sentence(classes) ⇒ Object



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

def for_sentence(classes)
  classes.map { |klass| "`#{klass}`" }.join(' or ')
end

#validate(key:, value:, classes:) ⇒ Object

Raises:



33
34
35
36
37
38
39
# File 'lib/validate.rb', line 33

def validate(key:, value:, classes:)
  classes = [classes].flatten

  return if classes.any? { |klass| value.is_a?(klass) }

  raise TablePalError, "#{class_method} expected `#{key}:` to be a #{for_sentence(classes)}, not a `#{value.class}`"
end

#validate_option(key, value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/validate.rb', line 14

def validate_option(key, value)
  case key
  when :colour        then validate(key: key, value: value, classes: [Symbol, NilClass])
  when :column        then validate(key: key, value: value, classes: Column)
  when :content       then validate(key: key, value: value, classes: [String, Float, Integer, NilClass])
  when :formatter     then validate(key: key, value: value, classes: Proc)
  when :heading       then validate(key: key, value: value, classes: [TrueClass])
  when :justification then validate(key: key, value: value, classes: Symbol)
  when :left_border   then validate(key: key, value: value, classes: [String, NilClass])
  when :left_padding  then validate(key: key, value: value, classes: String)
  when :right_border  then validate(key: key, value: value, classes: [String, NilClass])
  when :right_padding then validate(key: key, value: value, classes: String)
  when :row           then validate(key: key, value: value, classes: Row)
  when :subheading    then validate(key: key, value: value, classes: [TrueClass, FalseClass])
  when :section_end   then validate(key: key, value: value, classes: [TrueClass, FalseClass])
  else raise TablePalError, "#{class_method} received Unexpected option: `#{key}`"
  end
end