Class: Everypolitician::Popolo::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/everypolitician/popolo/entity.rb

Direct Known Subclasses

Area, Event, Membership, Organization, Person, Post

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, popolo = nil) ⇒ Entity

Returns a new instance of Entity.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/everypolitician/popolo/entity.rb', line 22

def initialize(document, popolo = nil)
  @document = document
  @popolo = popolo

  document.each do |key, value|
    if respond_to?("#{key}=")
      __send__("#{key}=", value)
    else
      define_singleton_method(key) { value }
    end
  end
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



6
7
8
# File 'lib/everypolitician/popolo/entity.rb', line 6

def document
  @document
end

#popoloObject (readonly)

Returns the value of attribute popolo.



7
8
9
# File 'lib/everypolitician/popolo/entity.rb', line 7

def popolo
  @popolo
end

Class Method Details

.classification(classification = nil) ⇒ Object



9
10
11
# File 'lib/everypolitician/popolo/entity.rb', line 9

def self.classification(classification = nil)
  @classification ||= classification
end

.inherited(subclass) ⇒ Object



13
14
15
16
# File 'lib/everypolitician/popolo/entity.rb', line 13

def self.inherited(subclass)
  @subclasses ||= []
  @subclasses.push(subclass)
end

.subclassesObject



18
19
20
# File 'lib/everypolitician/popolo/entity.rb', line 18

def self.subclasses
  @subclasses || []
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



47
48
49
# File 'lib/everypolitician/popolo/entity.rb', line 47

def ==(other)
  self.class == other.class && id == other.id
end

#[](key) ⇒ Object



39
40
41
# File 'lib/everypolitician/popolo/entity.rb', line 39

def [](key)
  document[key]
end

#idObject



35
36
37
# File 'lib/everypolitician/popolo/entity.rb', line 35

def id
  document.fetch(:id, nil)
end

#identifier(scheme) ⇒ Object



56
57
58
# File 'lib/everypolitician/popolo/entity.rb', line 56

def identifier(scheme)
  identifiers.find(-> { {} }) { |i| i[:scheme] == scheme }[:identifier]
end

#identifiersObject



52
53
54
# File 'lib/everypolitician/popolo/entity.rb', line 52

def identifiers
  document.fetch(:identifiers, [])
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/everypolitician/popolo/entity.rb', line 43

def key?(key)
  document.key?(key)
end

#wikidataObject



60
61
62
# File 'lib/everypolitician/popolo/entity.rb', line 60

def wikidata
  identifier('wikidata')
end