Class: Wikidata::Item

Inherits:
Entity show all
Defined in:
lib/wikidata/item.rb

Instance Attribute Summary

Attributes inherited from HashedObject

#data_hash

Instance Method Summary collapse

Methods inherited from Entity

#delocalize, #description, find_all, find_all_by_id, find_all_by_title, find_by_id, find_by_title, #inspect, #label, query_and_build_objects

Methods inherited from HashedObject

#initialize, #method_missing

Constructor Details

This class inherits a constructor from Wikidata::HashedObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wikidata::HashedObject

Instance Method Details

#childrenObject



59
60
61
# File 'lib/wikidata/item.rb', line 59

def children
  entities_for_property_id :children
end

#claimsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/wikidata/item.rb', line 4

def claims
  @claims ||= begin
    if self.data_hash.claims
      self.data_hash.claims.map do |statement_type, statement_array|
        statement_array.map do |statement_hash|
          Wikidata::Statement.new(statement_hash)
        end
      end.flatten
    else
      []
    end
  end
end

#claims_for_property_id(property_id) ⇒ Object



27
28
29
# File 'lib/wikidata/item.rb', line 27

def claims_for_property_id(property_id)
  claims.select{|c| c.mainsnak.property_id == property_id }
end

#doctoral_advisorsObject



63
64
65
# File 'lib/wikidata/item.rb', line 63

def doctoral_advisors
  entities_for_property_id :doctoral_advisor
end

#entities_for_property_id(property_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/wikidata/item.rb', line 31

def entities_for_property_id(property_id)
  presets = {
    mother:   "P25",
    father:   "P22",
    children: "P40",
    doctoral_advisor: "P184"
  }
  property_id = presets[property_id.to_sym] if presets.include?(property_id.to_sym)
  claims_for_property_id(property_id).map{|c| c.mainsnak.value.entity }
rescue
  []
end

#fathersObject



55
56
57
# File 'lib/wikidata/item.rb', line 55

def fathers
  entities_for_property_id :father
end

#imageObject

Convenience methods



46
47
48
49
# File 'lib/wikidata/item.rb', line 46

def image
  image_claim = claims_for_property_id("P18").first
  image_claim.mainsnak.value if image_claim
end

#mothersObject



51
52
53
# File 'lib/wikidata/item.rb', line 51

def mothers
  entities_for_property_id :mother
end

#resolve_claims!Object



18
19
20
21
22
23
24
25
# File 'lib/wikidata/item.rb', line 18

def resolve_claims!
  ids = []
  claims.each do |claim|
    ids << claim.mainsnak.property_id
    ids << claim.mainsnak.value.item_id if claim.mainsnak.value.class == Wikidata::DataValues::Entity
  end
  self.class.find_all_by_id ids
end