Class: ActiveRDF::PropertyLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/active_rdf/objectmanager/property_lookup.rb

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ PropertyLookup

not directly chainable like ResourceQuery



5
6
7
8
# File 'lib/active_rdf/objectmanager/property_lookup.rb', line 5

def initialize(subject)
  @ns = nil
  @subject = subject
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(ns_or_property, *args) ⇒ Object

Searches for property belonging to @subject. Returns RDF::Property. Replaces any existing values if method is an assignment: resource.prop=(new_value)

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_rdf/objectmanager/property_lookup.rb', line 12

def method_missing(ns_or_property, *args)
  # check registered namespace
  # foaf::name, where foaf is a registered abbreviation
  # evidence: foaf in Namespace
  # action: return self with @ns set
  if !@ns and Namespace.include?(ns_or_property)
    @ns = ns_or_property
    return self
  end

  property_name = ns_or_property.to_s
  # are we doing an update or not?
  # checking if method ends with '='
  update = property_name[-1..-1] == '='
  property_name = update ? property_name[0..-2] : property_name

  property =
    if @ns
      # seen registered namespace, lookup property
      RDF::Property.new(Namespace.lookup(@ns, property_name), @subject)
    elsif (registered_predicates = @subject.instance_eval{@predicates}) and registered_predicates.include?(property_name)
      # check for registered abbreviation
      # eyal.age is registered abbreviation
      # evidence: age in @predicates
      # action: return RDF::Property(age,self) and store value if assignment
      warn "Registered predicates is deprecated. Please use registered namespaces instead."
      RDF::Property.new(@predicates[property_name],@subject)
    else
      # search for known property
      @subject.properties.find{|prop| Namespace.localname(prop) == property_name}
    end

  if property
    property.replace(*args) if update
    return property
  end

  raise ActiveRdfError, "could not set #{methodname} to #{args}: no suitable predicate found. Maybe you are missing some schema information?" if update

  # If property can't be found, return nil instead of throwing NoMethodError.
  # In RDFS, we can't be sure that eyal cannot have an age, we just dont know the
  # age right now)
  nil
end