spread2rdf 0.0.1pre.1 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.ocra +6 -0
- data/VERSION +1 -1
- data/bin/spread2rdf +1 -1
- data/lib/spread2rdf/attributes.rb +1 -0
- data/lib/spread2rdf/cli.rb +128 -34
- data/lib/spread2rdf/coord.rb +49 -0
- data/lib/spread2rdf/mapping/cell.rb +105 -0
- data/lib/spread2rdf/mapping/column.rb +63 -0
- data/lib/spread2rdf/mapping/column_block.rb +23 -0
- data/lib/spread2rdf/mapping/default_cell_mappings.rb +26 -0
- data/lib/spread2rdf/mapping/element.rb +64 -0
- data/lib/spread2rdf/mapping/resource.rb +95 -0
- data/lib/spread2rdf/mapping/sheet.rb +80 -0
- data/lib/spread2rdf/mapping/spreadsheet.rb +56 -0
- data/lib/spread2rdf/mapping/statement.rb +22 -0
- data/lib/spread2rdf/mapping/worksheet.rb +12 -0
- data/lib/spread2rdf/namespace.rb +6 -2
- data/lib/spread2rdf/roo_helper.rb +45 -0
- data/lib/spread2rdf/schema/column.rb +37 -0
- data/lib/spread2rdf/{spreadsheet/sub_sheet.rb → schema/column_block.rb} +3 -2
- data/lib/spread2rdf/{spreadsheet → schema}/element.rb +12 -16
- data/lib/spread2rdf/schema/schema.rb +19 -0
- data/lib/spread2rdf/schema/sheet.rb +87 -0
- data/lib/spread2rdf/schema/sheet_dsl.rb +54 -0
- data/lib/spread2rdf/schema/spreadsheet.rb +49 -0
- data/lib/spread2rdf/schema/spreadsheet_dsl.rb +42 -0
- data/lib/spread2rdf/schema/statement_mapping_schema.rb +26 -0
- data/lib/spread2rdf/schema/worksheet.rb +47 -0
- data/lib/spread2rdf.rb +34 -2
- data/ontologies/unit-v1.1.ttl +8330 -0
- data/spread2rdf.gemspec +4 -2
- metadata +50 -26
- data/lib/spread2rdf/helper.rb +0 -14
- data/lib/spread2rdf/spreadsheet/column.rb +0 -48
- data/lib/spread2rdf/spreadsheet/column_mapping_context.rb +0 -156
- data/lib/spread2rdf/spreadsheet/coord.rb +0 -51
- data/lib/spread2rdf/spreadsheet/mapping_context.rb +0 -67
- data/lib/spread2rdf/spreadsheet/mapping_dsl.rb +0 -23
- data/lib/spread2rdf/spreadsheet/sheet.rb +0 -128
- data/lib/spread2rdf/spreadsheet/sheet_dsl.rb +0 -34
- data/lib/spread2rdf/spreadsheet/sheet_mapping_context.rb +0 -90
- data/lib/spread2rdf/spreadsheet/sub_sheet_mapping_context.rb +0 -55
- data/lib/spread2rdf/spreadsheet/worksheet.rb +0 -49
- data/lib/spread2rdf/spreadsheet.rb +0 -92
data/spread2rdf.gemspec
CHANGED
@@ -21,12 +21,14 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.required_ruby_version = '>= 1.9.3'
|
22
22
|
|
23
23
|
gem.add_dependency('activesupport', '~> 3.2.3')
|
24
|
-
gem.add_dependency('awesome_print')
|
24
|
+
gem.add_dependency('awesome_print', '~> 1.1.0')
|
25
25
|
|
26
26
|
gem.add_dependency('roo', '~> 1.12.2')
|
27
27
|
gem.add_dependency('rubyzip', '~> 1.0.0') # for the roo-xlsm-fix
|
28
28
|
|
29
|
-
gem.add_dependency('linkeddata')
|
29
|
+
gem.add_dependency('linkeddata', '~> 1.0.0')
|
30
|
+
|
31
|
+
gem.add_dependency('ocra', '~> 1.3.1')
|
30
32
|
|
31
33
|
gem.add_development_dependency('rake')
|
32
34
|
gem.add_development_dependency('pry', '~> 0.9.12.2')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spread2rdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcel Otto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: awesome_print
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.1.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.1.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: roo
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,16 +70,30 @@ dependencies:
|
|
70
70
|
name: linkeddata
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 1.0.0
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 1.0.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: ocra
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.1
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.1
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: rake
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -133,6 +147,7 @@ extra_rdoc_files: []
|
|
133
147
|
files:
|
134
148
|
- .gitignore
|
135
149
|
- Gemfile
|
150
|
+
- Gemfile.ocra
|
136
151
|
- LICENSE.txt
|
137
152
|
- README.md
|
138
153
|
- Rakefile
|
@@ -141,23 +156,32 @@ files:
|
|
141
156
|
- lib/spread2rdf.rb
|
142
157
|
- lib/spread2rdf/attributes.rb
|
143
158
|
- lib/spread2rdf/cli.rb
|
159
|
+
- lib/spread2rdf/coord.rb
|
144
160
|
- lib/spread2rdf/extensions/roo_xlsm_fix.rb
|
145
|
-
- lib/spread2rdf/
|
161
|
+
- lib/spread2rdf/mapping/cell.rb
|
162
|
+
- lib/spread2rdf/mapping/column.rb
|
163
|
+
- lib/spread2rdf/mapping/column_block.rb
|
164
|
+
- lib/spread2rdf/mapping/default_cell_mappings.rb
|
165
|
+
- lib/spread2rdf/mapping/element.rb
|
166
|
+
- lib/spread2rdf/mapping/resource.rb
|
167
|
+
- lib/spread2rdf/mapping/sheet.rb
|
168
|
+
- lib/spread2rdf/mapping/spreadsheet.rb
|
169
|
+
- lib/spread2rdf/mapping/statement.rb
|
170
|
+
- lib/spread2rdf/mapping/worksheet.rb
|
146
171
|
- lib/spread2rdf/namespace.rb
|
147
|
-
- lib/spread2rdf/
|
148
|
-
- lib/spread2rdf/
|
149
|
-
- lib/spread2rdf/
|
150
|
-
- lib/spread2rdf/
|
151
|
-
- lib/spread2rdf/
|
152
|
-
- lib/spread2rdf/
|
153
|
-
- lib/spread2rdf/
|
154
|
-
- lib/spread2rdf/spreadsheet
|
155
|
-
- lib/spread2rdf/
|
156
|
-
- lib/spread2rdf/
|
157
|
-
- lib/spread2rdf/
|
158
|
-
- lib/spread2rdf/spreadsheet/sub_sheet_mapping_context.rb
|
159
|
-
- lib/spread2rdf/spreadsheet/worksheet.rb
|
172
|
+
- lib/spread2rdf/roo_helper.rb
|
173
|
+
- lib/spread2rdf/schema/column.rb
|
174
|
+
- lib/spread2rdf/schema/column_block.rb
|
175
|
+
- lib/spread2rdf/schema/element.rb
|
176
|
+
- lib/spread2rdf/schema/schema.rb
|
177
|
+
- lib/spread2rdf/schema/sheet.rb
|
178
|
+
- lib/spread2rdf/schema/sheet_dsl.rb
|
179
|
+
- lib/spread2rdf/schema/spreadsheet.rb
|
180
|
+
- lib/spread2rdf/schema/spreadsheet_dsl.rb
|
181
|
+
- lib/spread2rdf/schema/statement_mapping_schema.rb
|
182
|
+
- lib/spread2rdf/schema/worksheet.rb
|
160
183
|
- lib/spread2rdf/version.rb
|
184
|
+
- ontologies/unit-v1.1.ttl
|
161
185
|
- spread2rdf.gemspec
|
162
186
|
homepage: http://github.com/marcelotto/spread2rdf
|
163
187
|
licenses:
|
@@ -174,9 +198,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
174
198
|
version: 1.9.3
|
175
199
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
200
|
requirements:
|
177
|
-
- - ! '
|
201
|
+
- - ! '>='
|
178
202
|
- !ruby/object:Gem::Version
|
179
|
-
version:
|
203
|
+
version: '0'
|
180
204
|
requirements: []
|
181
205
|
rubyforge_project:
|
182
206
|
rubygems_version: 2.1.9
|
data/lib/spread2rdf/helper.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
module Spread2RDF
|
2
|
-
class Spreadsheet
|
3
|
-
class Sheet
|
4
|
-
class Column < Element
|
5
|
-
|
6
|
-
attr_reader :coord # this is set by Worksheet#index_columns!
|
7
|
-
|
8
|
-
self.attributes = {
|
9
|
-
predicate: nil,
|
10
|
-
object: nil,
|
11
|
-
statement: nil
|
12
|
-
}
|
13
|
-
|
14
|
-
def initialize(sheet, options = {}, &block)
|
15
|
-
super
|
16
|
-
end
|
17
|
-
|
18
|
-
alias sheet parent
|
19
|
-
|
20
|
-
def worksheet
|
21
|
-
parent = self.parent
|
22
|
-
parent = parent.parent until parent.is_a? Worksheet or parent.nil?
|
23
|
-
parent
|
24
|
-
end
|
25
|
-
|
26
|
-
def map(range, context)
|
27
|
-
#puts "mapping #{self} in #{range} ..."
|
28
|
-
case range
|
29
|
-
when Integer
|
30
|
-
coord = Coord[row: range, column: self.coord]
|
31
|
-
worksheet.cell_mapping[coord.to_sym] ||= mapping =
|
32
|
-
create_context(context, row: range,
|
33
|
-
subject: context.subject, predicate: predicate)
|
34
|
-
mapping.object
|
35
|
-
when Range
|
36
|
-
range.map { |row| self.map(row, context) }
|
37
|
-
else raise ArgumentError
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def to_s
|
42
|
-
"#{super} of #{sheet}"
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,156 +0,0 @@
|
|
1
|
-
module Spread2RDF
|
2
|
-
class Spreadsheet
|
3
|
-
class Sheet
|
4
|
-
class Column
|
5
|
-
class MappingContext < Spreadsheet::MappingContext
|
6
|
-
|
7
|
-
self.attributes = {
|
8
|
-
subject: nil,
|
9
|
-
predicate: nil,
|
10
|
-
row: nil
|
11
|
-
}
|
12
|
-
|
13
|
-
alias column element
|
14
|
-
alias property predicate
|
15
|
-
|
16
|
-
attr_reader :value
|
17
|
-
|
18
|
-
def initialize(sheet, parent_context = nil, attr = {})
|
19
|
-
super
|
20
|
-
@value = cell_value(row: row, column: column.coord)
|
21
|
-
return if @value.blank?
|
22
|
-
statements_to_object
|
23
|
-
worksheet.graph << self.graph
|
24
|
-
end
|
25
|
-
|
26
|
-
def cell_coord
|
27
|
-
Coord[row: row, column: column.coord]
|
28
|
-
end
|
29
|
-
|
30
|
-
def subject
|
31
|
-
@subject or parent_context.try(:subject)
|
32
|
-
end
|
33
|
-
|
34
|
-
def object
|
35
|
-
@object ||= @value && map_to_object(value)
|
36
|
-
end
|
37
|
-
|
38
|
-
def value_of_column(name)
|
39
|
-
other_column = sheet.column[name]
|
40
|
-
raise "couldn't find column #{name} when mapping #{column}" if
|
41
|
-
other_column.nil?
|
42
|
-
cell_value(row: row, column: other_column.coord)
|
43
|
-
end
|
44
|
-
|
45
|
-
def object_of_column(name)
|
46
|
-
other_column = sheet.column[name]
|
47
|
-
raise "couldn't find column #{name} when mapping #{column}" if
|
48
|
-
other_column.nil?
|
49
|
-
cell(row: row, column: other_column.coord).object
|
50
|
-
end
|
51
|
-
|
52
|
-
######################################################################
|
53
|
-
# Value-to-object mapping
|
54
|
-
|
55
|
-
private
|
56
|
-
|
57
|
-
def map_to_object(value)
|
58
|
-
case object_mapping_mode
|
59
|
-
when :to_string
|
60
|
-
value
|
61
|
-
when :resource_ref
|
62
|
-
resolve_resource_ref
|
63
|
-
when :new_resource
|
64
|
-
create_resource_object
|
65
|
-
when :custom
|
66
|
-
# TODO execute a mapping block in the context of Column::MappingContext
|
67
|
-
else
|
68
|
-
raise 'internal error: unknown column mapping type'
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
|
73
|
-
def object_mapping_mode
|
74
|
-
case
|
75
|
-
when column.object.nil? then :to_string
|
76
|
-
when column.object.is_a?(Proc) then :custom
|
77
|
-
when !column.object[:uri].nil? then :new_resource
|
78
|
-
when !column.object[:from].nil? then :resource_ref
|
79
|
-
else
|
80
|
-
raise "mapping specification error: don't know how to map #{column}"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def resolve_resource_ref
|
85
|
-
source = column.object[:from]
|
86
|
-
source = { worksheet: source } if source.is_a? Symbol
|
87
|
-
raise ArgumentError, "expecting a Hash as source, but got #{source}" unless source.is_a? Hash
|
88
|
-
source_worksheet = source[:worksheet]
|
89
|
-
source_worksheet = spreadsheet.worksheet[source_worksheet]
|
90
|
-
raise "#{column}: couldn't find source worksheet #{source[:worksheet]}" if source_worksheet.nil?
|
91
|
-
source_predicate = source[:predicate] || RDF::RDFS.label
|
92
|
-
result = source_worksheet.graph.query([nil, source_predicate, value])
|
93
|
-
raise "#{column}: couldn't find a resource for #{value} in #{source_worksheet}" if result.empty?
|
94
|
-
raise "#{column}: found multiple resources for #{value} in #{source_worksheet}: #{result.map(&:subject)}" if result.count > 1
|
95
|
-
result.first.subject
|
96
|
-
end
|
97
|
-
|
98
|
-
# TODO: Should we reuse/share mapping logic with Sheet::MappingContext (#subject etc.)?
|
99
|
-
def create_resource_object
|
100
|
-
case
|
101
|
-
when (column.object.try(:fetch, :uri, nil) || object) == :bnode
|
102
|
-
RDF::Node.new
|
103
|
-
else
|
104
|
-
raise NotImplementedError
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
|
109
|
-
######################################################################
|
110
|
-
# Statement mapping
|
111
|
-
|
112
|
-
def statement_mapping_mode
|
113
|
-
case
|
114
|
-
when column.statement == :none then :ignore
|
115
|
-
when column.statement == :none then :ignore
|
116
|
-
when column.predicate.nil? then :ignore
|
117
|
-
when restriction_mode then :restriction
|
118
|
-
else :default
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def restriction_mode
|
123
|
-
restriction_mode = column.statement
|
124
|
-
case restriction_mode
|
125
|
-
when :restriction then RDF::OWL.hasValue
|
126
|
-
when Hash then restriction_mode[:restriction]
|
127
|
-
else nil
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def statements_to_object
|
132
|
-
case statement_mapping_mode
|
133
|
-
when :default
|
134
|
-
statement(subject, predicate, object)
|
135
|
-
when :restriction
|
136
|
-
restriction_class = RDF::Node.new
|
137
|
-
statements(
|
138
|
-
[ subject, RDF::RDFS.subClassOf, restriction_class ],
|
139
|
-
[ restriction_class, RDF.type, RDF::OWL.Restriction ],
|
140
|
-
[ restriction_class, RDF::OWL.onProperty, predicate ],
|
141
|
-
[ restriction_class, restriction_mode, object ]
|
142
|
-
)
|
143
|
-
end
|
144
|
-
exec(value, &column.block) if column.block
|
145
|
-
end
|
146
|
-
|
147
|
-
def exec(value, &block)
|
148
|
-
#puts "executing block of #{@___column___} in row #{row}"
|
149
|
-
self.instance_exec(value, &block)
|
150
|
-
end
|
151
|
-
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
module Spread2RDF
|
2
|
-
class Spreadsheet
|
3
|
-
class Coord < Struct.new(:column, :row)
|
4
|
-
def initialize(*args)
|
5
|
-
case args.length
|
6
|
-
when 2 then super
|
7
|
-
when 1
|
8
|
-
case args = args.first
|
9
|
-
when Hash
|
10
|
-
super(args[:column], args[:row])
|
11
|
-
when Symbol, String
|
12
|
-
coord = args.to_s
|
13
|
-
raise "Invalid cell coordinates #{coord}" unless coord =~ /(\w+)(\d+)/
|
14
|
-
super(Regexp.last_match[1], Regexp.last_match[2].to_i)
|
15
|
-
else raise ArgumentError, "can't handle argument #{args}"
|
16
|
-
end
|
17
|
-
else raise ArgumentError, "too many arguments: #{args}"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def column_as_number
|
22
|
-
Roo::Base.letter_to_number(column)
|
23
|
-
end
|
24
|
-
|
25
|
-
def column_as_index
|
26
|
-
column_as_number - 1
|
27
|
-
end
|
28
|
-
|
29
|
-
def increment_column(count = 1)
|
30
|
-
self.class.increment_column(self.column, count)
|
31
|
-
end
|
32
|
-
|
33
|
-
def to_s
|
34
|
-
"#{column}#{row}"
|
35
|
-
end
|
36
|
-
|
37
|
-
def to_sym
|
38
|
-
to_s.to_sym
|
39
|
-
end
|
40
|
-
|
41
|
-
class << self
|
42
|
-
alias [] new
|
43
|
-
|
44
|
-
def increment_column(column, count=1)
|
45
|
-
Roo::Base.number_to_letter(Roo::Base.letter_to_number(column) + count)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
module Spread2RDF
|
2
|
-
class Spreadsheet
|
3
|
-
class MappingContext
|
4
|
-
include Attributes
|
5
|
-
|
6
|
-
self.attributes = {
|
7
|
-
}
|
8
|
-
|
9
|
-
attr_reader :element
|
10
|
-
attr_reader :parent_context
|
11
|
-
|
12
|
-
attr_reader :graph
|
13
|
-
|
14
|
-
def initialize(element, parent_context, attr = {})
|
15
|
-
@element = element
|
16
|
-
@parent_context = parent_context
|
17
|
-
@graph = RDF::Repository.new
|
18
|
-
init_attributes(attr)
|
19
|
-
end
|
20
|
-
|
21
|
-
def sheet
|
22
|
-
@element.sheet
|
23
|
-
end
|
24
|
-
|
25
|
-
def worksheet
|
26
|
-
@element.worksheet
|
27
|
-
end
|
28
|
-
|
29
|
-
def spreadsheet
|
30
|
-
@element.spreadsheet
|
31
|
-
end
|
32
|
-
|
33
|
-
def cell_value(coord)
|
34
|
-
worksheet.cell(coord)
|
35
|
-
end
|
36
|
-
|
37
|
-
def cell(coord)
|
38
|
-
coord = Coord[coord] unless coord.is_a? Coord
|
39
|
-
worksheet.cell_mapping[coord.to_sym]
|
40
|
-
end
|
41
|
-
|
42
|
-
def to_s
|
43
|
-
"#{self.class.name.split('::')[-2..-1].join('::')} of #{element}"
|
44
|
-
end
|
45
|
-
|
46
|
-
##########################################################################
|
47
|
-
# statement generators
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def add_statement(*args)
|
52
|
-
args = args.first if args.count == 1 and args.first.is_a? Array
|
53
|
-
#puts "adding statement: #{args.inspect}"
|
54
|
-
raise "internal error: trying to add a bad triple with nil value: #{args}" if args.count != 3 or args.one? { |arg| arg.nil? }
|
55
|
-
@graph << RDF::Statement.new(*args)
|
56
|
-
end
|
57
|
-
alias statement add_statement
|
58
|
-
|
59
|
-
def add_statements(*args)
|
60
|
-
args = args.first if args.count == 1 and args.first.is_a? Array
|
61
|
-
args.each { |arg| statement(arg) }
|
62
|
-
end
|
63
|
-
alias statements add_statements
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Spread2RDF
|
2
|
-
class Spreadsheet
|
3
|
-
class MappingDSL
|
4
|
-
|
5
|
-
def initialize(schema)
|
6
|
-
@schema = schema
|
7
|
-
end
|
8
|
-
|
9
|
-
def namespaces(namespaces)
|
10
|
-
namespaces.each { |name, namespace| Namespace[name] = namespace }
|
11
|
-
end
|
12
|
-
|
13
|
-
def worksheet(name, options={}, &block)
|
14
|
-
source_name = options[:source_name] = name
|
15
|
-
name = ( options.delete(:name) || source_name ).to_sym
|
16
|
-
worksheet = @schema.worksheet[name] ||= Worksheet.new(@schema)
|
17
|
-
worksheet.update_attributes options.merge(name: name, source_name: source_name)
|
18
|
-
Sheet::DSL.new(worksheet, &block)
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,128 +0,0 @@
|
|
1
|
-
module Spread2RDF
|
2
|
-
class Spreadsheet
|
3
|
-
class Sheet < Element
|
4
|
-
|
5
|
-
self.attributes = {
|
6
|
-
subject: nil,
|
7
|
-
start: :A2,
|
8
|
-
row_count_per_resource: nil
|
9
|
-
}
|
10
|
-
|
11
|
-
def initialize(parent, options={}, &block)
|
12
|
-
super(parent, options, &block)
|
13
|
-
@column = {}
|
14
|
-
end
|
15
|
-
|
16
|
-
def worksheet
|
17
|
-
return self if self.is_a? Worksheet
|
18
|
-
parent = self.parent
|
19
|
-
parent = parent.parent until parent.is_a? Worksheet or parent.nil?
|
20
|
-
parent
|
21
|
-
end
|
22
|
-
|
23
|
-
def column(name = nil)
|
24
|
-
return @column if name.nil?
|
25
|
-
name = name.to_sym
|
26
|
-
@column[name] or ( parent.is_a?(Sheet) and parent.column(name) ) or nil
|
27
|
-
end
|
28
|
-
|
29
|
-
def columns
|
30
|
-
@column.values
|
31
|
-
end
|
32
|
-
|
33
|
-
def cell(coord)
|
34
|
-
coord = Coord[coord]
|
35
|
-
spreadsheet.roo.cell(coord.column, coord.row, worksheet.source_name)
|
36
|
-
end
|
37
|
-
|
38
|
-
def start_coord
|
39
|
-
Coord[start]
|
40
|
-
end
|
41
|
-
|
42
|
-
def row_range
|
43
|
-
raise NotImplementedError, 'subclasses of Sheet must implement this method'
|
44
|
-
end
|
45
|
-
|
46
|
-
def column_range
|
47
|
-
first = columns.first.coord
|
48
|
-
first = first.begin if first.is_a? Range
|
49
|
-
last = columns.last.coord
|
50
|
-
last = last.end if last.is_a? Range
|
51
|
-
first .. last
|
52
|
-
end
|
53
|
-
|
54
|
-
def each_column(&block)
|
55
|
-
columns.each do |column|
|
56
|
-
if column.is_a? SubSheet
|
57
|
-
column.each_column(&block)
|
58
|
-
else
|
59
|
-
yield column
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def subject_column
|
65
|
-
#return nil unless subject_mapping_type == :from_column
|
66
|
-
column_name = self.subject.try(:fetch, :column, nil) || :uri
|
67
|
-
@column[column_name]
|
68
|
-
end
|
69
|
-
|
70
|
-
def fix_row_count_per_resource
|
71
|
-
row_count_per_resource or ( !subject_column && 1 ) or nil
|
72
|
-
end
|
73
|
-
|
74
|
-
def add_triple(*args)
|
75
|
-
raise "internal error: trying to add a bad triple with nil value in #{self}: #{args}" if args.count != 3 or args.one? { |arg| arg.nil? }
|
76
|
-
worksheet.graph << RDF::Statement.new(*args)
|
77
|
-
end
|
78
|
-
|
79
|
-
def map(row_range = self.row_range, context = nil)
|
80
|
-
#puts "processing #{self} ..."
|
81
|
-
return [] if row_range.nil?
|
82
|
-
subjects = rows_per_resource(row_range).map do |resource_range|
|
83
|
-
mapping = create_context(context, row_range: resource_range)
|
84
|
-
spreadsheet.worksheet_mapping[worksheet.name] = mapping unless self.is_a? SubSheet
|
85
|
-
mapping.subject
|
86
|
-
end
|
87
|
-
subjects
|
88
|
-
end
|
89
|
-
|
90
|
-
private
|
91
|
-
|
92
|
-
def rows_per_resource(row_range)
|
93
|
-
return [] if row_range.nil?
|
94
|
-
rows = if fix_row_count = fix_row_count_per_resource
|
95
|
-
row_range.find_all do |row|
|
96
|
-
(row - row_range.begin) % fix_row_count == 0
|
97
|
-
end
|
98
|
-
else
|
99
|
-
subject_column_coord = self.subject_column.try(:coord)
|
100
|
-
raise "no subject column for #{self}" if subject_column_coord.blank?
|
101
|
-
row_range.find_all do |row|
|
102
|
-
not cell(row: row, column: subject_column_coord).blank?
|
103
|
-
end
|
104
|
-
end
|
105
|
-
rows_per_resource = []
|
106
|
-
rows.each_with_index do |first_row, i|
|
107
|
-
last_row = (i+1 == rows.count ? row_range.end : rows[i+1]-1)
|
108
|
-
rows_per_resource << Range.new(first_row, last_row)
|
109
|
-
end
|
110
|
-
rows_per_resource
|
111
|
-
end
|
112
|
-
|
113
|
-
def roo_select
|
114
|
-
spreadsheet.roo.default_sheet = worksheet.source_name
|
115
|
-
end
|
116
|
-
|
117
|
-
def roo
|
118
|
-
last_default_sheet = spreadsheet.roo.default_sheet
|
119
|
-
roo_select
|
120
|
-
return nil unless block_given?
|
121
|
-
result = yield
|
122
|
-
spreadsheet.roo.default_sheet = last_default_sheet
|
123
|
-
result
|
124
|
-
end
|
125
|
-
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Spread2RDF
|
2
|
-
class Spreadsheet
|
3
|
-
class Sheet
|
4
|
-
class DSL
|
5
|
-
def initialize(sheet, &block)
|
6
|
-
@sheet = sheet
|
7
|
-
instance_exec(&block) if block_given?
|
8
|
-
end
|
9
|
-
|
10
|
-
def column(name, options={}, &block)
|
11
|
-
name = name.to_sym
|
12
|
-
column = @sheet.column[name] ||= Column.new(@sheet, &block)
|
13
|
-
column.update_attributes options.merge(name: name)
|
14
|
-
column # TODO: chaining logic ...?
|
15
|
-
end
|
16
|
-
|
17
|
-
def sub_sheet(name, options={}, &block)
|
18
|
-
name = name.to_sym
|
19
|
-
sub_sheet = @sheet.column[name] ||= SubSheet.new(@sheet)
|
20
|
-
sub_sheet.update_attributes options.merge(name: name)
|
21
|
-
Sheet::DSL.new(sub_sheet, &block)
|
22
|
-
end
|
23
|
-
alias column_block sub_sheet
|
24
|
-
|
25
|
-
def cell(coord, options = {}, &block)
|
26
|
-
content = @sheet.cell(coord)
|
27
|
-
content = block.call(content) if block_given?
|
28
|
-
content
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|