Class: Veritable::Schema

Inherits:
Hash
  • Object
show all
Defined in:
lib/veritable/api.rb

Overview

Represents a schema for a Veritable analysis

A Veritable::Schema is a Hash with some additional convenience methods. Schema objects can be used interchangeably with Hashes of the same structure throughout veritable-ruby.

Methods

type – gets the datatype for a given column validate – checks that the schema is well-formed

See also: dev.priorknowledge.com/docs/client/ruby

Instance Method Summary collapse

Constructor Details

#initialize(data, subset = nil) ⇒ Schema

Initalizes a new Schema from a Hash

Arguments

  • data – the data for the schema as a Hash with the form:

    {'col_1': {type: 'datatype'}, 'col_2': {type: 'datatype'}, ...}
    

where the datatype must be one of ["real", "categorical", "count", "boolean"]

  • subset – a Hash or Array whose keys will be used to limit the columns present in the Schema created from the input data

Returns

A new Veritable::Schema

See also: dev.priorknowledge.com/docs/client/ruby



775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/veritable/api.rb', line 775

def initialize(data, subset=nil)
  begin
    data.each {|k, v|
      if subset.is_a? Array
        self[k] = v if subset.include? k
      elsif subset.is_a? Hash
        self[k] = v if subset.has_key? k
      else
        self[k] = v
      end
    }
  rescue
    begin
      data.to_s
    rescue
      raise VeritableError.new("Initialize schema -- invalid schema data.")
    else
      raise VeritableError.new("Initialize schema -- invalid schema data #{data}.")
    end
  end
end

Instance Method Details

#type(column) ⇒ Object

Convenience accessor for the type of a Schema column

Running schema.type(column) is sugar for schema[column]['type']

Arguments

column – the id of the column whose type we are retrieving

See also: dev.priorknowledge.com/docs/client/ruby



805
# File 'lib/veritable/api.rb', line 805

def type(column); self[column]['type']; end

#validateObject

Validates the schema, checking that it is well-formed

Raises

A Veritable::VeritableError if any column ids or types are invalid.

Returns

nil on success

See also: dev.priorknowledge.com/docs/client/ruby



816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/veritable/api.rb', line 816

def validate
  self.each {|k, v|
    if not k.is_a? String
      begin
        k.to_s
      rescue
        raise VeritableError.new("Validate schema -- Invalid schema specification: nonstring column id.")
      else
        raise VeritableError.new("Validate schema -- Invalid schema specification: nonstring column id #{k}")
      end
    end
    begin
      Util.check_id k
    rescue
      raise VeritableError.new("Validate schema -- Invalid column name #{k}: must contain only alphanumerics, dashes, and underscores, and may not begin with a dash or underscore.")
    end
    if not v.include? 'type'
      raise VeritableError.new("Validate schema -- Invalid schema specification. Column #{k} must specify a 'type', one of #{DATATYPES}")
    end
    if not DATATYPES.include? v['type']
      raise VeritableError.new("Validate schema -- Invalid schema specification. Column #{k}, type #{v['type']} is not valid. Type must be one of #{DATATYPES}")
    end
  }
  nil
end