Sha256: b6b0614623dac58843ef21ab2a530c772c17d175f464248078f90e5a0d9861b1

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

module TablePal
  class Validate

    attr_reader :meth

    def initialize(meth, options)
      @meth = meth

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

    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

    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

    def class_method
      "Table.#{meth}"
    end

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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_pal-0.3.2 lib/validate.rb
table_pal-0.3.1 lib/validate.rb
table_pal-0.3.0 lib/validate.rb