Class: Factoid::Entitoid

Inherits:
Object
  • Object
show all
Defined in:
lib/factoid/entitoid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid) ⇒ Entitoid

Returns a new instance of Entitoid


6
7
8
9
# File 'lib/factoid/entitoid.rb', line 6

def initialize(uuid)
	@uuid = uuid
	@factoids = []
end

Instance Attribute Details

#uuidObject (readonly)

Returns the value of attribute uuid


11
12
13
# File 'lib/factoid/entitoid.rb', line 11

def uuid
  @uuid
end

Class Method Details

.from_xml(filename) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/factoid/xml.rb', line 21

def Entitoid.from_xml(filename)
	x = Nokogiri::XML(open(filename))

	uuid = x.root.attr('xml:id')
	e = self.new(uuid)

	context_sources = []
	x.xpath('/*/f:sources/f:source', NS).each do |s|
		context_sources << Source.from_xml(s)
	end

	x.xpath('/*/f:*', NS).each do |f|
		if f.name == 'sources'
			next
		end

		e.add_factoid(Factoid.from_xml(f, context_sources))
	end

	return e
end

Instance Method Details

#add_factoid(factoid) ⇒ Object


13
14
15
# File 'lib/factoid/entitoid.rb', line 13

def add_factoid(factoid)
	@factoids << factoid
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/factoid/entitoid.rb', line 40

def eql?(other)
	if other.equal?(self)
		return true
	end

	if other.class != self.class
		return false
	end

	f = factoids(:all)
	other_f = other.factoids(:all)

	return f.eql?(other_f)
end

#factoids(name, context = {}) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/factoid/entitoid.rb', line 17

def factoids(name, context = {})
	if name == :all
		return @factoids
	end

	matching = @factoids.select { |f| f.match?(name, context) }
	return matching
end

#value(name, context = {}) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/factoid/entitoid.rb', line 26

def value(name, context = {})
	f = factoids(name, context)

	if f.empty?
		raise "Cannot return value, no factoid matches name: '#{name}', context: #{context}"
	end

	if f.count > 1
		raise "Cannot return value, too many factoids match name: '#{name}', context: #{context}"
	end

	return f.first.value
end