module Dbd::Fact::Factory

Public Class Methods

attribute_formats() click to toggle source

Formats for checking CSV fields the predicate, object_type and object are tested in the initializer

# File lib/dbd/fact/factory.rb, line 40
def attribute_formats
  {
    id: [true, Fact::ID.valid_regexp],
    time_stamp: [true, TimeStamp.valid_regexp],
    context_subject: [false, Fact::Subject.valid_regexp],
    subject: [true, Fact::Subject.valid_regexp]
  }
end
from_string_values(string_values, options={}) click to toggle source

Constructs a Fact or ContextFact from a string values array (e.g. pulled from a CSV row).

@param [Array] string_values Required : the array with values, organized as in attributes @return [Fact, Context] the constructed fact

# File lib/dbd/fact/factory.rb, line 31
def from_string_values(string_values, options={})
  string_hash = string_hash_from_values(string_values)
  validate_string_hash(string_hash) if options[:validate]
  fact_from_values_hash(values_hash(string_hash))
end
new_id() click to toggle source

@return [String] A new id string.

# File lib/dbd/fact/factory.rb, line 21
def new_id
  top_class::ID.new_id
end
new_subject() click to toggle source

@return [String] A new subject string.

# File lib/dbd/fact/factory.rb, line 15
def new_subject
  top_class::Subject.new_subject
end
top_class() click to toggle source

@return [Class] the top class for which instances are created here.

# File lib/dbd/fact/factory.rb, line 9
def top_class
  Fact
end

Private Class Methods

fact_from_values_hash(values_hash) click to toggle source
# File lib/dbd/fact/factory.rb, line 77
def fact_from_values_hash(values_hash)
  if values_hash[:context_subject]
    Fact.new(values_hash)
  else
    ContextFact.new(values_hash)
  end
end
report_invalid_entry(string) click to toggle source
# File lib/dbd/fact/factory.rb, line 100
def report_invalid_entry(string)
  raise FactError, "invalid entry found : #{string}"
end
string_hash_from_values(string_values) click to toggle source
# File lib/dbd/fact/factory.rb, line 63
def string_hash_from_values(string_values)
  unescaped_values = unescaped_string_values(string_values)
  attributes_strings_array = [top_class.attributes, unescaped_values].transpose
  # Remove empty values (e.g. the context_subject for a ContextFact).
  attributes_strings_array.delete_if{ |_, v| v == '' }
  Hash[attributes_strings_array]
end
unescaped_string(string) click to toggle source
# File lib/dbd/fact/factory.rb, line 55
def unescaped_string(string)
  r = %r{(\\\\|\\n)}
  repl = {
    "\\\\" => "\\",  # double backslash => single backslash
    "\\n" => "\n"}   # backslash n => newline
  string.gsub(r, repl)
end
unescaped_string_values(string_values) click to toggle source
# File lib/dbd/fact/factory.rb, line 51
def unescaped_string_values(string_values)
  string_values.map{ |string_value| unescaped_string(string_value) }
end
validate_string_by_regexp(mandatory, string, format) click to toggle source
# File lib/dbd/fact/factory.rb, line 94
def validate_string_by_regexp(mandatory, string, format)
  if (mandatory || string) && (string !~ format)
    report_invalid_entry(string)
  end
end
validate_string_hash(string_hash) click to toggle source
# File lib/dbd/fact/factory.rb, line 85
def validate_string_hash(string_hash)
  attribute_formats.each do |attr, validation|
    string = string_hash[attr]
    mandatory, format = validation
    validate_string_by_regexp(mandatory, string, format)
  end

end
values_hash(string_hash) click to toggle source
# File lib/dbd/fact/factory.rb, line 71
def values_hash(string_hash)
  string_hash.tap do |h|
    h[:time_stamp] = TimeStamp.new(time: h[:time_stamp])
  end
end