New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Our pairing pass on validating the attributes. #3

Merged
merged 1 commit into from Jul 2, 2013
Jump to file or symbol
Failed to load files and symbols.
+27 −8
Diff settings

Always

Just for now

View
@@ -57,15 +57,21 @@ module Dbd
# in RDF. Probably more detailed modeling using RDF object will follow.
class Fact
def self.attribute_formats
{
id: [true, Helpers::UUID.regexp],
time_stamp: [true, TimeStamp.valid_regexp],
provenance_subject: [false, Helpers::UUID.regexp],
subject: [true, Helpers::UUID.regexp],
predicate: [true, /.+/],
object: [true, /.+/]
}
end
##
# @return [Array] The 6 attributes of a Fact.
def self.attributes
[:id,
:time_stamp,
:provenance_subject,
:subject,
:predicate,
:object]
attribute_formats.keys
end
attributes.each do |attribute|
@@ -158,8 +164,17 @@ def string_values
#
# @param [Array] string_values Required : the array with values, organized as in attributes
# @return [Fact, ProvenanceFact] the constructed fact
def self.from_string_values(string_values)
def self.from_string_values(string_values, options={})
string_hash = hash_from_values(string_values)
if options[:validate]
attribute_formats.each do |attr, validation|
mandatory, format = validation
unless !mandatory && string_hash[attr].nil? ||
string_hash[attr].match(format)
raise FactError
end
end
end
fact_from_hash(values_hash(string_hash))
end
View
@@ -56,7 +56,7 @@ def from_CSV(csv)
self.tap do |graph|
CSV.new(csv).each do |row|
# TODO validate the input formats (e.g. invalid uuid codes)
graph << Fact.from_string_values(row)
graph << Fact.from_string_values(row, validate: true)
end
end
end
View
@@ -57,6 +57,10 @@ def self.to_s_regexp
/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d{9} UTC/
end
class << self
alias_method :valid_regexp, :to_s_regexp
end
private
def new_time(larger_than)
ProTip! Use n and p to navigate between commits in a pull request.