Class: When::Events::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/when_exe/events.rb

Overview

時間座標を持つイベント

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset, id, row) ⇒ Event

イベントの生成

Parameters:

  • dataset (When::Events::DataSet)

    所属する一言語対応データセット

  • id (Integer)

    通し番号

  • row (Array<String>)

    CSVの一行分

  • row (Hash<String=>Object>)

    RDFの1クエリ分



1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
# File 'lib/when_exe/events.rb', line 1783

def initialize(dataset, id, row)
  @dataset = dataset
  @id      = id
  @row     = row
  @role    = {}
  @rdf     = {}
  @csv     = {}
  @order   = {}

  case row
  when Hash
    @rdf = row
  when Array
    row.each_with_index do |item, index|
      @csv[(index+1).to_s] = item
    end

    dataset.rdf.each_pair do |item, definition|
      fields = []
      format = definition[:target].gsub(/\[(\d+)\]/) {
        field  =  row[$1.to_i-1]
        field  =  escape(field, definition[:type])
        fields << field
        '%s'
      }
      @rdf[item] = definition[:operation].call(self, format == '%s' ? fields.first : format % fields)
    end
  end

  @role[ID] = @rdf[ID] = id
  dataset.role.each_pair do |item, definition|
    fields = []
    format = definition[:target].gsub('%','%%').gsub(/\[(\d+)\]|<(.+?)>|\{(.+?)\}/) {|match|
      key  = $1 || $2 || $3
      field = case match
              when /\A\[/ ; row[key.to_i-1]
              when /\A</  ; @rdf[key]
              when /\A\{/ ; @role[key]
              end
      field  =  escape(field, definition[:type])
      fields << field
      '%s'
    }
    pre_operation = DataSet::Operations[item]
    @role[item] = 
      if pre_operation
        definition[:operation].call(self, pre_operation.call(self, format == '%s' ? fields.first : format % fields))
      else
        definition[:operation].call(self, format == '%s' ? fields.first : format % fields)
      end
  end
end

Instance Attribute Details

#csvHash<String(番号)=>Object>

CSV変数

Returns:

  • (Hash<String(番号)=>Object>)


1653
1654
1655
# File 'lib/when_exe/events.rb', line 1653

def csv
  @csv
end

#datasetWhen::Events::DataSet (readonly)

所属する一言語対応データセット



1616
1617
1618
# File 'lib/when_exe/events.rb', line 1616

def dataset
  @dataset
end

#idInteger (readonly)

通し番号

Returns:

  • (Integer)


1623
1624
1625
# File 'lib/when_exe/events.rb', line 1623

def id
  @id
end

#orderHash<String(prefix:name)=>Array(Integer(id))> (readonly)

当該順序キーで何番目のイベントか

Returns:



1661
1662
1663
# File 'lib/when_exe/events.rb', line 1661

def order
  @order
end

#rdfHash<String(prefix:name)=>Object>

RDF変数

Returns:

  • (Hash<String(prefix:name)=>Object>)


1645
1646
1647
# File 'lib/when_exe/events.rb', line 1645

def rdf
  @rdf
end

#roleHash<String(prefix:name)=>Object>

ロール変数

Returns:

  • (Hash<String(prefix:name)=>Object>)


1637
1638
1639
# File 'lib/when_exe/events.rb', line 1637

def role
  @role
end

#rowArray<String> (readonly)

CSVの一行分

Returns:



1630
1631
1632
# File 'lib/when_exe/events.rb', line 1630

def row
  @row
end

Instance Method Details

#abstract(item = ABSTRACT, method = :role) ⇒ String

Note:

ブロックを渡された場合、そのブロックに{}部分のマークアップを依頼する

指定の情報を{}部分のマークアップ処理など行って整形して返す

Parameters:

  • item (String) (defaults to: ABSTRACT)

    返す情報の名称

  • method (Symbol) (defaults to: :role)

    返す情報が属するグループ(:role, :rdf, :csv)

Returns:



1684
1685
1686
1687
1688
1689
# File 'lib/when_exe/events.rb', line 1684

def abstract(item=ABSTRACT, method=:role)
  send(method)[item].gsub(/(\{+)(.*?)(\}+)/) {
    bra, word, cket = $~[1..3]
    '{'*(bra.length/2) + (block_given? ? yield(word) : word) + '}'*(cket.length/2)
  }
end

#deep_copyWhen::Events::Event

イベントの複製

Returns:



1767
1768
1769
1770
1771
1772
1773
# File 'lib/when_exe/events.rb', line 1767

def deep_copy
  result = self.dup
  result.csv  = @csv.dup
  result.rdf  = @rdf.dup
  result.role = @role.dup
  result
end

#each_wordObject

HAS_PART対象の文字列中の{}で囲まれた語に対して yield で指定された処理を行う



1666
1667
1668
1669
1670
1671
1672
# File 'lib/when_exe/events.rb', line 1666

def each_word
  @role[HAS_PART].scan(/(\{+)(.*?)(\}+)/) do
    bra, word, cket = $~[1..3]
    next unless bra.length.odd? && cket.length.odd?
    yield(word)
  end
end

#groupString

Returns:



1714
1715
1716
# File 'lib/when_exe/events.rb', line 1714

def group
  @role[GROUP]
end

#source(item = SOURCE, method = :role) ⇒ Array<String(IRI), String(説明)>

指定の情報のIRIとその説明を返す

Parameters:

  • item (String) (defaults to: SOURCE)

    返す情報の名称

  • method (Symbol) (defaults to: :role)

    返す情報が属するグループ(:role, :rdf, :csv)

Returns:



1699
1700
1701
1702
1703
1704
1705
1706
1707
# File 'lib/when_exe/events.rb', line 1699

def source(item=SOURCE, method=:role)
  iri = send(method)[item]
  return [nil, iri] unless /:\/\// =~ iri
  @dataset.prefix_description.each do |description|
    index = iri.index(description[0])
    return [iri, description[1]] if index && index == 0
  end
  [iri]
end

#statementsArray<RDF::Statement>

自身を主語とする RDF::Statement の Array を返す

Returns:

  • (Array<RDF::Statement>)


1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
# File 'lib/when_exe/events.rb', line 1723

def statements
  unless @statements
    @statements = []
    raise ArgumentError, 'Role for rdf:subject not defined' unless @role.key?(SUBJECT)
    subject = @dataset.resource[@role[SUBJECT]]
    @role.each_pair do |predicate, object|
      case predicate
      when SUBJECT, ID, GRAPH, WHAT_DAY
        # Do nothing
      when HAS_PART
        if @role[HAS_PART].kind_of?(Array)
          words = @role[HAS_PART]
        else
          words = []
          each_word do |word|
            words << word
          end
          words.uniq!
        end
        words.each do |word|
          @statements << ::RDF::Statement(subject, @dataset.resource[predicate], word)
        end
    # when ABSTRACT
    #   @statements << ::RDF::Statement(subject, @dataset.resource[predicate], abstract)
      else
        if [URI, IRI].include?(@dataset.role[predicate][:type])
          object = @dataset.resource[object]
        elsif object.respond_to?(:to_uri)
          object = object.to_uri
        elsif @dataset.role[predicate][:lang] != '' && object.kind_of?(String)
          object = ::RDF::Literal.new(object, {:language=>@dataset.role[predicate][:lang]})
        end
        @statements << ::RDF::Statement(subject, @dataset.resource[predicate], object)
      end
    end
  end
  @statements
end