Class: TaliaCore::SemanticCollectionItem

Inherits:
Object
  • Object
show all
Defined in:
lib/talia_core/semantic_collection_item.rb

Overview

This is a single item in a semantic collection wrapper. The contents are

* fat_relation - a SemanticRelation with all the columns needed to build the
  related objects
* plain_relation - a normal semantic relation object (either this or fat_relation)
  should be given

Only one of the above should be usually given

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, plain_or_fat) ⇒ SemanticCollectionItem

Returns a new instance of SemanticCollectionItem.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/talia_core/semantic_collection_item.rb', line 13

def initialize(relation, plain_or_fat)
  case plain_or_fat
  when :plain
    @plain_relation = relation
  when :fat
    @fat_relation = relation
  else
    raise(ArgumentError, "Unknown type")
  end
  @object_type = SemanticCollectionWrapper.special_types[relation.predicate_uri.to_s]
end

Instance Attribute Details

#fat_relationObject (readonly)

Returns the value of attribute fat_relation.



11
12
13
# File 'lib/talia_core/semantic_collection_item.rb', line 11

def fat_relation
  @fat_relation
end

#plain_relationObject (readonly)

Returns the value of attribute plain_relation.



11
12
13
# File 'lib/talia_core/semantic_collection_item.rb', line 11

def plain_relation
  @plain_relation
end

Instance Method Details

#==(compare) ⇒ Object



57
58
59
# File 'lib/talia_core/semantic_collection_item.rb', line 57

def ==(compare)
  self.value == compare
end

#create_object_from(fat_relation) ⇒ Object

Creates an object frm the given “fat” relation. This retrieves the data from the relation object and instantiates it just like it would be after a find operation.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/talia_core/semantic_collection_item.rb', line 66

def create_object_from(fat_relation)
  # First we find out which class (table our target object is in)
  klass = fat_relation.object_type.constantize
  record = nil
  if(klass == TaliaCore::ActiveSource)
    # We prepare a hash of properties for the ActiveSource
    record = {
      'uri' => fat_relation.object_uri,
      'created_at' => fat_relation.object_created_at,
      'updated_at' => fat_relation.object_updated_at,
      'type' => fat_relation.object_realtype
    }
  elsif(klass == TaliaCore::SemanticProperty)
    # We prepare a hash of properties for the SemanticProperty
    record = {
      'value' => fat_relation.property_value,
      'created_at' => fat_relation.property_created_at,
      'updated_at' => fat_relation.property_updated_at
    }
  end
  # Common attributes
  record['id'] = fat_relation.object_id
  # Instantiate the new record
  klass.send(:instantiate, record)
end

#objectObject

Creates an object from the given relation



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/talia_core/semantic_collection_item.rb', line 45

def object
  @object ||= begin
    if(@fat_relation)
      create_object_from(@fat_relation)
    elsif(@plain_relation)
      @plain_relation.object
    else
      raise(ArgumentError, "No relation was given to this object")
    end
  end
end

#relationObject

Return the relation object that was given



26
27
28
# File 'lib/talia_core/semantic_collection_item.rb', line 26

def relation
  @fat_relation || @plain_relation
end

#valueObject

Return the “value” (Semantic relation value or the related ActiveSource). String values will actually be PropertyString strings



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

def value
  semprop = object.is_a?(SemanticProperty)
  if(@object_type)
    assit(object, "Must have object for #{relation.predicate_uri}")
    raise(ArgumentError, 'Must not have a property for a typed item') if(semprop)
    @object_type.new(object.uri.to_s)
  else
    # Plain, return the object or the value for SemanticProperties
    semprop ? PropertyString.parse(object.value) : object
  end
end