class Abrupt::Transformation::Base

base class

Constants

SCHEMA_MAPPING

Attributes

md5[RW]
parent_uri[RW]
result[RW]
uri[RW]
values[RW]

Public Class Methods

customize_to_schema(values) click to toggle source

rubocop:disable all

# File lib/abrupt/transformation/base.rb, line 37
def self.customize_to_schema(values)
  @values = values
  keyname = name.split('::').last.downcase.to_sym
  schema_file = File.join Abrupt.root, 'assets', 'schema', 'v1', "#{keyname}.json"
  return values unless File.exist?(schema_file)
  schema = ::JSON.load(File.read(schema_file)).deep_symbolize_keys
  # :button => ..., :text => {:type => "array", :items => {...}}
  schema[:properties][keyname][:properties].each do |state_key, state_schema|
    set_value(state_key, state_schema, [':state', ":#{keyname}"])
  end
  @values
end
new(parent_uri, uri, values = {}) click to toggle source

Initializes Transformer for Individual Statement for parent_uri & uri. @param parent_uri [Array] the parent uri in array structure of paths @param uri [Array] the uri as array structure of path and id @example Readability.new([

  'Website',
  'http://www.rikscha-mainz.de',
  'Page',
  'http://www.rikscha-mainz.de/Angebote'
], [
  'State',
  'state54'
])
# File lib/abrupt/transformation/base.rb, line 29
def initialize(parent_uri, uri, values = {})
  @parent_uri = parent_uri.to_a.map(&:remove_last_slashes)
  @uri = uri.to_a.map(&:remove_last_slashes)
  @values = values
  @result = []
end
set_value(key, schema, ref) click to toggle source
# File lib/abrupt/transformation/base.rb, line 50
def self.set_value(key, schema, ref)
  ref << ":#{key}"
  key_string = '[' + ref.join('][') + ']'
  value = eval "@values#{key_string}" rescue nil
  return unless value
  case schema[:type]
  when 'array'
    case schema[:items][:type]
    when 'object'
      # :name => { :type => :string }
      schema[:items][:properties].each do |arr_key, arr_val|
        eval "@values#{key_string} = [value].flatten.compact" unless value.is_a? Array
        value.each_with_index do |_obj, i|
          set_value arr_key, arr_val, ref.dup + [i]
        end
      end
    end
  when 'object'
    schema[:properties].each do |schema_key, schema_value|
      set_value(schema_key, schema_value, ref.dup)
    end
  else
    if value.is_a? Array
      value.each_with_index do |val, i|
        eval "@values#{key_string}[i] = val.send(*SCHEMA_MAPPING[schema[:type].to_sym])"
      end
    else
      eval "@values#{key_string} = value.send(*SCHEMA_MAPPING[schema[:type].to_sym])"
    end
  end
end

Public Instance Methods

add_data_property(type, value, name = @values[:name]) click to toggle source
# File lib/abrupt/transformation/base.rb, line 131
def add_data_property(type, value, name = @values[:name])
  type = type.to_s.camelize(:lower)
  @result << Statement.new(resolve_uri(name), VOC[type], value)
end
add_individual(name = @values[:name], klass = nil) click to toggle source
# File lib/abrupt/transformation/base.rb, line 124
def add_individual(name = @values[:name], klass = nil)
  klass ||= (@uri.empty? ? class_name : @uri.first).to_s.camelize
  uri = resolve_uri(name)
  @result << Statement.new(uri, RDF.type, VOC[klass])
  @result << Statement.new(resolve_parent_uri, VOC["has#{klass}"], uri)
end
add_individuals() click to toggle source

rubocop:enable all

# File lib/abrupt/transformation/base.rb, line 84
def add_individuals
  add_individual
  return @result unless @values[keyname]
  @values[keyname].each do |k, v|
    s = k.to_s.eql?('language') ? "#{keyname}Language" : k
    add_data_property s, v
  end
  @result
end
add_object_property(parent_uri, type, child_uri) click to toggle source
# File lib/abrupt/transformation/base.rb, line 136
def add_object_property(parent_uri, type, child_uri)
  parent_uri = RDF::URI(parent_uri) if parent_uri.is_a?(String)
  type = type.to_s.camelize
  @result << Statement.new(parent_uri, VOC["has#{type}"], child_uri)
end
class_name() click to toggle source

Returns the class name

# File lib/abrupt/transformation/base.rb, line 95
def class_name
  self.class.name.split('::').last
end
keyname() click to toggle source

Returns the keyname @example: Readability.new(parent_uri, uri).keyname

=> :readability
# File lib/abrupt/transformation/base.rb, line 103
def keyname
  class_name.downcase.to_sym
end
resolve_parent_uri() click to toggle source
# File lib/abrupt/transformation/base.rb, line 111
def resolve_parent_uri
  RDF::URI(resolve_parent_uri_part)
end
resolve_parent_uri_part() click to toggle source
# File lib/abrupt/transformation/base.rb, line 107
def resolve_parent_uri_part
  "#{VOC}#{@parent_uri.join('/')}"
end
resolve_uri(name = nil) click to toggle source
# File lib/abrupt/transformation/base.rb, line 119
def resolve_uri(name = nil)
  name ||= @uri.last
  RDF::URI(resolve_parent_uri_part + '/' + resolve_uri_part(name))
end
resolve_uri_part(name) click to toggle source
# File lib/abrupt/transformation/base.rb, line 115
def resolve_uri_part(name)
  @uri.empty? ? "#{class_name}/#{name}" : "#{@uri.join('/')}"
end