Class: Wongi::Engine::Template

Inherits:
Struct
  • Object
show all
Defined in:
lib/wongi-engine/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject

Returns the value of attribute object

Returns:

  • (Object)

    the current value of object



3
4
5
# File 'lib/wongi-engine/template.rb', line 3

def object
  @object
end

#predicateObject

Returns the value of attribute predicate

Returns:

  • (Object)

    the current value of predicate



3
4
5
# File 'lib/wongi-engine/template.rb', line 3

def predicate
  @predicate
end

#subjectObject

Returns the value of attribute subject

Returns:

  • (Object)

    the current value of subject



3
4
5
# File 'lib/wongi-engine/template.rb', line 3

def subject
  @subject
end

Class Method Details

.hash_for(*args) ⇒ Object



28
29
30
# File 'lib/wongi-engine/template.rb', line 28

def self.hash_for *args
  args.map( &:hash ).hash
end

.variable?(thing) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
# File 'lib/wongi-engine/template.rb', line 5

def self.variable? thing
  return false unless thing.is_a?(Symbol)
  thing[0] >= 'A' && thing[0] <= 'Z'
end

Instance Method Details

#==(other) ⇒ Object



32
33
34
# File 'lib/wongi-engine/template.rb', line 32

def == other
  other.is_a?(Template) && subject == other.subject && predicate == other.predicate && object == other.object
end

#=~(template) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/wongi-engine/template.rb', line 36

def =~ template
  case template
  when Template
    ( template.subject == :_ || template.subject == subject ) &&
    ( template.predicate == :_ || template.predicate == predicate ) &&
    ( template.object == :_ || template.object == object )
  else
    raise Error, "templates can only match other templates"
  end
end

#hashObject



24
25
26
# File 'lib/wongi-engine/template.rb', line 24

def hash
  @hash ||= [subject.hash, predicate.hash, object.hash].hash
end

#inspectObject



47
48
49
# File 'lib/wongi-engine/template.rb', line 47

def inspect
  "<~#{subject.inspect} #{predicate.inspect} #{object.inspect}>"
end

#resolve!(token) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wongi-engine/template.rb', line 55

def resolve!(token)
  s = if Template.variable?(subject)
    token[subject].tap { |x| raise DefinitionError, "unbound variable #{subject} in token #{token}" unless x}
  else
    subject
  end
  p = if Template.variable?(predicate)
    token[predicate].tap { |x| raise DefinitionError, "unbound variable #{predicate} in token #{token}" unless x}
  else
    predicate
  end
  o = if Template.variable?(object)
    token[object].tap { |x| raise DefinitionError, "unbound variable #{object} in token #{token}" unless x}
  else
    object
  end
  [s, p, o]
end

#root?Boolean

TODO: reintroduce Network#import when bringing back RDF support

Returns:

  • (Boolean)


12
13
14
# File 'lib/wongi-engine/template.rb', line 12

def root?
  subject == :_ && predicate == :_ && object == :_
end

#to_sObject



51
52
53
# File 'lib/wongi-engine/template.rb', line 51

def to_s
  inspect
end

#variablesObject



16
17
18
19
20
21
22
# File 'lib/wongi-engine/template.rb', line 16

def variables
  [].tap do |a|
    a << subject if Template.variable?( subject )
    a << predicate if Template.variable?( predicate )
    a << object if Template.variable?( object )
  end
end