Class: Spread2RDF::Mapping::Sheet

Inherits:
Element
  • Object
show all
Defined in:
lib/spread2rdf/mapping/sheet.rb

Direct Known Subclasses

ColumnBlock, Worksheet

Instance Attribute Summary collapse

Attributes inherited from Element

#parent, #schema

Instance Method Summary collapse

Methods inherited from Element

#empty?, #graph, #to_s

Constructor Details

#initialize(sheet, parent) ⇒ Sheet

Returns a new instance of Sheet.



5
6
7
8
# File 'lib/spread2rdf/mapping/sheet.rb', line 5

def initialize(sheet, parent)
  super
  @resources = []
end

Instance Attribute Details

#resourcesObject (readonly) Also known as: _children_

Returns the value of attribute resources.



52
53
54
# File 'lib/spread2rdf/mapping/sheet.rb', line 52

def resources
  @resources
end

Instance Method Details

#cell_value(coord) ⇒ Object Also known as: cell

Roo helper



67
68
69
70
71
# File 'lib/spread2rdf/mapping/sheet.rb', line 67

def cell_value(coord)
  value = ROO.cell(coord, schema.worksheet.source_name)
  value = value.strip if value.is_a? String
  value
end

#mapObject



10
11
12
13
14
15
16
17
# File 'lib/spread2rdf/mapping/sheet.rb', line 10

def map
  #puts "processing #{self} in #{row_range}"
  return [] if row_range.nil? or schema.columns.empty?
  rows_per_resource.each do |resource_range|
    @resources << Mapping::Resource.new(schema, self, resource_range)
  end
  self
end

#resource_by_index(index) ⇒ Object



60
61
62
# File 'lib/spread2rdf/mapping/sheet.rb', line 60

def resource_by_index(index)
  @resources[index]
end

#resource_by_row(row) ⇒ Object



55
56
57
58
# File 'lib/spread2rdf/mapping/sheet.rb', line 55

def resource_by_row(row)
  index = rows_per_resource.find_index { |range| range.include? row }
  resource_by_index(index)
end

#roo(&block) ⇒ Object



74
75
76
# File 'lib/spread2rdf/mapping/sheet.rb', line 74

def roo(&block)
  ROO.roo(schema.worksheet.source_name, &block)
end

#rows_per_resourceObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/spread2rdf/mapping/sheet.rb', line 19

def rows_per_resource
  return [] if row_range.nil?
  @rows_per_resource ||= begin
    rows = if fix_row_count = schema.fix_row_count_per_resource
             row_range.find_all do |row|
               (row - row_range.begin) % fix_row_count == 0
             end
           else
             subject_column_coord = schema.subject_column.try(:coord)
             raise "no subject column for #{self}" if subject_column_coord.blank?
             row_range.find_all do |row|
               not cell(row: row, column: subject_column_coord).blank?
             end
           end
    rows_per_resource = []
    rows.each_with_index do |first_row, i|
      last_row = (i+1 == rows.count ? row_range.end : rows[i+1]-1)
      rows_per_resource << Range.new(first_row, last_row)
    end
    rows_per_resource
  end
end

#worksheetObject

Mapping::Element structure



45
46
47
48
49
50
# File 'lib/spread2rdf/mapping/sheet.rb', line 45

def worksheet
  return self if self.is_a? Worksheet
  parent = self.parent
  parent = parent.parent until parent.is_a? Worksheet or parent.nil?
  parent
end