Class: Abrupt::Transformation::Base

Inherits:
Object
  • Object
show all
Includes:
RDF
Defined in:
lib/abrupt/transformation/base.rb

Overview

base class

Constant Summary collapse

SCHEMA_MAPPING =
{
    integer: :to_i,
    number: :to_f,
    string: :to_s,
    boolean: [:kind_of?, Object]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_uri, uri, values = {}) ⇒ Base

Initializes Transformer for Individual Statement for parent_uri & uri. Readability.new([

  'Website',
  'http://www.rikscha-mainz.de',
  'Page',
  'http://www.rikscha-mainz.de/Angebote'
], [
  'State',
  'state54'
])

Parameters:

  • parent_uri (Array)

    the parent uri in array structure of paths

  • uri (Array)

    the uri as array structure of path and id



29
30
31
32
33
34
# 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

Instance Attribute Details

#md5Object

Returns the value of attribute md5.



7
8
9
# File 'lib/abrupt/transformation/base.rb', line 7

def md5
  @md5
end

#parent_uriObject

Returns the value of attribute parent_uri.



7
8
9
# File 'lib/abrupt/transformation/base.rb', line 7

def parent_uri
  @parent_uri
end

#resultObject

Returns the value of attribute result.



7
8
9
# File 'lib/abrupt/transformation/base.rb', line 7

def result
  @result
end

#uriObject

Returns the value of attribute uri.



7
8
9
# File 'lib/abrupt/transformation/base.rb', line 7

def uri
  @uri
end

#valuesObject

Returns the value of attribute values.



7
8
9
# File 'lib/abrupt/transformation/base.rb', line 7

def values
  @values
end

Class Method Details

.customize_to_schema(values) ⇒ Object

rubocop:disable all



37
38
39
40
41
42
43
44
45
46
47
48
# 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

.set_value(key, schema, ref) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

Instance Method Details

#add_data_property(type, value, name = ) ⇒ Object



135
136
137
# File 'lib/abrupt/transformation/base.rb', line 135

def add_data_property(type, value, name = @values[:name])
  @result << Statement.new(resolve_uri(name), VOC[type], value)
end

#add_individual(name = , klass = nil) ⇒ Object



128
129
130
131
132
133
# File 'lib/abrupt/transformation/base.rb', line 128

def add_individual(name = @values[:name], klass = nil)
  klass ||= @uri.empty? ? class_name : @uri.first
  uri = resolve_uri(name)
  @result << Statement.new(uri, RDF.type, VOC[klass])
  @result << Statement.new(resolve_parent_uri, VOC["has#{klass}"], uri)
end

#add_individualsObject

rubocop:enable all



84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



139
140
141
142
# File 'lib/abrupt/transformation/base.rb', line 139

def add_object_property(parent_uri, type, child_uri)
  parent_uri = RDF::URI(parent_uri) if parent_uri.is_a?(String)
  @result << Statement.new(parent_uri, VOC["has#{type}"], child_uri)
end

#class_nameObject

Returns the class name



95
96
97
# File 'lib/abrupt/transformation/base.rb', line 95

def class_name
  self.class.name.split('::').last
end

#keynameObject

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

=> :readability


103
104
105
# File 'lib/abrupt/transformation/base.rb', line 103

def keyname
  class_name.downcase.to_sym
end

#resolve_parent_uriObject



111
112
113
# File 'lib/abrupt/transformation/base.rb', line 111

def resolve_parent_uri
  RDF::URI(resolve_parent_uri_part)
end

#resolve_parent_uri_partObject



107
108
109
# File 'lib/abrupt/transformation/base.rb', line 107

def resolve_parent_uri_part
  "#{VOC}#{@parent_uri.join('/')}"
end

#resolve_uri(name = nil) ⇒ Object



123
124
125
126
# File 'lib/abrupt/transformation/base.rb', line 123

def resolve_uri(name = nil)
  name ||= @uri.last
  RDF::URI(resolve_parent_uri_part + '/' + resolve_uri_part(name))
end

#resolve_uri_part(name) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/abrupt/transformation/base.rb', line 115

def resolve_uri_part(name)
  if @uri.empty?
    "#{class_name}/#{name}"
  else
    "#{@uri.join('/')}"
  end
end